Repository: pytorch
Issue: [libtorch] Build shared library with libtorch and compile a static library
Labels: triaged,module: static linking

I am playing libtorch with cmake / linux. It worked well when I built an executable file, but didn't work when I tried to build a shared library with libtorch and make the project loading the generated .so file. The error and CMakeLists are shown below.

Environment:
Ubuntu 16.04 
CUDA 10.1
libtorch:1.7.0+cu101 
cmake 3.5

[ makeing .so ] 
|---include
|            |---- *.h
|---src
|            |---- *.cpp
|---build
|---CMakeLists.txt

[ loading .so ] 
|---include
|        |---- *.h
|---lib
|      |---- *.so
|---build
|---CMakeLists.txt
|---main.cpp


`[100%] Linking CXX executable hwcd
/usr/bin/ld: warning: libgomp-7c85b1e2.so.1, needed by //home2/user/pk/libtorch/lib/libc10.so, not found (try using -rpath or -rpath-link)
/usr/bin/ld: warning: libcudart-1b201d85.so.10.1, needed by //home2/user/pk/libtorch/lib/libtorch_cpu.so, not found (try using -rpath or -rpath-link)
/usr/bin/ld: warning: libc10_cuda.so, needed by //home2/user/pk/libtorch/lib/libtorch_cuda.so, not found (try using -rpath or -rpath-link)
/usr/bin/ld: warning: libnvToolsExt-3965bdd0.so.1, needed by //home2/user/pk/libtorch/lib/libtorch_cuda.so, not found (try using -rpath or -rpath-link)
//home2/user/pk/libtorch/lib/libtorch_cpu.so: undefined reference to `GOMP_loop_dynamic_start@GOMP_1.0'
//home2/user/pk/libtorch/lib/libtorch_cuda.so: undefined reference to `c10::cuda::getDefaultCUDAStream(short)'
//home2/user/pk/libtorch/lib/libtorch_cpu.so: undefined reference to `GOMP_loop_dynamic_next@GOMP_1.0'
[other undefined references...]
`

[CMakeLists of loading .so]

`cmake_minimum_required(VERSION 3.5)
project(hwcd)
include_directories(include)
add_library(final SHARED IMPORTED)
set_property(TARGET final PROPERTY IMPORTED_LOCATION /home2/user/Projects/torchplayground/final3/lib/libfinal.so )
link_directories(/home2/user/Projects/torchplayground/final3/lib/final)
add_executable(hwcd main.cpp)
target_link_libraries(hwcd final)
set_property(TARGET hwcd PROPERTY CXX_STANDARD 14)
`

[CMakeLists of makeing .so]

`cmake_minimum_required(VERSION 3.2)# Define project name & version
set(PROJECT_VERSION "0.0.1")
set (CMAKE_CXX_STANDARD 14)

project(final VERSION ${PROJECT_VERSION})# LANGUAGES CXX)

include_directories(include)
#######################
set(Torch_DIR /home2/user/pk/libtorch/share/cmake/Torch)
set(OpenCV_DIR "/home2/user/pk/OpenCV/share/OpenCV")

find_package(Torch REQUIRED)  # find libtorch
find_package(OpenCV REQUIRED)  # find OpenCV

FILE(GLOB SC_FILES  "src/*.cpp" "include/*.h" )

ADD_LIBRARY(final SHARED ${SC_FILES}) ##${LIBHELLO_SRC})

target_link_libraries(final  ${TORCH_LIBRARIES} ${OpenCV_LIBS} ) 


if(NOT Torch_FOUND)
    message(FATAL_ERROR "Pytorch Not Found!")
endif(NOT Torch_FOUND)

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}")
`

[CMakeLists of perfectly worked executable one]

`cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
project(custom_ops)

set(Torch_DIR /home2/user/pk/libtorch/share/cmake/Torch)
set(OpenCV_DIR "/home2/user/pk/OpenCV/share/OpenCV")
find_package(Torch REQUIRED)  # find libtorch
find_package(OpenCV REQUIRED)  # find OpenCV
FILE(GLOB SC_FILES "*.cpp" "*.h" )
message(STATUS "src_libraries: ${SC_FILES}")
if(NOT Torch_FOUND)
    message(FATAL_ERROR "Pytorch Not Found!")
endif(NOT Torch_FOUND)
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}")
message(STATUS "   ------------------------: ${TORCH_LIBRARIES}")
add_executable(apitest ${SC_FILES})
target_link_libraries(apitest ${TORCH_LIBRARIES} ${OpenCV_LIBS})
set_property(TARGET apitest PROPERTY CXX_STANDARD 11)
`
Attempt:

(1) I printed what were fetched by find_package(Torch REQUIRED)

`Pytorch status:
--     libraries: torch;torch_library;/home2/user/pk/libtorch/lib/libc10.so;/home/user/cuda/cuda-10.1/lib64/stubs/libcuda.so;/home/user/cuda/cuda-10.1/lib64/libnvrtc.so;/home/user/cuda/cuda-10.1/lib64/libnvToolsExt.so;/home/user/cuda/cuda-10.1/lib64/libcudart.so;/home2/user/pk/libtorch/lib/libc10_cuda.so`


(2) All missed files could be found under the '/home2/user/pk/libtorch/lib', such as libcudart-1b201d85.so.10.1 and etc.

(3)Also "ldd libtorch_cpu.so", all dependence could be found.

(4)Adding "/home2/user/pk/libtorch/lib/libcudart-1b201d85.so.10.1" to target_link_libraries, such like "target_link_libraries(apitest ${TORCH_LIBRARIES} ${OpenCV_LIBS} /home2/user/pk/libtorch/lib/libcudart-1b201d85.so.10.1)" when makeing shared library DIDN'T work either.

(5)Adding "find_library(GOMP_LIBRARY gomp-7c85b1e2 PATHS "${TORCH_INSTALL_PREFIX}/lib")" in  "TorchConfig.cmake" ---> NOTFOUND

(6)Adding list(APPEND TORCH_LIBRARIES ${TORCH_INSTALL_PREFIX}/lib/libgomp-7c85b1e2.so.1) in "TorchConfig.cmake" ---> DON'T WORK