Libtorch does not link together with boost

Hi all,

I would like to report a build issue when linking an application that uses both libtorch and boost.


#include <boost/program_options/options_description.hpp>

int main() {
    boost::program_options::options_description d("");


cmake_minimum_required(VERSION 3.0 FATAL_ERROR)

set(Torch_DIR "${CMAKE_SOURCE_DIR}/libtorch/share/cmake/Torch")
set(Caffe2_DIR "${CMAKE_SOURCE_DIR}/libtorch/share/cmake/Caffe2")
find_package(Torch REQUIRED)


find_package(Boost COMPONENTS program_options REQUIRED)

add_executable(main tensor_test.cpp)
target_link_libraries(main Boost::program_options ${TORCH_LIBRARIES})

set_property(TARGET main PROPERTY CXX_STANDARD 11)

This is the link error I get:

Scanning dependencies of target main
[ 50%] Building CXX object CMakeFiles/main.dir/tensor_test.cpp.o
[100%] Linking CXX executable main
CMakeFiles/main.dir/tensor_test.cpp.o: In function `main':
tensor_test.cpp:(.text+0x6b): undefined reference to `boost::program_options::options_description::options_description(std::string const&, unsigned int, unsigned int)'

When removing the ${TORCH_LIBRARIES} variable from target_link_libraries linking works correctly.

The same issue arises with other shared libraries.

I guess this is a similar issue to the one reported here:

I am on Ubuntu18.04 and I am using libtorch 1.0.0.dev20181022, boost 1.65.1


I just used your two files and that compiles fine for me on Ubuntu 14.04.
Maybe try including them by two different calls?

target_link_libraries(main Boost::program_options)
target_link_libraries(main ${TORCH_LIBRARIES})


thank you for trying it out.
The problem seems related to the Ubuntu version.
I confirm that the build works with 14.04, but it fails on Ubuntu 16.04 and 18.04.

I particular, I moved the object file from the working 14.04 machine to the 16.04 machine and the link stage failed.
I have used g++ 5.5.0 20171010 on both.

(The suggestion of duplicating the target_link_libraries function does not make a difference)

Just for the record,
I have worked around the problem by compiling libtorch locally on my machine from

1 Like

Does anybody know how to fix this problem? I’m finding the same thing happens with another proprietary library.

Could this be caused by the nightly build coming with an absolute file path to /usr/local/cuda/lib64/libculibos.a in Caffe2Targets.cmake?

I think that this can be related to the C++11 ABI as well, see this comment I posted: Issues linking with libtorch (C++11 ABI?), I’m facing similar issues with other libraries. The issue seems that libtorch is adding this definition to compilation _GLIBCXX_USE_CXX11_ABI=0, which forces GCC to use old C++11 ABI, and this is incompatible with your boost dependency, hence the error you’re getting, because the std::string was changed between old and new C++11 ABI.

Hi,there is a similar problem …
When libtorch is built with opencv :

cmake_minimum_required(VERSION 3.0 FATAL_ERROR)

find_package(Torch REQUIRED)
find_package(OpenCV 4 REQUIRED)

message(STATUS "Pytorch status:")
message(STATUS "    libraries: ${TORCH_LIBRARIES}")

message(STATUS "OpenCV library status:")
message(STATUS "    version: ${OpenCV_VERSION}")
message(STATUS "    libraries: ${OpenCV_LIBS}")
message(STATUS "    include path: ${OpenCV_INCLUDE_DIRS}")

add_executable(simnet main.cpp)


target_link_libraries(simnet "${TORCH_LIBRARIES}")
target_link_libraries(simnet "${OpenCV_LIBS}")

It will give rise to ‘undefined reference to `cv::imread(std::string const&, int)’’
And when I removed ‘target_link_libraries(simnet “${TORCH_LIBRARIES}”)’ and the build process succeeded.

Hi, I have the same problem with opencv and libtorch
I’ve also tried this piece of code :
But it does’nt work either…
Has anyone found the solution?

I was able to fix it by building from source instead of trying to use the nightly builds.

Thanks for the reply KevNull
I’ve just build pytorch from source now and i search how to link it with my code.

We now have new ABI binaries for libtorch. They can be found on, or at