我正在尝试使用 libcinder 进行一些可视化。在集成我现有的代码库时,我遇到了一些我无法弄清楚的链接器错误。我将问题归结为一个最小的示例,结果如下:
cd /build/src && /usr/bin/cmake -E cmake_link_script CMakeFiles/BasicApp.dir/link.txt --verbose=1
/usr/bin/c++ -O3 -DNDEBUG CMakeFiles/BasicApp.dir/BasicApp.cpp.o -o ../bin/BasicApp -rdynamic -lPocoUtil -lPocoFoundation
make[2]: Leaving directory '/build'
[100%] Built target BasicApp
make[1]: Leaving directory '/build'
/usr/bin/cmake -E cmake_progress_start /build/CMakeFiles 0
如果根本不涉及煤渣,一切正常,如上所示。如果我将我的函数添加到最小的 cinder 示例 (BasicApp),我会在编译时得到以下输出:
cd /build/src && /usr/bin/cmake -E cmake_link_script CMakeFiles/BasicApp.dir/link.txt --verbose=1
/usr/bin/c++ -O3 -DNDEBUG CMakeFiles/BasicApp.dir/BasicApp.cpp.o -o ../bin/BasicApp -rdynamic -lPocoUtil -lPocoFoundation /cinder/lib/linux/x86_64/ogl/Release/libcinder.a -lGLU -lGL -lSM -lICE -lX11 -lXext -lXcursor -lXinerama -lXrandr -lXi -lz -lcurl -lfontconfig -lpulse -lmpg123 -lsndfile -lgobject-2.0 -lglib-2.0 -lgstreamer-1.0 -lgstbase-1.0 -lgstapp-1.0 -lgstvideo-1.0 -lgstgl-1.0 /cinder/lib/linux/x86_64//libboost_system.a /cinder/lib/linux/x86_64//libboost_filesystem.a -ldl -lpthread
CMakeFiles/BasicApp.dir/BasicApp.cpp.o: In function `load_images(std::string, std::string, int, int)':
BasicApp.cpp:(.text+0xca2): undefined reference to `Poco::DirectoryIterator::DirectoryIterator(std::string const&)'
BasicApp.cpp:(.text+0xd57): undefined reference to `Poco::DirectoryIterator::DirectoryIterator(std::string const&)'
collect2: error: ld returned 1 exit status
src/CMakeFiles/BasicApp.dir/build.make:118: recipe for target 'bin/BasicApp' failed
make[2]: *** [bin/BasicApp] Error 1
make[2]: Leaving directory '/build'
CMakeFiles/Makefile2:1244: recipe for target 'src/CMakeFiles/BasicApp.dir/all' failed
make[1]: *** [src/CMakeFiles/BasicApp.dir/all] Error 2
make[1]: Leaving directory '/build'
Makefile:138: recipe for target 'all' failed
make: *** [all] Error 2
我假设 cinder cmake 文件中设置的某些配置搞砸了,但我不明白为什么或如何修复它。这是 gcc 5.4.0 版和 cmake 3.5.1 版。
这是一个存在此问题的简化 cmake 文件:
cmake_minimum_required(VERSION 3.2)
project(segslam C CXX)
set(CMAKE_CXX_STANDARD 11)
list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake/poco")
find_package(Poco REQUIRED)
#include($ENV{CINDER_PATH}/proj/cmake/configure.cmake)
#set(cinder_DIR $ENV{CINDER_PATH}/${CINDER_LIB_DIRECTORY})
#find_package(cinder REQUIRED PATHS " $ENV{CINDER_PATH}/${CINDER_LIB_DIRECTORY}")
add_executable(BasicApp BasicApp.cpp)
#target_include_directories(BasicApp PUBLIC $ENV{CINDER_PATH}/include)
#target_link_libraries(BasicApp cinder)
target_link_libraries(BasicApp ${Poco_LIBRARIES})
set_target_properties(BasicApp PROPERTIES
DEBUG_POSTFIX "d"
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")
install(TARGETS BasicApp RUNTIME DESTINATION bin COMPONENT main)
install(FILES ${srcs} DESTINATION app COMPONENT main)
如果注释行未注释,它会在 cinder 中链接,并且事情会中断。我正在使用这里的 FindPoco.cmake 文件。cinder cmake 文件位于此处。
链接顺序是什么似乎并不重要,我使用的其他库都没有这个问题。