我一直从这里关注 cmake 示例,在链接时遇到了一个奇怪的问题
项目结构:
├── 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)
链接时出现错误:
/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
完整输出:
https ://pastebin.com/tqrLVjZE
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
inadd_executable
并去掉audiofile
in ,它也会消失CMakeLists.txt
AudioFile<T>
定义在AudioFile.cpp
- 一个模板类,其中和 in 具有显式double
实例float
化AudioFile.cpp
。它来自这个库。所需的符号似乎出现在objdump -t -C
:
0000000000000000 w F .text._ZN9AudioFileIfE4loadENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE 00000000000002be AudioFile<float>::load(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)
0000000000000000 w F .text._ZN9AudioFileIdE4loadENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE 00000000000002be AudioFile<double>::load(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)