CMake-链接我不使用的其他库时的未定义参考

时间:2019-01-31 11:03:09

标签: cmake libtorch

我一直在跟踪here的cmake示例,并在链接时遇到了一个奇怪的问题

项目结构:

SELECT  BOM.STYLE_ID,
BOM.SEASON_ID,BOM.Ad_compo_desc,
BOM.CONTENT_CLASS,
sum(CASE WHEN BOM.CONTENT_CLASS = 'ART' AND BOM.Ad_compo_desc LIKE '%emb%' THEN 1 else 0 END)  AS EMBRO,
sum(CASE WHEN BOM.CONTENT_CLASS = 'ART' AND BOM.Ad_compo_desc LIKE '%print%' THEN 1 else 0 END)  AS PRINTING
FROM IPLEXSTY_AD_BOM_DTL BOM
WHERE CONTENT_CLASS = 'ART' --AND BOM.Ad_compo_desc IS NOT NULL
GROUP BY BOM.STYLE_ID,
BOM.SEASON_ID,BOM.CONTENT_CLASS,
BOM.Ad_compo_desc,BOM.CONTENT_CLASS

Distinct

├── CMakeLists.txt
├── compile_commands.json -> build/compile_commands.json
├── external
│   └── AudioFile
│       ├── AudioFile.cpp
│       ├── AudioFile.h
│       ├── LICENSE
│       ├── README.md
│       └── tests
└── test.cpp

链接时出现错误:

CMakeLists.txt

cmake_minimum_required(VERSION 3.0 FATAL_ERROR) project(torch-sound) set(CMAKE_CXX_EXTENSIONS OFF) set(CMAKE_EXPORT_COMPILE_COMMANDS ON) set(CMAKE_PREFIX_PATH "/home/nikita/tmp/libtorch") set(AUDIOFILE_PATH "external/AudioFile") find_package(Torch REQUIRED) add_library(audiofile OBJECT "${AUDIOFILE_PATH}/AudioFile.cpp") target_include_directories(audiofile PUBLIC "${AUDIOFILE_PATH}") add_executable(mainapp test.cpp) target_include_directories(mainapp PUBLIC "${AUDIOFILE_PATH}" . ) target_link_libraries(mainapp audiofile "${TORCH_LIBRARIES}") # if I remove this ^^^^^^^^^^^^^^^^^^^ # the problem disappears set_property(TARGET mainapp PROPERTY CXX_STANDARD 11) 的完整输出: https://pastebin.com/tqrLVjZE

/usr/bin/c++ -rdynamic CMakeFiles/mainapp.dir/test.cpp.o CMakeFiles/audiofile.dir/external/AudioFile/AudioFile.cpp.o -o mainapp -Wl,-rpath,/home/nikita/tmp/libtorch/lib /home/nikita/tmp/libtorch/lib/libtorch.so -Wl,--no-as-needed,/home/nikita/tmp/libtorch/lib/libcaffe2.so -Wl,--as-needed /home/nikita/tmp/libtorch/lib/libc10.so -lpthread /usr/bin/ld: CMakeFiles/mainapp.dir/test.cpp.o: in function `main': test.cpp:(.text+0x56): undefined reference to `AudioFile<double>::load(std::string)' collect2: error: ld returned 1 exit status

make VERBOSE=1

请注意,我在代码中的任何地方都没有使用test.cpp,我只链接了它,并得到了链接错误。但是,如果我从链接库中删除#include <AudioFile.h> #include <string> int main () { AudioFile<double> audio_file; audio_file.load("/home/nikita/Music/split-track08.wav"); std::cout << "Success!" << std::endl; return 0; } ,问题就会消失。

如果我将libtorch直接添加到libtorch中的AudioFile.cpp并在mainapp中删除add_executable,它也会消失

audiofileCMakeLists.txt中定义-模板类,在AudioFile<T>中具有AudioFile.cppdouble的显式实例化。它来自this库。并且所需的符号似乎出现在float中:

AudioFile.cpp

1 个答案:

答案 0 :(得分:0)

[nikita@x1c build]$ objdump -C -t CMakeFiles/mainapp.dir/test.cpp.o | grep load
0000000000000000         *UND*  0000000000000000 AudioFile<double>::load(std::string)

从上方的对象转储中,mainapp个目标似乎是从libtorch继承了旧的ABI,而audiofile使用了新的。我认为问题已经解决。