0
\lib
 mylib.so
 \- inc
   \- mod1
   \- mod2
   \- another_lib
\examples
  \- common_inc 
  \- example1
    CMakeLists.txt
    \- src
    \- inc
  \- example2
     ...

I'm trying to link shared lib mylib.so to executable but CMake throws error Imported target "mylib" includes non-existent path in its INTERFACE_INCLUDE_DIRECTORIES.

How to link shared library from directory outside of example1?

Yeah, there are a lot of similar questions but nobody had this issue.

I followed Andre answer and came out with this CMakeLists.txt

cmake_minimum_required (VERSION 3.5)

project (example1 CXX)
set (CMAKE_CXX_STANDARD 14)

set (SRC
                "src/main.cpp"
                "src/mod1.cpp"
)

set (INC
                "inc/mod1.hpp"
)


add_library(mylib SHARED IMPORTED GLOBAL)
set_target_properties(mylib PROPERTIES 
    IMPORTED_LOCATION "${CMAKE_SOURCE_DIR}../../lib/mylib.so"
    INTERFACE_INCLUDE_DIRECTORIES "${CMAKE_SOURCE_DIR}../../lib/inc"
)

add_executable (example1 ${SRC})
target_include_directories(example1 PRIVATE "inc")
target_include_directories(example1 PRIVATE "../common_inc") 
target_link_libraries(example1 mylib)
KaDw
  • 78
  • 1
  • 7
  • 1
    Path `${CMAKE_SOURCE_DIR}../../lib/inc` is actually doesn't exist: you forgot slash (`/`) after `${CMAKE_SOURCE_DIR}`. The same issue is with *IMPORTED_LOCATION* property. – Tsyvarev May 05 '18 at 21:31
  • @Tsyvarev Silly mistake... You are right, It's working now. – KaDw May 05 '18 at 21:53

0 Answers0