What must I do with C++ libraries on cross compila

2019-05-20 06:42发布

问题:

Here is my compiler part of my config:

IF(UNIX)
    ## Compiler flags

    # specify the cross compiler
    SET(CMAKE_C_COMPILER   /home/username/projects/buildroot/output/host/usr/bin/arm-linux-gcc)
    SET(CMAKE_CXX_COMPILER /home/username/projects/buildroot/output/host/usr/bin/arm-linux-g++)

    if(CMAKE_COMPILER_IS_GNUCXX)
        set(CMAKE_CXX_FLAGS "-O3")
        set(CMAKE_EXE_LINKER_FLAGS "-lsqlite3 -lrt -lpthread")
    endif()

    target_link_libraries(complex
      ${Boost_FILESYSTEM_LIBRARY}
      ${Boost_SYSTEM_LIBRARY})
ENDIF(UNIX)

There are 3 problems : -lsqlite3 -lrt -lpthread

How must I to make them for my architecture and specify here? How to set (using set?) the path of compiled libraries after I will get them recompiled for my architecture somehow?

回答1:

If you want to do cross-compilation with CMake you really should use a toolchain file for that. See the CMake Wiki for an introduction. In order to use third-party libraries (i.e. not included by the cross-compilation toolchain) you need to cross-compile them too.

Edit: Since you are using the buildroot toolchain, you can use the already included CMake toolchain file. Just pass -DCMAKE_TOOLCHAIN_FILE=/home/username/projects/buildroot/output/toolchainfile.cmake when invoking CMake. No need to set CMAKE_C_COMPILER and CMAKE_CXX_COMPILER in your CMakeLists.txt file. Also, setting CMAKE_CXX_FLAGS and CMAKE_EXE_LINKER_FLAGS is considered to be very bad practice.

Presumably you have built sqlite3 while building the buildroot toolchain, so you can use it just like any other library. I.e:

find_path(SQLITE3_INCLUDE_DIR sqlite3.h)
find_library(SQLITE3_LIBRARY sqlite3)
if(NOT SQLITE3_INCLUDE_DIR)
  message(SEND_ERROR "Failed to find sqlite3.h")
endif()
if(NOT SQLITE3_LIBRARY)
  message(SEND_ERROR "Failed to find the sqlite3 library")
endif()

find_package(Threads REQUIRED)

# ...

target_link_libraries(complex
  ${Boost_FILESYSTEM_LIBRARY}
  ${Boost_SYSTEM_LIBRARY}
  ${SQLITE3_LIBRARY}
  ${CMAKE_THREAD_LIBS_INIT}
  rt)

Lastly, do not set CMAKE_CXX_FLAGS to -O3. The user should pass -DCMAKE_BUILD_TYPE=Release when configuring the project instead.



回答2:

You'll have to cross-compile the dependencies as well. The path depends on where you install them.

Btw., using -lpthread is not the safe way of getting POSIX threads. You should give the option -pthread to both the compiler and the linker.