Home > Back-end >  CMake - No rule exists to target
CMake - No rule exists to target

Time:11-08

In Ubuntu, I have downloaded a third-party shared library (liba1.so), placed in /lib/extern/lib . The associated header files I placed in /lib/extern/include. My own header files are placed in /include/public/ and /include/private/ . And now (to test mylib) I want to link this in my main.cpp code, using CMake.

My structure:

**My structure:** 
| 
| 
 ---CMakeLists.txt 
| 
 ---lib 
|    | 
|     ---extern 
|         |    
|          ---lib 
|         |    | 
|         |     ---liba.so 
|         |     ---libb.so 
|         |     ---liba12.so 
|         | 
|          ---include 
|                |     
|                 ...headers.h 
|                
 ---include 
|     | 
|      ---public 
|     |     | 
|     |      ---file1.hpp 
|     |      ... 
|     |
|      ---private 
|            | 
|             ---file2.hpp 
|             ... 
| 
 ---src 
     | 
      ---public 
     |     | 
     |      ---file1.cpp 
     |      ... 
     | 
      ---private 
     |     | 
     |      ---file2.cpp 
     |      ...
     | 
      ---main.cpp

My CMakeLists.txt:

cmake_minimum_required(VERSION 3.9...3.19) 
if(${CMAKE_VERSION} VERSION_LESS 3.12) 
     cmake_policy(VERSION ${CMAKE_VERSION}) 
endif() 

project(mylib VERSION 0.0.1 DESCRIPTION "Test" LANGUAGES CXX ) 

set(CMAKE_CXX_FLAGS "${CMAKE_C_FLAGS} -DTPM_POSIX") 
set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD_REQUIRED ON) 
set(CMAKE_CXX_EXTENSIONS OFF) 

add_library(${PROJECT_NAME} STATIC src/private/file2.cpp src/public/file1.cpp ) 
add_library(lib SHARED IMPORTED) 

set_target_properties(lib PROPERTIES IMPORTED_LOCATION ${CMAKE_CURRENT_BINARY_DIR}/lib/extern/lib/liba.so) 

set_target_properties(lib PROPERTIES IMPORTED_LOCATION ${CMAKE_CURRENT_BINARY_DIR}/lib/extern/lib/libb.so) 

set_target_properties(lib PROPERTIES IMPORTED_LOCATION ${CMAKE_CURRENT_BINARY_DIR}/lib/extern/lib/liba12.so) 

target_link_libraries(${PROJECT_NAME} PUBLIC lib) 

target_include_directories(${PROJECT_NAME} 
   PUBLIC 
     $<INSTALL_INTERFACE:lib/extern/include>
     $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/lib/extern/include>

     $<INSTALL_INTERFACE:include/public>
     $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include/public>

     $<INSTALL_INTERFACE:include/private>
     $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include/private> 
) 

add_executable(test src/main.cpp) 

target_link_libraries(test PRIVATE mylib) 

ERROR: 

[build] Consolidate compiler generated dependencies of target mylib 
[build] [ 60%] Built target mylib [build] Consolidate compiler generated dependencies of target test 
[build] gmake[2]: *** No rule exists to target „lib/extern/lib/liba12.so“, 
[build] required by „test“ to create. Ending. 
[build] gmake[1]: *** [CMakeFiles/Makefile2:111: CMakeFiles/test.dir/all] Error 2 
[build] gmake: *** [Makefile:91: all] Error 2 
[proc] The command: /usr/bin/cmake --build /home/mathew/proj/build --config Debug --target all -j 10 -- exited with code: 2 and signal: null [build] Build finished with exit code 2

CodePudding user response:

Your imported files are relative to the source directory, not the build directory. You need to use CMAKE_CURRENT_SOURCE_DIR instead of CMAKE_CURRENT_BINARY_DIR when setting IMPORTED_LOCATION.

  • Related