CMake: linking shared library located out of root project

Viewed 87

How to handle library out of project's root? Currently I want to use CMake to link to my project shared library that is located not in the root directory of a certain library. The library is located in lib, it's shared only between projects located in projects\project_n. Also, I need every project to build the lib independently

.
├── lib
│   ├── CMakeLists.txt
│   ├── src1.c
│   ├── hed1.h
│   ├── src2.c
│   ├── hed2.h
│  ...
│   ├── src_n.c
│   └── hed_n.h
└── projects
    ├── project_1
    │   ├── Src 
    │   ├── Inc
    │   ├── build
    │   └── CMakeLists.txt
    ├── project_2
    │   ├── Src
    │   ├── Inc
    │   ├── build
    │   └── CMakeLists.txt
   ...
    └── project_n
        ├── Src
        ├── Inc
        ├── build
        └── CMakeLists.txt

Currently my lib\CMakeLists.txt looks like that:

cmake_minimum_required(VERSION 3.20)
file(GLOB_RECURSE SRCS *.c)
add_library(lib SRCS)

Part of code in projects\project_1\CMakeLists.txt responsible for lib linking looks like that:

add_subdirectory(../../lib build)
target_include_directories(${PROJECT_NAME} PUBLIC ../../lib)
target_link_directories(${PROJECT_NAME} PRIVATE ../../lib)
target_link_libraries(${PROJECT_NAME} lib)

Currently I'm getting Errors belows: CMake Error at CMakeLists.txt:65 (target_include_directories): Cannot specify include directories for target "project_1" which is not built by this project.

CMake Error at CMakeLists.txt:66 (target_link_directories): Cannot specify link directories for target "project_1" which is not built by this project.

CMake Error at CMakeLists.txt:67 (target_link_libraries): Cannot specify link libraries for target "project_1" which is not built by this project.

0 Answers
Related