Search code examples
cmakelibtorch

CMake -- undefined reference when linking additional libraries which I don't use


I've been following cmake example from here and got a strange problem while linking

Project structure:

├── 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)

I get an error while linking:

/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 full output: 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;
}

Notice that I don't use libtorch anywhere in code, I only link it, and get the linkage error. HOWEVER, if I remove libtorch from linked libraries, the problem disappears.

It also disappears if I add AudioFile.cpp directly to mainapp in add_executable and get rid of audiofile in CMakeLists.txt

AudioFile<T> is defined in AudioFile.cpp -- a template class with explicit instantiations for double and float in AudioFile.cpp. It comes from this library. And the needed symbol seems to be present in 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> >)

Solution

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

    From the object dump above it seems like mainapp target inherited old ABI from libtorch, while audiofile used the new one. I think the question is now closed.