multiple definition of 'CGAL::Surface_mesh_parameterization::get_error_message(int)' - cgal

I am able to use CGAL surface mesh parametrization with given examples easily. Which means that CGAL is installed properly and is functional.
Ways to reproduce error:
Create a empty main file
Create a class file with appropriate modifications to CGAL square_border_parametrize.cpp
After I do cmake . and make this is the output
Scanning dependencies of target Main
[ 33%] Building CXX object CMakeFiles/Main.dir/SBP.cpp.o
:0:15: warning: missing whitespace after the macro name
:0:15: warning: missing whitespace after the macro name
[ 66%] Building CXX object CMakeFiles/Main.dir/main.cpp.o
:0:15: warning: missing whitespace after the macro name
:0:15: warning: missing whitespace after the macro name
[100%] Linking CXX executable Main
CMakeFiles/Main.dir/main.cpp.o: In function _mm_getcsr()':
/usr/local/include/CGAL/Interval_nt.h:202: multiple definition ofCGAL::Surface_mesh_parameterization::get_error_message(int)'
CMakeFiles/Main.dir/SBP.cpp.o:/usr/local/include/CGAL/Surface_mesh_parameterization/Error_code.h:53: first defined here
collect2: error: ld returned 1 exit status
CMakeFiles/Main.dir/build.make:450: recipe for target 'Main' failed
make[2]: * [Main] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/Main.dir/all' failed
make[1]: * [CMakeFiles/Main.dir/all] Error 2
Makefile:140: recipe for target 'all' failed
make: *** [all] Error 2
I am not able to find any multiple definition for CGAL::Surface_mesh_parameterization::get_error_message(int). My CMakeLists.txt file looks like this:
cmake_minimum_required (VERSION 2.8 FATAL_ERROR)
PROJECT(LSCM)
FIND_PACKAGE( OpenCV REQUIRED )
find_package( PCL 1.7 REQUIRED COMPONENTS common io visualization filters )
find_package( CGAL QUIET COMPONENTS )
if ( NOT CGAL_FOUND )
message(STATUS "This project requires the CGAL library, and will not be compiled.")
return()
endif()
include_directories(${PCL_INCLUDE_DIRS})
link_directories(${PCL_LIBRARY_DIRS})
add_definitions(${PCL_DEFINITIONS})
include( CGAL_CreateSingleSourceCGALProgram )
find_package( Boost REQUIRED )
if ( NOT Boost_FOUND )
message(STATUS "This project requires the Boost library, and will not be compiled.")
return()
endif()
SET(CMAKE_BUILD_TYPE "Debug")
SET(LIBRARY_OUTPUT_PATH "./lib")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-deprecated-declarations")
file(GLOB SOURCE_FILES ./*.cpp)
ADD_EXECUTABLE(Main ${SOURCE_FILES})
TARGET_LINK_LIBRARIES(Main ${OpenCV_LIBS} ${PCL_LIBRARIES} ${CGAL_LIBRARIES} ${CGAL_3RD_PARTY_LIBRARIES} -lCGAL -lm )
install (TARGETS Main DESTINATION ~/bin)

A quick fix: In the file include/CGAL/Surface_mesh_parameterization/Error_code.h write inline in front of const char* get_error_message(int error_code).
The fix is in this pull request, that is now merged in CGAL-4.12.

Related

cmake targets from find_package are not propagated to siblings [duplicate]

I have a set of libraries and their respective tests, and they are organized in the following fashion:
-Lib1
-Lib1_Test
-Lib2
-Lib2_Test
-Lib3
-Lib3_Test
....
and so on. some of these libs depend on others, for example Lib1 depends on Lib2, and Lib3 depends on Lib1. I can easily build each target separately and all is fine. However, When I try to build them in such a way that each target first builds its dependencies and then itself I face an issue here.
For this very purpose, I used 'add_subdirectory` for each dependenant target and I also check if the target is already built or not so only if its not, we include it and build it.
This is how one of my CMakeLists.txt look like :
cmake_minimum_required(VERSION 3.11)
project(FV)
set(CMAKE_CXX_STANDARD 17)
set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC" )
find_package(Torch REQUIRED)
find_package(OpenCV REQUIRED)
find_package(Protobuf REQUIRED)
if ("${TORCH_LIB_DIRS}" STREQUAL "")
set(TORCH_LIB_DIRS /home/rika/libtorch-cxx11-abi-shared-with-deps-1.6.0+cpu/libtorch/lib)
endif()
get_filename_component(PARENT_DIR ${PROJECT_SOURCE_DIR} DIRECTORY)
#if (DLL)
add_definitions(-D_FV_BUILD_DLL)
set(CMAKE_INSTALL_PREFIX ${PARENT_DIR}/built_stuff)
if(NOT TARGET AntiSpoofer)
add_subdirectory(${PARENT_DIR}/AntiSpoofer ${PARENT_DIR}/AntiSpoofer/build)
endif()
if(NOT TARGET Detector)
add_subdirectory(${PARENT_DIR}/Detector ${PARENT_DIR}/Detector/build)
endif()
if(NOT TARGET Hashing)
add_subdirectory(${PARENT_DIR}/Hashing ${PARENT_DIR}/Hashing/build)
endif()
include_directories(${PARENT_DIR}/Dependencies/include ${Protobuf_INCLUDE_DIRS} ${TORCH_INCLUDE_DIRS} ${OpenCV_INCLUDE_DIRS})
set(FV_H ${PARENT_DIR}/Dependencies/include/FV/FV.h)
set(Config_H ${PARENT_DIR}/Dependencies/include/messages/Config.pb.h)
set(TorchSerializer_H ${PARENT_DIR}/Dependencies/include/messages/TorchSerializer.pb.h)
set(TorchSerializer_PY ${PARENT_DIR}/Dependencies/include/messages/TorchSerializer_pb2.py)
set(FV_SRC ./FV.cpp)
set(Config_SRC ${PARENT_DIR}/Dependencies/include/messages/Config.pb.cc)
set(TorchSerializer_SRC ${PARENT_DIR}/Dependencies/include/messages/TorchSerializer.pb.cc)
LINK_DIRECTORIES(${TORCH_LIB_DIRS})
LINK_DIRECTORIES(${PARENT_DIR}/AntiSpoofer/build)
LINK_DIRECTORIES(${PARENT_DIR}/Blinker/build)
LINK_DIRECTORIES(${PARENT_DIR}/Detector/build)
LINK_DIRECTORIES(${PARENT_DIR}/Hashing/build)
add_library(
FV
SHARED
${FV_SRC}
${Config_SRC}
${TorchSerializer_SRC}
)
target_link_directories(FV PUBLIC ${PARENT_DIR}/built_stuff/lib)
# Link
target_link_libraries(FV ${OpenCV_LIBS})
target_link_libraries(FV ${TORCH_LIB_DIRS}/libc10.so)
target_link_libraries(FV ${TORCH_LIB_DIRS}/libtorch_cpu.so)
target_link_libraries(FV Hashing)
target_link_libraries(FV Blinker)
target_link_libraries(FV AntiSpoofer)
target_link_libraries(FV Detector)
# add FV include
install(TARGETS FV LIBRARY DESTINATION lib)
install(FILES ${FV_H} DESTINATION include/FV)
# add protobuf related headers/srcs for c++ and python
install(FILES ${Config_H} DESTINATION include/messages)
install(FILES ${TorchSerializer_H} DESTINATION include/messages)
install(FILES ${Config_SRC} DESTINATION src/messages)
install(FILES ${TorchSerializer_SRC} DESTINATION src/messages)
install(FILES ${TorchSerializer_PY} DESTINATION Python)
when I try to call this very cmakelists like this :
cmake -DCMAKE_PREFIX_PATH="/home/rika/libtorch-cxx11-abi-shared-with-deps-1.6.0+cpu/libtorch/share/cmake" ..
everything builds just fine and all targets seem to be able to get and use the passed CMAKE_PREFIX_PATH that I send here. However, just after this, if I cd into the FV_Test which by the way is given below, it fails with the error messages indicating the failure of CMake in finding torch! which is clearly specified in the argument when calling CMake. This is the error message I get after trying to build FV_Test:
-- The C compiler identification is GNU 9.3.0
-- The CXX compiler identification is GNU 9.3.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found OpenCV: /usr/local (found version "3.4.10")
-- Found Protobuf: /usr/local/lib/libprotobuf.so;-lpthread (found version "3.13.0")
-- Found Threads: TRUE
-- Found Torch: /home/rika/libtorch-cxx11-abi-shared-with-deps-1.6.0+cpu/libtorch/lib/libtorch.so
-- Using CMake version: 3.18.2
-- Compiling dlib version: 19.21.0
-- Found X11: /usr/include
-- Looking for XOpenDisplay in /usr/lib/x86_64-linux-gnu/libX11.so;/usr/lib/x86_64-linux-gnu/libXext.so
-- Looking for XOpenDisplay in /usr/lib/x86_64-linux-gnu/libX11.so;/usr/lib/x86_64-linux-gnu/libXext.so - found
-- Looking for gethostbyname
-- Looking for gethostbyname - found
-- Looking for connect
-- Looking for connect - found
-- Looking for remove
-- Looking for remove - found
-- Looking for shmat
-- Looking for shmat - found
-- Looking for IceConnectionNumber in ICE
-- Looking for IceConnectionNumber in ICE - found
-- Found system copy of libpng: /usr/lib/x86_64-linux-gnu/libpng.so;/usr/lib/x86_64-linux-gnu/libz.so
-- Found system copy of libjpeg: /usr/lib/x86_64-linux-gnu/libjpeg.so
-- Searching for BLAS and LAPACK
-- Searching for BLAS and LAPACK
-- Found PkgConfig: /usr/bin/pkg-config (found version "0.29.1")
-- Checking for module 'cblas'
-- No package 'cblas' found
-- Checking for module 'lapack'
-- Found lapack, version 3.10.3
-- Looking for sys/types.h
-- Looking for sys/types.h - found
-- Looking for stdint.h
-- Looking for stdint.h - found
-- Looking for stddef.h
-- Looking for stddef.h - found
-- Check size of void*
-- Check size of void* - done
-- Found Intel MKL BLAS/LAPACK library
-- Looking for sgesv
-- Looking for sgesv - found
-- Looking for sgesv_
-- Looking for sgesv_ - found
CUDA_TOOLKIT_ROOT_DIR not found or specified
-- Could NOT find CUDA (missing: CUDA_TOOLKIT_ROOT_DIR CUDA_NVCC_EXECUTABLE CUDA_INCLUDE_DIRS CUDA_CUDART_LIBRARY) (Required is at least version "7.5")
-- DID NOT FIND CUDA
-- Disabling CUDA support for dlib. DLIB WILL NOT USE CUDA
-- C++11 activated.
-- Configuring done
CMake Error at CMakeLists.txt:58 (target_link_libraries):
Error evaluating generator expression:
$<TARGET_PROPERTY:torch_cpu,INTERFACE_COMPILE_DEFINITIONS>
Target "torch_cpu" not found.
CMake Error at CMakeLists.txt:58 (target_link_libraries):
Error evaluating generator expression:
$<TARGET_PROPERTY:torch,INTERFACE_COMPILE_DEFINITIONS>
Target "torch" not found.
CMake Error at CMakeLists.txt:58 (target_link_libraries):
Error evaluating generator expression:
$<TARGET_PROPERTY:torch_cpu,INTERFACE_INCLUDE_DIRECTORIES>
Target "torch_cpu" not found.
CMake Error at CMakeLists.txt:58 (target_link_libraries):
Error evaluating generator expression:
$<TARGET_PROPERTY:torch,INTERFACE_INCLUDE_DIRECTORIES>
Target "torch" not found.
CMake Error:
Error evaluating generator expression:
$<TARGET_PROPERTY:torch_cpu,INTERFACE_SYSTEM_INCLUDE_DIRECTORIES>
Target "torch_cpu" not found.
CMake Error:
Error evaluating generator expression:
$<TARGET_PROPERTY:torch_cpu,INTERFACE_INCLUDE_DIRECTORIES>
Target "torch_cpu" not found.
CMake Error:
Error evaluating generator expression:
$<TARGET_PROPERTY:torch,INTERFACE_SYSTEM_INCLUDE_DIRECTORIES>
Target "torch" not found.
CMake Error:
Error evaluating generator expression:
$<TARGET_PROPERTY:torch,INTERFACE_INCLUDE_DIRECTORIES>
Target "torch" not found.
CMake Error at CMakeLists.txt:58 (target_link_libraries):
Error evaluating generator expression:
$<TARGET_PROPERTY:torch_cpu,INTERFACE_COMPILE_OPTIONS>
Target "torch_cpu" not found.
CMake Error at CMakeLists.txt:58 (target_link_libraries):
Error evaluating generator expression:
$<TARGET_PROPERTY:torch,INTERFACE_COMPILE_OPTIONS>
Target "torch" not found.
CMake Warning at CMakeLists.txt:49 (add_executable):
Cannot generate a safe runtime search path for target FV_test because files
in some directories may conflict with libraries in implicit directories:
runtime library [libpng16.so.16] in /usr/lib/x86_64-linux-gnu may be hidden by files in:
/home/rika/anaconda3/lib
runtime library [libz.so.1] in /usr/lib/x86_64-linux-gnu may be hidden by files in:
/home/rika/anaconda3/lib
Some of these libraries may not be found correctly.
CMake Error at CMakeLists.txt:58 (target_link_libraries):
Error evaluating generator expression:
$<TARGET_PROPERTY:torch_cpu,INTERFACE_COMPILE_DEFINITIONS>
Target "torch_cpu" not found.
CMake Error at CMakeLists.txt:58 (target_link_libraries):
Error evaluating generator expression:
$<TARGET_PROPERTY:torch,INTERFACE_COMPILE_DEFINITIONS>
Target "torch" not found.
CMake Error at CMakeLists.txt:58 (target_link_libraries):
Error evaluating generator expression:
$<TARGET_PROPERTY:torch_cpu,INTERFACE_INCLUDE_DIRECTORIES>
Target "torch_cpu" not found.
CMake Error at CMakeLists.txt:58 (target_link_libraries):
Error evaluating generator expression:
$<TARGET_PROPERTY:torch,INTERFACE_INCLUDE_DIRECTORIES>
Target "torch" not found.
CMake Warning at /home/rika/Desktop/LibtorchPort/FV/CMakeLists.txt:68 (add_library):
Cannot generate a safe runtime search path for target FV because files in
some directories may conflict with libraries in implicit directories:
runtime library [libpng16.so.16] in /usr/lib/x86_64-linux-gnu may be hidden by files in:
/home/rika/anaconda3/lib
runtime library [libz.so.1] in /usr/lib/x86_64-linux-gnu may be hidden by files in:
/home/rika/anaconda3/lib
Some of these libraries may not be found correctly.
CMake Warning at /home/rika/Desktop/LibtorchPort/AntiSpoofer/CMakeLists.txt:57 (add_library):
Cannot generate a safe runtime search path for target AntiSpoofer because
files in some directories may conflict with libraries in implicit
directories:
runtime library [libpng16.so.16] in /usr/lib/x86_64-linux-gnu may be hidden by files in:
/home/rika/anaconda3/lib
runtime library [libz.so.1] in /usr/lib/x86_64-linux-gnu may be hidden by files in:
/home/rika/anaconda3/lib
Some of these libraries may not be found correctly.
CMake Warning at /home/rika/Desktop/LibtorchPort/Blinker/CMakeLists.txt:46 (add_library):
Cannot generate a safe runtime search path for target Blinker because files
in some directories may conflict with libraries in implicit directories:
runtime library [libpng16.so.16] in /usr/lib/x86_64-linux-gnu may be hidden by files in:
/home/rika/anaconda3/lib
runtime library [libz.so.1] in /usr/lib/x86_64-linux-gnu may be hidden by files in:
/home/rika/anaconda3/lib
Some of these libraries may not be found correctly.
-- Generating done
CMake Generate step failed. Build files cannot be regenerated correctly.
And this is the CMakeLists.txt that I'm using for FV_Test.
cmake_minimum_required(VERSION 3.11)
project(FV_Test)
set(CMAKE_CXX_STANDARD 17)
find_package(OpenCV REQUIRED)
find_package(Protobuf REQUIRED)
if ("${TORCH_LIB_DIRS}" STREQUAL "")
set(TORCH_LIB_DIRS /home/rika/libtorch-cxx11-abi-shared-with-deps-1.6.0+cpu/libtorch/lib)
endif()
get_filename_component(PARENT_DIR ${PROJECT_SOURCE_DIR} DIRECTORY)
if(NOT TARGET FV)
add_subdirectory(${PARENT_DIR}/FV ${PARENT_DIR}/FV/build)
endif()
set(CMAKE_INSTALL_PREFIX ${PARENT_DIR}/built_stuff)
include_directories(${PARENT_DIR}/Dependencies/include ${OpenCV_INCLUDE_DIRS} ${Protobuf_INCLUDE_DIRS}) #
LINK_DIRECTORIES(${TORCH_LIB_DIRS})
LINK_DIRECTORIES(${PARENT_DIR}/AntiSpoofer/build)
LINK_DIRECTORIES(${PARENT_DIR}/Blinker/build)
LINK_DIRECTORIES(${PARENT_DIR}/FV/build)
add_executable(FV_test ./FV_Test.cpp )
target_link_directories(FV_test PUBLIC ${PARENT_DIR}/AntiSpoofer/build)
target_link_directories(FV_test PUBLIC ${PARENT_DIR}/Blinker/build)
target_link_directories(FV_test PUBLIC ${PARENT_DIR}/FV/build)
# Link
target_link_libraries(FV_test ${OpenCV_LIBS})
target_link_libraries(FV_test ${Protobuf_LIBRARIES})
target_link_libraries(FV_test FV)
target_link_libraries(FV_test Blinker)
install(TARGETS FV_test DESTINATION bin)
Why am I seeing this behavior and how can I solve this?
If the problem is really depicted in the following comment:
As far as I understand, the core of the problem is: 1. In subdirectory use find_package(Torch REQUIRED) and then link some library with a Torch library via target_link_libraries(mylib ${TORCH_LIBRARIES}). 2. Linking with that library in the outer directory: target_link_libraries(myexe mylib). For some (unknown) reason, during the last link CMake tries to evaluate generator expressions for torch_cpu. But this is an IMPORTED target, which is created in the subdirectory, so it cannot be accessed from the outer directory. – Tsyvarev Sep 27 '20 at 8:15
I ran into the same problem with Pytorch. The fix that worked for me was to link the torch libraries privately: target_link_libraries(mylib PRIVATE ${TORCH_LIBRARIES}").
Now don't ask my why that works I wouldn't be able to answer that.

CMake cant find the external libraries when subdirectory is used to build dependencies for each target

I have a set of libraries and their respective tests, and they are organized in the following fashion:
-Lib1
-Lib1_Test
-Lib2
-Lib2_Test
-Lib3
-Lib3_Test
....
and so on. some of these libs depend on others, for example Lib1 depends on Lib2, and Lib3 depends on Lib1. I can easily build each target separately and all is fine. However, When I try to build them in such a way that each target first builds its dependencies and then itself I face an issue here.
For this very purpose, I used 'add_subdirectory` for each dependenant target and I also check if the target is already built or not so only if its not, we include it and build it.
This is how one of my CMakeLists.txt look like :
cmake_minimum_required(VERSION 3.11)
project(FV)
set(CMAKE_CXX_STANDARD 17)
set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC" )
find_package(Torch REQUIRED)
find_package(OpenCV REQUIRED)
find_package(Protobuf REQUIRED)
if ("${TORCH_LIB_DIRS}" STREQUAL "")
set(TORCH_LIB_DIRS /home/rika/libtorch-cxx11-abi-shared-with-deps-1.6.0+cpu/libtorch/lib)
endif()
get_filename_component(PARENT_DIR ${PROJECT_SOURCE_DIR} DIRECTORY)
#if (DLL)
add_definitions(-D_FV_BUILD_DLL)
set(CMAKE_INSTALL_PREFIX ${PARENT_DIR}/built_stuff)
if(NOT TARGET AntiSpoofer)
add_subdirectory(${PARENT_DIR}/AntiSpoofer ${PARENT_DIR}/AntiSpoofer/build)
endif()
if(NOT TARGET Detector)
add_subdirectory(${PARENT_DIR}/Detector ${PARENT_DIR}/Detector/build)
endif()
if(NOT TARGET Hashing)
add_subdirectory(${PARENT_DIR}/Hashing ${PARENT_DIR}/Hashing/build)
endif()
include_directories(${PARENT_DIR}/Dependencies/include ${Protobuf_INCLUDE_DIRS} ${TORCH_INCLUDE_DIRS} ${OpenCV_INCLUDE_DIRS})
set(FV_H ${PARENT_DIR}/Dependencies/include/FV/FV.h)
set(Config_H ${PARENT_DIR}/Dependencies/include/messages/Config.pb.h)
set(TorchSerializer_H ${PARENT_DIR}/Dependencies/include/messages/TorchSerializer.pb.h)
set(TorchSerializer_PY ${PARENT_DIR}/Dependencies/include/messages/TorchSerializer_pb2.py)
set(FV_SRC ./FV.cpp)
set(Config_SRC ${PARENT_DIR}/Dependencies/include/messages/Config.pb.cc)
set(TorchSerializer_SRC ${PARENT_DIR}/Dependencies/include/messages/TorchSerializer.pb.cc)
LINK_DIRECTORIES(${TORCH_LIB_DIRS})
LINK_DIRECTORIES(${PARENT_DIR}/AntiSpoofer/build)
LINK_DIRECTORIES(${PARENT_DIR}/Blinker/build)
LINK_DIRECTORIES(${PARENT_DIR}/Detector/build)
LINK_DIRECTORIES(${PARENT_DIR}/Hashing/build)
add_library(
FV
SHARED
${FV_SRC}
${Config_SRC}
${TorchSerializer_SRC}
)
target_link_directories(FV PUBLIC ${PARENT_DIR}/built_stuff/lib)
# Link
target_link_libraries(FV ${OpenCV_LIBS})
target_link_libraries(FV ${TORCH_LIB_DIRS}/libc10.so)
target_link_libraries(FV ${TORCH_LIB_DIRS}/libtorch_cpu.so)
target_link_libraries(FV Hashing)
target_link_libraries(FV Blinker)
target_link_libraries(FV AntiSpoofer)
target_link_libraries(FV Detector)
# add FV include
install(TARGETS FV LIBRARY DESTINATION lib)
install(FILES ${FV_H} DESTINATION include/FV)
# add protobuf related headers/srcs for c++ and python
install(FILES ${Config_H} DESTINATION include/messages)
install(FILES ${TorchSerializer_H} DESTINATION include/messages)
install(FILES ${Config_SRC} DESTINATION src/messages)
install(FILES ${TorchSerializer_SRC} DESTINATION src/messages)
install(FILES ${TorchSerializer_PY} DESTINATION Python)
when I try to call this very cmakelists like this :
cmake -DCMAKE_PREFIX_PATH="/home/rika/libtorch-cxx11-abi-shared-with-deps-1.6.0+cpu/libtorch/share/cmake" ..
everything builds just fine and all targets seem to be able to get and use the passed CMAKE_PREFIX_PATH that I send here. However, just after this, if I cd into the FV_Test which by the way is given below, it fails with the error messages indicating the failure of CMake in finding torch! which is clearly specified in the argument when calling CMake. This is the error message I get after trying to build FV_Test:
-- The C compiler identification is GNU 9.3.0
-- The CXX compiler identification is GNU 9.3.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found OpenCV: /usr/local (found version "3.4.10")
-- Found Protobuf: /usr/local/lib/libprotobuf.so;-lpthread (found version "3.13.0")
-- Found Threads: TRUE
-- Found Torch: /home/rika/libtorch-cxx11-abi-shared-with-deps-1.6.0+cpu/libtorch/lib/libtorch.so
-- Using CMake version: 3.18.2
-- Compiling dlib version: 19.21.0
-- Found X11: /usr/include
-- Looking for XOpenDisplay in /usr/lib/x86_64-linux-gnu/libX11.so;/usr/lib/x86_64-linux-gnu/libXext.so
-- Looking for XOpenDisplay in /usr/lib/x86_64-linux-gnu/libX11.so;/usr/lib/x86_64-linux-gnu/libXext.so - found
-- Looking for gethostbyname
-- Looking for gethostbyname - found
-- Looking for connect
-- Looking for connect - found
-- Looking for remove
-- Looking for remove - found
-- Looking for shmat
-- Looking for shmat - found
-- Looking for IceConnectionNumber in ICE
-- Looking for IceConnectionNumber in ICE - found
-- Found system copy of libpng: /usr/lib/x86_64-linux-gnu/libpng.so;/usr/lib/x86_64-linux-gnu/libz.so
-- Found system copy of libjpeg: /usr/lib/x86_64-linux-gnu/libjpeg.so
-- Searching for BLAS and LAPACK
-- Searching for BLAS and LAPACK
-- Found PkgConfig: /usr/bin/pkg-config (found version "0.29.1")
-- Checking for module 'cblas'
-- No package 'cblas' found
-- Checking for module 'lapack'
-- Found lapack, version 3.10.3
-- Looking for sys/types.h
-- Looking for sys/types.h - found
-- Looking for stdint.h
-- Looking for stdint.h - found
-- Looking for stddef.h
-- Looking for stddef.h - found
-- Check size of void*
-- Check size of void* - done
-- Found Intel MKL BLAS/LAPACK library
-- Looking for sgesv
-- Looking for sgesv - found
-- Looking for sgesv_
-- Looking for sgesv_ - found
CUDA_TOOLKIT_ROOT_DIR not found or specified
-- Could NOT find CUDA (missing: CUDA_TOOLKIT_ROOT_DIR CUDA_NVCC_EXECUTABLE CUDA_INCLUDE_DIRS CUDA_CUDART_LIBRARY) (Required is at least version "7.5")
-- DID NOT FIND CUDA
-- Disabling CUDA support for dlib. DLIB WILL NOT USE CUDA
-- C++11 activated.
-- Configuring done
CMake Error at CMakeLists.txt:58 (target_link_libraries):
Error evaluating generator expression:
$<TARGET_PROPERTY:torch_cpu,INTERFACE_COMPILE_DEFINITIONS>
Target "torch_cpu" not found.
CMake Error at CMakeLists.txt:58 (target_link_libraries):
Error evaluating generator expression:
$<TARGET_PROPERTY:torch,INTERFACE_COMPILE_DEFINITIONS>
Target "torch" not found.
CMake Error at CMakeLists.txt:58 (target_link_libraries):
Error evaluating generator expression:
$<TARGET_PROPERTY:torch_cpu,INTERFACE_INCLUDE_DIRECTORIES>
Target "torch_cpu" not found.
CMake Error at CMakeLists.txt:58 (target_link_libraries):
Error evaluating generator expression:
$<TARGET_PROPERTY:torch,INTERFACE_INCLUDE_DIRECTORIES>
Target "torch" not found.
CMake Error:
Error evaluating generator expression:
$<TARGET_PROPERTY:torch_cpu,INTERFACE_SYSTEM_INCLUDE_DIRECTORIES>
Target "torch_cpu" not found.
CMake Error:
Error evaluating generator expression:
$<TARGET_PROPERTY:torch_cpu,INTERFACE_INCLUDE_DIRECTORIES>
Target "torch_cpu" not found.
CMake Error:
Error evaluating generator expression:
$<TARGET_PROPERTY:torch,INTERFACE_SYSTEM_INCLUDE_DIRECTORIES>
Target "torch" not found.
CMake Error:
Error evaluating generator expression:
$<TARGET_PROPERTY:torch,INTERFACE_INCLUDE_DIRECTORIES>
Target "torch" not found.
CMake Error at CMakeLists.txt:58 (target_link_libraries):
Error evaluating generator expression:
$<TARGET_PROPERTY:torch_cpu,INTERFACE_COMPILE_OPTIONS>
Target "torch_cpu" not found.
CMake Error at CMakeLists.txt:58 (target_link_libraries):
Error evaluating generator expression:
$<TARGET_PROPERTY:torch,INTERFACE_COMPILE_OPTIONS>
Target "torch" not found.
CMake Warning at CMakeLists.txt:49 (add_executable):
Cannot generate a safe runtime search path for target FV_test because files
in some directories may conflict with libraries in implicit directories:
runtime library [libpng16.so.16] in /usr/lib/x86_64-linux-gnu may be hidden by files in:
/home/rika/anaconda3/lib
runtime library [libz.so.1] in /usr/lib/x86_64-linux-gnu may be hidden by files in:
/home/rika/anaconda3/lib
Some of these libraries may not be found correctly.
CMake Error at CMakeLists.txt:58 (target_link_libraries):
Error evaluating generator expression:
$<TARGET_PROPERTY:torch_cpu,INTERFACE_COMPILE_DEFINITIONS>
Target "torch_cpu" not found.
CMake Error at CMakeLists.txt:58 (target_link_libraries):
Error evaluating generator expression:
$<TARGET_PROPERTY:torch,INTERFACE_COMPILE_DEFINITIONS>
Target "torch" not found.
CMake Error at CMakeLists.txt:58 (target_link_libraries):
Error evaluating generator expression:
$<TARGET_PROPERTY:torch_cpu,INTERFACE_INCLUDE_DIRECTORIES>
Target "torch_cpu" not found.
CMake Error at CMakeLists.txt:58 (target_link_libraries):
Error evaluating generator expression:
$<TARGET_PROPERTY:torch,INTERFACE_INCLUDE_DIRECTORIES>
Target "torch" not found.
CMake Warning at /home/rika/Desktop/LibtorchPort/FV/CMakeLists.txt:68 (add_library):
Cannot generate a safe runtime search path for target FV because files in
some directories may conflict with libraries in implicit directories:
runtime library [libpng16.so.16] in /usr/lib/x86_64-linux-gnu may be hidden by files in:
/home/rika/anaconda3/lib
runtime library [libz.so.1] in /usr/lib/x86_64-linux-gnu may be hidden by files in:
/home/rika/anaconda3/lib
Some of these libraries may not be found correctly.
CMake Warning at /home/rika/Desktop/LibtorchPort/AntiSpoofer/CMakeLists.txt:57 (add_library):
Cannot generate a safe runtime search path for target AntiSpoofer because
files in some directories may conflict with libraries in implicit
directories:
runtime library [libpng16.so.16] in /usr/lib/x86_64-linux-gnu may be hidden by files in:
/home/rika/anaconda3/lib
runtime library [libz.so.1] in /usr/lib/x86_64-linux-gnu may be hidden by files in:
/home/rika/anaconda3/lib
Some of these libraries may not be found correctly.
CMake Warning at /home/rika/Desktop/LibtorchPort/Blinker/CMakeLists.txt:46 (add_library):
Cannot generate a safe runtime search path for target Blinker because files
in some directories may conflict with libraries in implicit directories:
runtime library [libpng16.so.16] in /usr/lib/x86_64-linux-gnu may be hidden by files in:
/home/rika/anaconda3/lib
runtime library [libz.so.1] in /usr/lib/x86_64-linux-gnu may be hidden by files in:
/home/rika/anaconda3/lib
Some of these libraries may not be found correctly.
-- Generating done
CMake Generate step failed. Build files cannot be regenerated correctly.
And this is the CMakeLists.txt that I'm using for FV_Test.
cmake_minimum_required(VERSION 3.11)
project(FV_Test)
set(CMAKE_CXX_STANDARD 17)
find_package(OpenCV REQUIRED)
find_package(Protobuf REQUIRED)
if ("${TORCH_LIB_DIRS}" STREQUAL "")
set(TORCH_LIB_DIRS /home/rika/libtorch-cxx11-abi-shared-with-deps-1.6.0+cpu/libtorch/lib)
endif()
get_filename_component(PARENT_DIR ${PROJECT_SOURCE_DIR} DIRECTORY)
if(NOT TARGET FV)
add_subdirectory(${PARENT_DIR}/FV ${PARENT_DIR}/FV/build)
endif()
set(CMAKE_INSTALL_PREFIX ${PARENT_DIR}/built_stuff)
include_directories(${PARENT_DIR}/Dependencies/include ${OpenCV_INCLUDE_DIRS} ${Protobuf_INCLUDE_DIRS}) #
LINK_DIRECTORIES(${TORCH_LIB_DIRS})
LINK_DIRECTORIES(${PARENT_DIR}/AntiSpoofer/build)
LINK_DIRECTORIES(${PARENT_DIR}/Blinker/build)
LINK_DIRECTORIES(${PARENT_DIR}/FV/build)
add_executable(FV_test ./FV_Test.cpp )
target_link_directories(FV_test PUBLIC ${PARENT_DIR}/AntiSpoofer/build)
target_link_directories(FV_test PUBLIC ${PARENT_DIR}/Blinker/build)
target_link_directories(FV_test PUBLIC ${PARENT_DIR}/FV/build)
# Link
target_link_libraries(FV_test ${OpenCV_LIBS})
target_link_libraries(FV_test ${Protobuf_LIBRARIES})
target_link_libraries(FV_test FV)
target_link_libraries(FV_test Blinker)
install(TARGETS FV_test DESTINATION bin)
Why am I seeing this behavior and how can I solve this?
If the problem is really depicted in the following comment:
As far as I understand, the core of the problem is: 1. In subdirectory use find_package(Torch REQUIRED) and then link some library with a Torch library via target_link_libraries(mylib ${TORCH_LIBRARIES}). 2. Linking with that library in the outer directory: target_link_libraries(myexe mylib). For some (unknown) reason, during the last link CMake tries to evaluate generator expressions for torch_cpu. But this is an IMPORTED target, which is created in the subdirectory, so it cannot be accessed from the outer directory. – Tsyvarev Sep 27 '20 at 8:15
I ran into the same problem with Pytorch. The fix that worked for me was to link the torch libraries privately: target_link_libraries(mylib PRIVATE ${TORCH_LIBRARIES}").
Now don't ask my why that works I wouldn't be able to answer that.

How to add Ziplib library in Clion on Ubuntu

I'm trying to add ZipLip into my project using Clion on ubuntu, but I have this output:
====================[ Build | TryZip | Debug ]==================================
/home/david/Snap/clion-2019.2.4/bin/cmake/linux/bin/cmake --build
/home/david/CLionProjects/TryZip/cmake-build-debug --target TryZip -- -j 2
[ 13%] Built target bzip2
[ 31%] Built target zlib
[ 83%] Built target lzma
[ 95%] Built target ZipLib
Scanning dependencies of target TryZip
[ 97%] Linking CXX executable ../bin/TryZip
/usr/bin/ld: cannot find -lExternalLibrary/ZipLib
collect2: error: ld returned 1 exit status
CMakeFiles/TryZip.dir/build.make:102: recipe for target '../bin/TryZip' failed
make[3]: *** [../bin/TryZip] Error 1
CMakeFiles/Makefile2:109: recipe for target 'CMakeFiles/TryZip.dir/all' failed
make[2]: *** [CMakeFiles/TryZip.dir/all] Error 2
CMakeFiles/Makefile2:116: recipe for target 'CMakeFiles/TryZip.dir/rule' failed
make[1]: *** [CMakeFiles/TryZip.dir/rule] Error 2
Makefile:131: recipe for target 'TryZip' failed
make: *** [TryZip] Error 2
This is my Cmakefile.txt
cmake_minimum_required(VERSION 3.15)
project(TryZip)
if(BOOST_FILESYSTEM)
include_directories(${BOOST_INCLUDE_DIR})
link_directories(${BOOST_LIB_DIR})
add_definitions(-DUSE_BOOST_FILESYSTEM)
else()
if(MSVC)
add_definitions(-DFILESYSTEM_EXPERIMENTAL)
endif()
endif()
if(BOOST_FILESYSTEM)
if(UNIX)
find_package(Boost COMPONENTS system filesystem REQUIRED)
target_link_libraries(${Boost_FILESYSTEM_LIBRARY}
${Boost_SYSTEM_LIBRARY})
endif()
endif()
add_subdirectory(ExternalLibrary/ZipLib)
link_libraries(ExternalLibrary/ZipLib)
include_directories(ExternalLibrary/ZipLib)
set(CMAKE_CXX_STANDARD 17)
add_executable(TryZip main.cpp ExternalLibrary/ZipLib/ZipFile.cpp)
target_link_libraries(TryZip ZipLib)
Can someone help me to solve this please?
My ZipLib folder is in the same folder as my cmakefile.txt file.
The call to link_libraries() appears to accept the wrong arguments in this case. The link_libraries() command takes arguments of existing CMake targets, or library names. It is also redundant with your target_link_libraries() call, as this already links ZipLib to TryZip.
Try removing the call to link_libraries(), as this CMake function is deprecated and its use is highly discouraged. The include_directories() call is similarly deprecated, in favor of the target-specific command, so consider using target_include_directories() instead.
Assuming your added sub-directory ExternalLibrary/ZipLib contains an additional CMakeLists.txt file for configuring the ZipLib target, you should not need to add the ZipFile.cpp file again. If this file is already compiled in the sub-directory into the target ZipLib, you do not need to compile it again into TryZip.
add_subdirectory(ExternalLibrary/ZipLib)
set(CMAKE_CXX_STANDARD 17)
add_executable(TryZip main.cpp)
target_include_directories(TryZip PRIVATE ExternalLibrary/ZipLib)
target_link_libraries(TryZip PRIVATE ZipLib)
EDIT: Based on your feedback, it appears ZipLib also depends on pthread but somehow it is not getting linked correctly. You might try to add the following to your ExternalLibrary/ZipLib/CMakeLists.txt file (if it doesn't already exist), to utilize CMake's FindThreads module:
find_package(Threads REQUIRED)
...
target_link_libraries(ZipLib PUBLIC Threads::Threads)

cmake on windows. Link libraries in folder

Disclaimer: I'm new to cmake so i have no idea what I'm doing.
All guides and tutorials i find seem to think I'm running a 20 man team that needs to work together.
All I'm trying to do is put all my libraries in an include folder and lib folder.
In visual studio and code::blocks i just set up the linker in the IDE, but cmake is a little hard for me to wrap my head around.
I've set include and link directories with
include_directories(${PROJECT_SOURCE_DIR}/include)
link_directories(${PROJECT_SOURCE_DIR}/lib)
Then i try to
target_link_libraries(${PROJECT_NAME} jsoncpp)
This however produces undefined references. Is there a simple way to add and link libraries without me having to google and add a cmake module for each one?
Edit:
attempts:
target_link_libraries(${PROJECT_NAME} libjsoncpp.a)
error: undefined reference
target_link_libraries(${PROJECT_NAME} libjsoncpp)
error: cannot find -llibjsoncpp
target_link_libraries(${PROJECT_NAME} jsoncpp)
error: undefined reference
target_link_libraries(${PROJECT_NAME} C:/Repos/prosjekt-bad-racoon/lib/libjsoncpp.a)
error: undefined reference
target_link_libraries(${PROJECT_NAME} ${PROJECT_SOURCE_DIR}/lib/libjsoncpp.a)
error: undefined reference
Edit:
Full cmake file
cmake_minimum_required(VERSION 3.8)
project(prosjekt_bad_racoon)
set(CMAKE_CXX_STANDARD 11)
include_directories(${PROJECT_SOURCE_DIR}/include)
link_directories(${PROJECT_SOURCE_DIR}/lib)
set(SOURCE_FILES main.cpp Core.cpp Core.h Collider.cpp Collider.h Player.cpp Player.h Texture.cpp Texture.h AudioController.cpp AudioController.h)
add_executable(prosjekt_bad_racoon ${SOURCE_FILES})
set(EXECUTABLE_NAME ${PROJECT_NAME})
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake_modules" ${CMAKE_MODULE_PATH})
target_link_libraries(${PROJECT_NAME} libjsoncpp.a)
find_package(SFML 2 REQUIRED system window graphics network audio)
if(SFML_FOUND)
include_directories(${SFML_INCLUDE_DIR})
target_link_libraries(${EXECUTABLE_NAME} ${SFML_LIBRARIES})
link_directories("${PROJECT_SOURCE_DIR}")
endif()
"C:\Program Files\JetBrains\CLion 2017.2.2\bin\cmake\bin\cmake.exe" --build C:\Repos\prosjekt-bad-racoon\cmake-build-debug --target prosjekt_bad_racoon -- -j 2
[ 14%] Linking CXX executable prosjekt_bad_racoon.exe
CMakeFiles\prosjekt_bad_racoon.dir/objects.a(Core.cpp.obj): In function `ZN4CoreC2Ev':
C:/Repos/prosjekt-bad-racoon/Core.cpp:3: undefined reference to `Json::Value::Value(char const*)'
CMakeFiles\prosjekt_bad_racoon.dir/objects.a(Core.cpp.obj): In function `ZN4CoreD2Ev':
C:/Repos/prosjekt-bad-racoon/Core.cpp:8: undefined reference to `Json::Value::~Value()'
collect2.exe: error: ld returned 1 exit status
CMakeFiles\prosjekt_bad_racoon.dir\build.make:236: recipe for target 'prosjekt_bad_racoon.exe' failed
mingw32-make.exe[3]: *** [prosjekt_bad_racoon.exe] Error 1
CMakeFiles\Makefile2:66: recipe for target 'CMakeFiles/prosjekt_bad_racoon.dir/all' failed
mingw32-make.exe[2]: *** [CMakeFiles/prosjekt_bad_racoon.dir/all] Error 2
CMakeFiles\Makefile2:78: recipe for target 'CMakeFiles/prosjekt_bad_racoon.dir/rule' failed
mingw32-make.exe[1]: *** [CMakeFiles/prosjekt_bad_racoon.dir/rule] Error 2
Makefile:117: recipe for target 'prosjekt_bad_racoon' failed
mingw32-make.exe: *** [prosjekt_bad_racoon] Error 2

CMake, shared library linking fail

I'm currently getting used to cmake and I'm trying to compile a small project with a .so library linking.
My project is the following.
/
CMakeLists.txt
inc/
Als.h
src/
main.c
CMakeLists.txt
lib/
libals.so
build/
I'm compiling from the build directory with:
$ cmake ..
-- DIR:
-- Configuring done
-- Generating done
-- Build files have been written to: /home/julien/tmp/cmakeTest/build
And then:
$ make
Linking C executable cmakeTest
/usr/bin/ld: ne peut trouver -lals
collect2: error: ld returned 1 exit status
src/CMakeFiles/cmakeTest.dir/build.make:85: recipe for target 'src/cmakeTest' failed
make[2]: *** [src/cmakeTest] Error 1
CMakeFiles/Makefile2:75: recipe for target 'src/CMakeFiles/cmakeTest.dir/all' failed
make[1]: *** [src/CMakeFiles/cmakeTest.dir/all] Error 2
Makefile:76: recipe for target 'all' failed
make: *** [all] Error 2
The linker seems to be unable to find the libals.so file.
Here is the file /CMakeLists.txt:
cmake_minimum_required(VERSION 2.8)
PROJECT(cmaketest)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O3")
INCLUDE_DIRECTORIES(
inc
)
ADD_SUBDIRECTORY(src)
get_directory_property(OUT_VAR LINK_DIRECTORIES)
message(STATUS "DIR: ${OUT_VAR}")
And here is the file /src/CMakeLists.txt:
PROJECT(cmakeTest)
FILE(
GLOB
${PROJECT_NAME}_Sources
*.c
)
INCLUDE_DIRECTORIES(
inc
inc
)
ADD_EXECUTABLE(
${PROJECT_NAME}
${${PROJECT_NAME}_Sources}
)
LINK_DIRECTORIES(
/home/julien/tmp/cmakeTest/lib/
)
TARGET_LINK_LIBRARIES(
${PROJECT_NAME}
als
pthread
)
Maybe I missed something but if I change the /src/CMakeLists.txt to:
PROJECT(cmakeTest)
FILE(
GLOB
${PROJECT_NAME}_Sources
*.c
)
INCLUDE_DIRECTORIES(
inc
inc
)
ADD_EXECUTABLE(
${PROJECT_NAME}
${${PROJECT_NAME}_Sources}
)
TARGET_LINK_LIBRARIES(
${PROJECT_NAME}
/home/julien/tmp/cmakeTest/lib/libals.so
pthread
)
The compilation is ok. Does someone know why the linker is unable to find libals.so when I'm giving him the good directory path to look in?
The LINK_DIRECTORIES functions seems not to be working.
Besides the solution you already have, and my solution in a comment, the problem you have with the CMake file shown is the order in which you invoke the CMake commands.
From the link_directories command reference:
The command will apply only to targets created after it is called.
[Emphasis mine]
You simply need to call link_directories before you call add_executable.