如何在cmake中添加库路径? [英] How do I add a library path in cmake?
问题描述
我的项目中有2个文件夹 inc和 lib,分别具有标题和静态库。如何告诉cmake分别使用这两个目录进行include和linking?
最简单的方法是添加
include_directories($ {CMAKE_SOURCE_DIR} / inc)
$ p中找到$ p>
link_directories($ {CMAKE_SOURCE_DIR} / lib)
add_executable(foo $ {FOO_SRCS})
target_link_libraries(foo bar)#libbar.so在$ {CMAKE_SOURCE_DIR} / lib
现代CMake版本不会向每个编译器调用添加
-I和-L
标志使用导入的库:add_library(栏已共享导入)#或STATIC而不是SHARED
set_target_properties(栏属性
IMPORTED_LOCATION $ {CMAKE_SOURCE_DIR} /lib/libbar.so
INTERFACE_INCLUDE_DIRECTORIES $ {CMAKE_SOURCE_DIR} / include / libbar
)
set(FOO_SRCS foo.cpp )
add_executable(foo $ {FOO_SRCS})
target_link_libraries(foo bar)#也添加了ed include path
如果设置
INTERFACE_INCLUDE_DIRECTORIES
不没有添加路径,较早版本的CMake也允许您使用target_include_directories(bar PUBLIC / path / to / include)
。但是,此在CMake 3.6或更高版本中不再起作用。I have 2 folders "inc" and "lib" in my project which have headers and static libs respectively. How do I tell cmake to use those 2 directories for include and linking respectively?
解决方案The simplest way of doing this would be to add
include_directories(${CMAKE_SOURCE_DIR}/inc) link_directories(${CMAKE_SOURCE_DIR}/lib) add_executable(foo ${FOO_SRCS}) target_link_libraries(foo bar) # libbar.so is found in ${CMAKE_SOURCE_DIR}/lib
The modern CMake version that doesn't add the
-I and -L
flags to every compiler invocation would be to use imported libraries:add_library(bar SHARED IMPORTED) # or STATIC instead of SHARED set_target_properties(bar PROPERTIES IMPORTED_LOCATION "${CMAKE_SOURCE_DIR}/lib/libbar.so" INTERFACE_INCLUDE_DIRECTORIES "${CMAKE_SOURCE_DIR}/include/libbar" ) set(FOO_SRCS "foo.cpp") add_executable(foo ${FOO_SRCS}) target_link_libraries(foo bar) # also adds the required include path
If setting the
INTERFACE_INCLUDE_DIRECTORIES
doesn't add the path, older versions of CMake also allow you to usetarget_include_directories(bar PUBLIC /path/to/include)
. However, this no longer works with CMake 3.6 or newer.这篇关于如何在cmake中添加库路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!