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

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.

Related

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.

Issues with linking GLFW to the executable with CMake

I am currently trying to link the GLFW library with my executable files in CLion. After hours of research and trial & error, I am stuck. I am using find_package() to locate my GLFW library. For some reason my FindGLFW.cmake file seems to correctly locate the library in /usr/local/lib/libglfw.a when using find_library(), but my CMakeLists.txt returns empty with find_package(). I don't really understand how that's possible. Could someone help me out?
CMakeLists.txt:
cmake_minimum_required(VERSION 3.15)
project(learnopengl)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/CMake)
find_package(glfw 3.3 REQUIRED)
message(STATUS "(CMakeLists.txt) GLFW: [${glfw}]")
if (NOT glfw)
message(SEND_ERROR "(CMakeLists) Did not find glfw_library")
endif()
add_executable(learnopengl src/main.cpp)
target_link_libraries(learnopengl glfw)
FindGLFW.cmake
find_library( GLFW_LIBRARY
NAMES
glfw3 glfw
PATHS
${GLFW_LOCATION}/lib
$ENV{GLFW_LOCATION}/lib
/usr/lib64
/usr/lib
/usr/local/lib64
/usr/local/lib
NO_DEFAULT_PATH
DOC "The GLFW library"
)
message(STATUS "(FindGLFW.cmake) GLFW_INCLUDE_DIR: [${GLFW_INCLUDE_DIR}]")
message(STATUS "(FindGLFW.cmake) GLFW_LIB: [${GLFW_LIBRARY}]")
if (NOT GLFW_LIBRARY)
message(SEND_ERROR "(FindGLFW.cmake) Did not find glfw_library")
Output when running cmake ..
-- (FindGLFW.cmake) GLFW_INCLUDE_DIR: [/usr/local/include]
-- (FindGLFW.cmake) GLFW_LIB: [/usr/local/lib/libglfw3.a]
-- (CMakeLists.txt) GLFW: []
CMake Error at CMakeLists.txt:12 (message):
(CMakeLists) Did not find glfw_library
When I tried to directly include find_library() in my CMakeLists.txt instead, it did locate the library, but when I tried to link it with my executable, I got a linker error saying it couldn't locate the library:
CMakeLists.txt
cmake_minimum_required(VERSION 3.15)
project(learnopengl)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/CMake)
find_library( GLFW_LIBRARY
NAMES
glfw3 glfw
PATHS
${GLFW_LOCATION}/lib
$ENV{GLFW_LOCATION}/lib
/usr/lib64
/usr/lib
/usr/local/lib64
/usr/local/lib
NO_DEFAULT_PATH
DOC "The GLFW library"
)
message(STATUS "(FindGLFW.cmake) GLFW_LIB: [${GLFW_LIBRARY}]")
if (NOT GLFW_LIBRARY)
message(SEND_ERROR "(CMakeLists.txt) Did not find glfw_library")
endif()
add_executable(learnopengl src/main.cpp)
target_link_libraries(learnopengl GLFW_LIBRARY)
Output:
~/dev/projects/opengl/learnopengl/build  cmake ..
-- (CMakeLists.txt) GLFW_LIB: [/usr/local/lib/libglfw3.a]
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/Philipp/dev/projects/opengl/learnopengl/build
~/dev/projects/opengl/learnopengl/build  cmake --build .
[ 50%] Linking CXX executable learnopengl
ld: library not found for -lGLFW_LIBRARY
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [learnopengl] Error 1
make[1]: *** [CMakeFiles/learnopengl.dir/all] Error 2
make: *** [all] Error 2
I would really appreciate some hints as to what I am doing wrong. I can't see why it wouldn't locate/link the library correctly.
Thank you!
In cmake, arguments are case sensitive, so find_package should be
find_package(GLFW 3.3 REQUIRED)
Linker problem can be found easily in the logs. You are trying to link -lGLFW_LIBRARY, however what you want to link is GLFW_LIBRARY's value, /usr/local/lib/libglfw3.a .
target_link_libraries(learnopengl ${GLFW_LIBRARY})

Building FLANN with Cmake fails

I am trying to build FLANN libraries in order to build PCL library afterwards.
I get an error while using CMake to compile the source.
I guess I am missing something very basic. Since I can't find a compiled library for my system I have to build it myself.
With the command
~/flann-1.8.4-src/build> cmake ..
I get
CMake Error at src/cpp/CMakeLists.txt:86 (add_library):
No SOURCES given to target: flann
CMake Error at src/cpp/CMakeLists.txt:32 (add_library):
No SOURCES given to target: flann_cpp
This happens with flann 1.8.4 and 1.9.1 on a SLES11 operating system.
Any hint?
Here a complete transcription of what cmake says:
-- Could NOT find HDF5 (missing: HDF5_LIBRARIES HDF5_INCLUDE_DIRS) (found version "")
CMake Warning at CMakeLists.txt:76 (message):
hdf5 library not found, some tests will not be run
-- Could NOT find GTest (missing: GTEST_LIBRARY GTEST_INCLUDE_DIR GTEST_MAIN_LIBRARY)
CMake Warning at CMakeLists.txt:115 (message):
gtest library not found, some tests will not be run
-- Found OpenMP_C: -fopenmp
-- Found OpenMP_CXX: -fopenmp
-- Found OpenMP: TRUE
CMake Warning at src/matlab/CMakeLists.txt:79 (message):
Cannot find MATLAB or Octave instalation. Make sure that the 'bin'
directory from the MATLAB instalation or that mkoctfile is in PATH
hdf5 library not found, not compiling flann_example.cpp
-- Could NOT find LATEX (missing: LATEX_COMPILER)
-- Install prefix: /usr/local
-- Build type: RelWithDebInfo
-- Building C bindings: ON
-- Building python bindings: ON
-- Building matlab bindings: ON
-- Building CUDA library: OFF
-- Using OpenMP support: ON
-- Using MPI support: OFF
-- Configuring done
CMake Error at src/cpp/CMakeLists.txt:86 (add_library):
No SOURCES given to target: flann
CMake Error at src/cpp/CMakeLists.txt:32 (add_library):
No SOURCES given to target: flann_cpp
-- Build files have been written to: ~/flann-1.8.4-src/build
In your flann dir
run
touch src/cpp/empty.cpp
In src/cpp/CMakeLists.txt
replace
add_library(flann_cpp SHARED "") and add_library(flann SHARED "")
with
add_library(flann_cpp SHARED empty.cpp) and add_library(flann SHARED empty.cpp)
Hope this helps :)
link, thanks to the comment by Tsyvarev

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

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.

Can CMake FindBLAS find OpenBLAS?

For demonstration, I use the 3-line CMakeLists.txt:
cmake_minimum_required(VERSION 2.8)
find_package( BLAS REQUIRED )
message( STATUS BLAS found: ${BLAS_LIBRARIES} )
I have cblas, ATLAS and OpenBLAS including developer packages installed on a Debian Linux system, plus CMake 2.8.9. When I call
cmake . -DBLA_VENDOR=ATLAS -DCMAKE_PREFIX_PATH=/usr/lib/atlas-base
the ATLAS library nicely appears found:
-- The C compiler identification is GNU 4.7.2
-- The CXX compiler identification is GNU 4.7.2
-- Check for working C compiler: /usr/bin/gcc
-- Check for working C compiler: /usr/bin/gcc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Looking for dgemm_
-- Looking for dgemm_ - found
-- A library with BLAS API found.
-- BLASfound:/usr/lib/atlas-base/libf77blas.so/usr/lib/atlas-base/libatlas.so
-- Configuring done
-- Generating done
-- Build files have been written to: /tmp
Similarly, just
cmake .
will find /usr/lib/libblas.so for me. (I do not forget to remove the cache files before the second call.)
When I look into /usr/share/cmake-2.8/Modules/FindBLAS.cmake, I read as the permitted values of BLA_VENDOR:
## Goto,ATLAS PhiPACK,CXML,DXML,SunPerf,SCSL,SGIMATH,IBMESSL,Intel10_32 (intel mkl v10 32 bit),Intel10_64lp (intel mkl v10 64 bit,lp thread model, lp64 model),
## Intel10_64lp_seq (intel mkl v10 64 bit,sequential code, lp64 model),
## Intel( older versions of mkl 32 and 64 bit), ACML,ACML_MP,ACML_GPU,Apple, NAS, Generic
That is, OpenBLAS is not listed. And a few random trials like
cmake . -DBLA_VENDOR=open -DCMAKE_PREFIX_PATH=/usr/lib/openblas-base
do not work either. Do I have to write my own FindBLAS in order to link to OpenBLAS with CMake?
CMake supports finding OpenBLAS using FindBLAS since CMake 3.6.0, as this commit finally made it into the release.
NB: OpenBLAS can also be used to substitute LAPACK, for which you should use the FindLAPACK command, that is also available since 3.6.0.
The "FindBLAS" and "FindLAPACK" modules learned to support OpenBLAS.
(See: https://blog.kitware.com/cmake-3-6-0-rc3-is-now-ready/)
I think you have to write your own CMake module for that.
You can check there and there maybe for inspirations
According to caffe, you can use it this way,below is my project structure:
FindOpenBLAS.cmake contents as below:
SET(Open_BLAS_INCLUDE_SEARCH_PATHS
/usr/include
/usr/include/openblas
/usr/include/openblas-base
/usr/local/include
/usr/local/include/openblas
/usr/local/include/openblas-base
/opt/OpenBLAS/include
$ENV{OpenBLAS_HOME}
$ENV{OpenBLAS_HOME}/include
)
SET(Open_BLAS_LIB_SEARCH_PATHS
/lib/
/lib/openblas-base
/lib64/
/usr/lib
/usr/lib/openblas-base
/usr/lib64
/usr/local/lib
/usr/local/lib64
/opt/OpenBLAS/lib
$ENV{OpenBLAS}cd
$ENV{OpenBLAS}/lib
$ENV{OpenBLAS_HOME}
$ENV{OpenBLAS_HOME}/lib
)
FIND_PATH(OpenBLAS_INCLUDE_DIR NAMES
cblas.h PATHS ${Open_BLAS_INCLUDE_SEARCH_PATHS}
)
FIND_LIBRARY(OpenBLAS_LIB NAMES
openblas PATHS ${Open_BLAS_LIB_SEARCH_PATHS}
)
message("OpenBLAS_INCLUDE_DIR: ${OpenBLAS_INCLUDE_DIR}, OpenBLAS_LIB: ${OpenBLAS_LIB}")
when use cmake it outputs :
The main CMakeLists.txt use the above FindOpenBLAS.cmake to find blas:
cmake_minimum_required(VERSION 3.5)
project(my_caffe)
set(CMAKE_CXX_STANDARD 11)
#dependence lib
set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
find_package(OpenBLAS REQUIRED)
set(SOURCE_FILES main.cpp)
add_executable(my_caffe_test ${SOURCE_FILES})
# link blas
target_link_libraries(my_caffe_test
${OpenBLAS_LIB}
)