c++cmakestatic-librariesstatic-linking

how to link re2 library in my project like a static library with cmake


My question: in this moment I'm linking the libre2 dinamically, but I want to do this statically. I installed the library in my pc (sudo apt-get install libre2-dev), got the "binary" and linked this binary "libre2.so" in my executable. But I want to git clone the repository or to do this by git submodule, then build this repository and link it in my project statically.

I'm new here and sorry for my bad English.

  1. my project structure

    • bin
    • build
    • external
      • re2
    • main.cpp
    • CMakeLists.txt
    • README.md
  2. CMakeLists.txt

    cmake_minimum_required( VERSION 2.6 )

    project( simmc-agent )

    version number

    set ( VERSION_MAJOR 0 )
    set ( VERSION_MINOR 0 )

    cpr requires c++11

    set ( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11" )

    src : main

    file ( GLOB SOURCES *.cpp )

    linking res library dinamically

    set(RE2_LIBRARIES -L${RE2_LIBRARY_DIR} -l libre2.so)

    src : collect functions - depend on OS

    if ( WIN32 )
    file ( GLOB SOURCES ${SOURCES} src/windows/.cpp )
    else () # if( UNIX )
    file ( GLOB SOURCES ${SOURCES} src/linux/
    .cpp )
    endif ()

    headers

    include_directories ( "include" )

    test

    option( PRINT_JSON "Set to ON to print json objects before sending" OFF )
    message(STATUS "${PRINT_JSON}: ${${PRINT_JSON}}")
    if ( PRINT_JSON )
    add_definitions ( -DPRINT_JSON )
    endif ()

    compile

    set ( CMAKE_RUNTIME_OUTPUT_DIRECTORY "../bin" ) add_executable ( agent-v${VERSION_MAJOR}.${VERSION_MINOR} ${SOURCES} )
    target_link_libraries ( agent-v${VERSION_MAJOR}.${VERSION_MINOR} ${RE2_LIBRARY} )

  3. main.cpp

    #include
    #include <re2/re2.h>

    using namespace std;
    using namespace re2;

    int main (int argc, char **argv) {
    cout << "hello world" << endl;

    int matchResult;

    matchResult = RE2::FullMatch("hello", "h.*o"); cout << "matchResult = " << matchResult << endl;

    return 0;  
    

    }


Solution

  • EDIT - (26.01.17) : Hello, guys. I'm here to talk how I resolve it. Following some tips gave here, I create a file called re2.cmake:

    cmake_minimum_required ( VERSION 2.8.7 )
    
    if (NOT RE2_NAME)
    
    include (ExternalProject)
    
    SET (RE2_INCLUDE_DIRS ${CMAKE_CURRENT_BINARY_DIR}/re2/src/re2/)
    SET (RE2_EXTRA_INCLUDE_DIRS ${CMAKE_CURRENT_BINARY_DIR}/re2/src/re2/)
    SET (RE2_URL https://github.com/google/re2.git)
    SET (RE2_BUILD ${CMAKE_BINARY_DIR}/re2/src/re2)  
    SET (RE2_LIBRARIES ${RE2_BUILD}/obj/so/libre2.so)
    get_filename_component(RE2_STATIC_LIBRARIES ${RE2_BUILD}/libre2.a ABSOLUTE)  
    SET (RE2_INCLUDES ${RE2_BUILD})
    
    if ( WIN32 )   
      SET (RE2_STATIC_LIBRARIES ${RE2_BUILD}/${CMAKE_BUILD_TYPE}/re2.lib) 
    else ()   
      SET (RE2_STATIC_LIBRARIES ${RE2_BUILD}/libre2.a) 
    endif ()
    
    ExternalProject_Add(RE2      
      PREFIX RE2      
      GIT_REPOSITORY ${RE2_URL}      
      # GIT_TAG ${RE2_TAG}      
      DOWNLOAD_DIR "${DOWNLOAD_LOCATION}"      
      BUILD_IN_SOURCE 1      
      INSTALL_COMMAND sudo make install     
       CMAKE_CACHE_ARGS         
        -DCMAKE_BUILD_TYPE:STRING=Release          -DCMAKE_VERBOSE_MAKEFILE:BOOL=OFF          -DCMAKE_POSITION_INDEPENDENT_CODE:BOOL=ON  
    )
    
     ## put re2 includes in the directory where they are expected  
     add_custom_target(re2_create_destination_dir COMMAND ${CMAKE_COMMAND} -E make_directory ${RE2_INCLUDE_DIR}/re2 DEPENDS re2)   
    
     add_custom_target(re2_copy_headers_to_destination DEPENDS re2_create_destination_dir)   
    
     foreach(header_file ${RE2_HEADERS})      
        add_custom_command(TARGET re2_copy_headers_to_destination PRE_BUILD COMMAND ${CMAKE_COMMAND} -E copy ${header_file} ${RE2_INCLUDE_DIR}/re2)  
      endforeach ()   
    
      ADD_LIBRARY(RE2_LIB STATIC IMPORTED DEPENDS RE2)  
      SET_TARGET_PROPERTIES(RE2_LIB PROPERTIES IMPORTED_LOCATION ${RE2_STATIC_LIBRARIES})
    
    endif (NOT RE2_NAME)
    

    This file download the repository, building and install the library libre2 in my computer. This library has a dependency for Thread library* (I thinks all linux operation system come with this library).

    But, has a problem: I only do this, if I'm a root user. Because the library used the "make install" command and to do it, you need to be a root user.

    my project structure:

    - bin
    - build
    - src
    - include
    - modules
        - re2.cmake
    - CMakeLists.txt
    

    FOllowing my CMakeLists.txt:

    cmake_minimum_required ( VERSION 2.8.7 )
    
    project ( project C CXX)
    
    # version number
    SET ( VERSION_MAJOR 0 )
    SET ( VERSION_MINOR 0 )
    
    SET ( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11" )
    
    # src : main
    file ( GLOB SOURCES src/main.cpp )
    
    # headers
    include_directories ( "include" )
    
    # src : libre2 - Download, build and install the library
    
    find_package (Threads)
    
    include ( "modules/re2.cmake" )
    set(RE2_STATIC_LIBRARIES -L${RE2_LIBRARY_DIR} -l libre2.a )
    
    # compile 
    set ( CMAKE_RUNTIME_OUTPUT_DIRECTORY "../bin" )
    add_executable ( project-v${VERSION_MAJOR}.${VERSION_MINOR} ${SOURCES})
    target_link_libraries ( project-v${VERSION_MAJOR}.${VERSION_MINOR} ${RE2_STATIC_LIBRARIES})
    
    add_dependencies(project-v${VERSION_MAJOR}.${VERSION_MINOR} RE2)
    
    target_link_libraries (project-v${VERSION_MAJOR}.${VERSION_MINOR} ${CMAKE_THREAD_LIBS_INIT})