从外部目录CMake链接库

时间:2018-05-05 20:26:50

标签: c++ cmake shared-libraries

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

我试图将共享的lib mylib.so链接到可执行文件,但是CMake会抛出错误  Imported target "mylib" includes non-existent path in its INTERFACE_INCLUDE_DIRECTORIES.

如何从example1以外的目录链接共享库?

是的,有很多类似的问题,但没有人有这个问题。

我跟着Andre回答并推出了这个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)

0 个答案:

没有答案