如何使用cmake编译C ++共享库项目?

时间:2018-10-16 10:03:01

标签: c++ cmake

我有一个脚本compile.sh,该脚本是基于Eclipse IDE为我的C ++项目创建的。现在,我切换为使用cmake来编译我的C ++项目。我使用cmake配置了ccmake ../,将编译更改为使用g++-8gcc。我的CMakeList.txt正在使用-std=c++17标志,因为set(CMAKE_CXX_STANDARD 17)不可用。 当我在构建目录上使用命令make VERBOSE=1时,它会编译我的所有源文件,但是当它生成共享库时,它会失败并说找不到pthread_create,并且对{{1 }}。我猜是因为我没有像在gpsmm::stream(int)那样在$GPP -shared使用CMakeList.txt。谁能帮助我使用cmake编译共享库?

这是我的CMakeList.txt

compile.sh

错误:

cmake_minimum_required(VERSION 3.5)
project(sensors VERSION 0.0.0.0 LANGUAGES C CXX)
#message(CMAKE_SYSTEM_VERSION ${CMAKE_SYSTEM_VERSION})


message("---------------------------Release information---------------------------")
message("- Toolchain file: ${CMAKE_TOOLCHAIN_FILE}")
message("  - C   Compiler: ${CMAKE_C_COMPILER}")
message("  - CPP Compiler: ${CMAKE_CXX_COMPILER}")
message("-------------------------------------------------------------------------")

add_subdirectory(vendor/bcm/)

find_package(Threads REQUIRED)
# XXX: this is a little bit ugly but to be done 
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17 -Wall -fPIC -lpthread -lgps ")

# XXX: a little bit ugly aswell
file(GLOB_RECURSE SRC ${CMAKE_SOURCE_DIR}/code/*.cpp)

add_library(sensors ${SRC})
target_include_directories(sensors PUBLIC inc)

# XXX: target_link_lib also deals with transitive requirements, the name
#      does not really capture that 
#target_link_libraries(lit ${LIB_X11} ${LIB_FREETYPE} ${LIB_FONTCONFIG} )
# XXX: find out wich ones to use PUBLIC and PRIVATE
target_link_libraries(sensors bcm)

add_subdirectory(test)
add_subdirectory(vendor/sense)

#CMAKE_BUILD_TYPE Debug
# set(CMAKE_BUILD_TYPE Debug)

以及我以前如何使用$ make VERBOSE=1 /usr/bin/cmake -H/home/felipe/eclipse-workspace/SENSE-hackathon -B/home/felipe/eclipse-workspace/SENSE-hackathon/build --check-build-system CMakeFiles/Makefile.cmake 0 Re-run cmake file: Makefile older than: ../CMakeLists.txt ---------------------------Release information--------------------------- - Toolchain file: - C Compiler: /usr/bin/gcc - CPP Compiler: /usr/bin/g++-8 ------------------------------------------------------------------------- -- Configuring done -- Generating done -- Build files have been written to: /home/felipe/eclipse-workspace/SENSE-hackathon/build /usr/bin/cmake -E cmake_progress_start /home/felipe/eclipse-workspace/SENSE-hackathon/build/CMakeFiles /home/felipe/eclipse-workspace/SENSE-hackathon/build/CMakeFiles/progress.marks make -f CMakeFiles/Makefile2 all make[1]: Entering directory '/home/felipe/eclipse-workspace/SENSE-hackathon/build' make -f vendor/bcm/CMakeFiles/bcm.dir/build.make vendor/bcm/CMakeFiles/bcm.dir/depend make[2]: Entering directory '/home/felipe/eclipse-workspace/SENSE-hackathon/build' cd /home/felipe/eclipse-workspace/SENSE-hackathon/build && /usr/bin/cmake -E cmake_depends "Unix Makefiles" /home/felipe/eclipse-workspace/SENSE-hackathon /home/felipe/eclipse-workspace/SENSE-hackathon/vendor/bcm /home/felipe/eclipse-workspace/SENSE-hackathon/build /home/felipe/eclipse-workspace/SENSE-hackathon/build/vendor/bcm /home/felipe/eclipse-workspace/SENSE-hackathon/build/vendor/bcm/CMakeFiles/bcm.dir/DependInfo.cmake --color= make[2]: Leaving directory '/home/felipe/eclipse-workspace/SENSE-hackathon/build' make -f vendor/bcm/CMakeFiles/bcm.dir/build.make vendor/bcm/CMakeFiles/bcm.dir/build make[2]: Entering directory '/home/felipe/eclipse-workspace/SENSE-hackathon/build' [ 6%] Building C object vendor/bcm/CMakeFiles/bcm.dir/src/bcm2835.c.o cd /home/felipe/eclipse-workspace/SENSE-hackathon/build/vendor/bcm && /usr/bin/gcc -I/home/felipe/eclipse-workspace/SENSE-hackathon/vendor/bcm/include -o CMakeFiles/bcm.dir/src/bcm2835.c.o -c /home/felipe/eclipse-workspace/SENSE-hackathon/vendor/bcm/src/bcm2835.c [ 13%] Linking C static library libbcm.a cd /home/felipe/eclipse-workspace/SENSE-hackathon/build/vendor/bcm && /usr/bin/cmake -P CMakeFiles/bcm.dir/cmake_clean_target.cmake cd /home/felipe/eclipse-workspace/SENSE-hackathon/build/vendor/bcm && /usr/bin/cmake -E cmake_link_script CMakeFiles/bcm.dir/link.txt --verbose=1 /usr/bin/ar qc libbcm.a CMakeFiles/bcm.dir/src/bcm2835.c.o /usr/bin/ranlib libbcm.a make[2]: Leaving directory '/home/felipe/eclipse-workspace/SENSE-hackathon/build' [ 13%] Built target bcm make -f CMakeFiles/sensors.dir/build.make CMakeFiles/sensors.dir/depend make[2]: Entering directory '/home/felipe/eclipse-workspace/SENSE-hackathon/build' cd /home/felipe/eclipse-workspace/SENSE-hackathon/build && /usr/bin/cmake -E cmake_depends "Unix Makefiles" /home/felipe/eclipse-workspace/SENSE-hackathon /home/felipe/eclipse-workspace/SENSE-hackathon /home/felipe/eclipse-workspace/SENSE-hackathon/build /home/felipe/eclipse-workspace/SENSE-hackathon/build /home/felipe/eclipse-workspace/SENSE-hackathon/build/CMakeFiles/sensors.dir/DependInfo.cmake --color= make[2]: Leaving directory '/home/felipe/eclipse-workspace/SENSE-hackathon/build' make -f CMakeFiles/sensors.dir/build.make CMakeFiles/sensors.dir/build make[2]: Entering directory '/home/felipe/eclipse-workspace/SENSE-hackathon/build' [ 20%] Building CXX object CMakeFiles/sensors.dir/code/sensor/ISensor.cpp.o /usr/bin/g++-8 -I/home/felipe/eclipse-workspace/SENSE-hackathon/inc -I/home/felipe/eclipse-workspace/SENSE-hackathon/vendor/bcm/include -std=c++17 -Wall -fPIC -lpthread -lgps -o CMakeFiles/sensors.dir/code/sensor/ISensor.cpp.o -c /home/felipe/eclipse-workspace/SENSE-hackathon/code/sensor/ISensor.cpp [ 26%] Building CXX object CMakeFiles/sensors.dir/code/sensor/HCSR04.cpp.o /usr/bin/g++-8 -I/home/felipe/eclipse-workspace/SENSE-hackathon/inc -I/home/felipe/eclipse-workspace/SENSE-hackathon/vendor/bcm/include -std=c++17 -Wall -fPIC -lpthread -lgps -o CMakeFiles/sensors.dir/code/sensor/HCSR04.cpp.o -c /home/felipe/eclipse-workspace/SENSE-hackathon/code/sensor/HCSR04.cpp [ 33%] Building CXX object CMakeFiles/sensors.dir/code/sensor/Random.cpp.o /usr/bin/g++-8 -I/home/felipe/eclipse-workspace/SENSE-hackathon/inc -I/home/felipe/eclipse-workspace/SENSE-hackathon/vendor/bcm/include -std=c++17 -Wall -fPIC -lpthread -lgps -o CMakeFiles/sensors.dir/code/sensor/Random.cpp.o -c /home/felipe/eclipse-workspace/SENSE-hackathon/code/sensor/Random.cpp [ 40%] Building CXX object CMakeFiles/sensors.dir/code/sensor/GPS.cpp.o /usr/bin/g++-8 -I/home/felipe/eclipse-workspace/SENSE-hackathon/inc -I/home/felipe/eclipse-workspace/SENSE-hackathon/vendor/bcm/include -std=c++17 -Wall -fPIC -lpthread -lgps -o CMakeFiles/sensors.dir/code/sensor/GPS.cpp.o -c /home/felipe/eclipse-workspace/SENSE-hackathon/code/sensor/GPS.cpp [ 46%] Building CXX object CMakeFiles/sensors.dir/code/CommunicationLib.cpp.o /usr/bin/g++-8 -I/home/felipe/eclipse-workspace/SENSE-hackathon/inc -I/home/felipe/eclipse-workspace/SENSE-hackathon/vendor/bcm/include -std=c++17 -Wall -fPIC -lpthread -lgps -o CMakeFiles/sensors.dir/code/CommunicationLib.cpp.o -c /home/felipe/eclipse-workspace/SENSE-hackathon/code/CommunicationLib.cpp [ 53%] Building CXX object CMakeFiles/sensors.dir/code/utils/LRUBuffer.cpp.o /usr/bin/g++-8 -I/home/felipe/eclipse-workspace/SENSE-hackathon/inc -I/home/felipe/eclipse-workspace/SENSE-hackathon/vendor/bcm/include -std=c++17 -Wall -fPIC -lpthread -lgps -o CMakeFiles/sensors.dir/code/utils/LRUBuffer.cpp.o -c /home/felipe/eclipse-workspace/SENSE-hackathon/code/utils/LRUBuffer.cpp [ 60%] Building CXX object CMakeFiles/sensors.dir/code/utils/Metric.cpp.o /usr/bin/g++-8 -I/home/felipe/eclipse-workspace/SENSE-hackathon/inc -I/home/felipe/eclipse-workspace/SENSE-hackathon/vendor/bcm/include -std=c++17 -Wall -fPIC -lpthread -lgps -o CMakeFiles/sensors.dir/code/utils/Metric.cpp.o -c /home/felipe/eclipse-workspace/SENSE-hackathon/code/utils/Metric.cpp [ 66%] Building CXX object CMakeFiles/sensors.dir/code/utils/SkinnyBuffer.cpp.o /usr/bin/g++-8 -I/home/felipe/eclipse-workspace/SENSE-hackathon/inc -I/home/felipe/eclipse-workspace/SENSE-hackathon/vendor/bcm/include -std=c++17 -Wall -fPIC -lpthread -lgps -o CMakeFiles/sensors.dir/code/utils/SkinnyBuffer.cpp.o -c /home/felipe/eclipse-workspace/SENSE-hackathon/code/utils/SkinnyBuffer.cpp [ 73%] Linking CXX static library libsensors.a /usr/bin/cmake -P CMakeFiles/sensors.dir/cmake_clean_target.cmake /usr/bin/cmake -E cmake_link_script CMakeFiles/sensors.dir/link.txt --verbose=1 /usr/bin/ar qc libsensors.a CMakeFiles/sensors.dir/code/sensor/ISensor.cpp.o CMakeFiles/sensors.dir/code/sensor/HCSR04.cpp.o CMakeFiles/sensors.dir/code/sensor/Random.cpp.o CMakeFiles/sensors.dir/code/sensor/GPS.cpp.o CMakeFiles/sensors.dir/code/CommunicationLib.cpp.o CMakeFiles/sensors.dir/code/utils/LRUBuffer.cpp.o CMakeFiles/sensors.dir/code/utils/Metric.cpp.o CMakeFiles/sensors.dir/code/utils/SkinnyBuffer.cpp.o /usr/bin/ranlib libsensors.a make[2]: Leaving directory '/home/felipe/eclipse-workspace/SENSE-hackathon/build' [ 73%] Built target sensors make -f test/CMakeFiles/sensorTests.dir/build.make test/CMakeFiles/sensorTests.dir/depend make[2]: Entering directory '/home/felipe/eclipse-workspace/SENSE-hackathon/build' cd /home/felipe/eclipse-workspace/SENSE-hackathon/build && /usr/bin/cmake -E cmake_depends "Unix Makefiles" /home/felipe/eclipse-workspace/SENSE-hackathon /home/felipe/eclipse-workspace/SENSE-hackathon/test /home/felipe/eclipse-workspace/SENSE-hackathon/build /home/felipe/eclipse-workspace/SENSE-hackathon/build/test /home/felipe/eclipse-workspace/SENSE-hackathon/build/test/CMakeFiles/sensorTests.dir/DependInfo.cmake --color= make[2]: Leaving directory '/home/felipe/eclipse-workspace/SENSE-hackathon/build' make -f test/CMakeFiles/sensorTests.dir/build.make test/CMakeFiles/sensorTests.dir/build make[2]: Entering directory '/home/felipe/eclipse-workspace/SENSE-hackathon/build' [ 80%] Building CXX object test/CMakeFiles/sensorTests.dir/main.cpp.o cd /home/felipe/eclipse-workspace/SENSE-hackathon/build/test && /usr/bin/g++-8 -I/home/felipe/eclipse-workspace/SENSE-hackathon/test/include -I/home/felipe/eclipse-workspace/SENSE-hackathon/inc -I/home/felipe/eclipse-workspace/SENSE-hackathon/vendor/bcm/include -std=c++17 -Wall -fPIC -lpthread -lgps -o CMakeFiles/sensorTests.dir/main.cpp.o -c /home/felipe/eclipse-workspace/SENSE-hackathon/test/main.cpp [ 86%] Linking CXX executable sensorTests cd /home/felipe/eclipse-workspace/SENSE-hackathon/build/test && /usr/bin/cmake -E cmake_link_script CMakeFiles/sensorTests.dir/link.txt --verbose=1 /usr/bin/g++-8 -std=c++17 -Wall -fPIC -lpthread -lgps CMakeFiles/sensorTests.dir/main.cpp.o -o sensorTests -L/usr/lib/gcc/x86_64-linux-gnu/6 ../libsensors.a ../vendor/bcm/libbcm.a ../libsensors.a(GPS.cpp.o): In function `GPS::run()': GPS.cpp:(.text+0x973): undefined reference to `gpsmm::stream(int)' GPS.cpp:(.text+0x9cb): undefined reference to `gpsmm::waiting(int)' GPS.cpp:(.text+0x9e5): undefined reference to `gpsmm::read()' GPS.cpp:(.text+0xa2a): undefined reference to `gpsmm::read()' GPS.cpp:(.text+0xc31): undefined reference to `gpsmm::~gpsmm()' GPS.cpp:(.text+0xc56): undefined reference to `gpsmm::~gpsmm()' ../libsensors.a(GPS.cpp.o): In function `std::thread::thread<GPS::connect()::{lambda()#1}>(GPS::connect()::{lambda()#1}&&)': GPS.cpp:(.text+0xdad): undefined reference to `pthread_create' ../libsensors.a(GPS.cpp.o): In function `gpsmm::gpsmm(char const*, char const*)': GPS.cpp:(.text._ZN5gpsmmC2EPKcS1_[_ZN5gpsmmC5EPKcS1_]+0x17): undefined reference to `vtable for gpsmm' GPS.cpp:(.text._ZN5gpsmmC2EPKcS1_[_ZN5gpsmmC5EPKcS1_]+0x5f): undefined reference to `gpsmm::gps_inner_open(char const*, char const*)' collect2: error: ld returned 1 exit status test/CMakeFiles/sensorTests.dir/build.make:96: recipe for target 'test/sensorTests' failed make[2]: *** [test/sensorTests] Error 1 make[2]: Leaving directory '/home/felipe/eclipse-workspace/SENSE-hackathon/build' CMakeFiles/Makefile2:178: recipe for target 'test/CMakeFiles/sensorTests.dir/all' failed make[1]: *** [test/CMakeFiles/sensorTests.dir/all] Error 2 make[1]: Leaving directory '/home/felipe/eclipse-workspace/SENSE-hackathon/build' Makefile:83: recipe for target 'all' failed make: *** [all] Error 2

进行编译
compile.sh

1 个答案:

答案 0 :(得分:0)

我将target_link_libraries(sensors bcm pthread gps)添加到了CMakeList.txt上,就像评论中提到的@virgesmith一样。