cmake - find_library - 自定义库位置 [英] cmake - find_library - custom library location

查看:12878
本文介绍了cmake - find_library - 自定义库位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正试图让CMake运行我的项目(在Windows上)。我想使用所有库安装的自定义位置。要告诉CMake这个路径,我试图这样做:

 设置(CMAKE_PREFIX_PATH D:/ develop / cmake / libs)

但是当我尝试使用

找到库时

  find_library(CURL_LIBRARY NAMES curl curllib libcurl_imp curllib_static)

找不到。
当我将我的前缀路径设置为

  set(CMAKE_PREFIX_PATH D:/ develop / cmake / libs / curl)

...库位于。



所以我的问题是:
如何正确配置CMake,使用目录结构体在自定义位置,如下所示:

  D:/ develop / cmake / libs / 
- > libA
- > include
- > lib
- > libB
- > include
- > lib
- > ...
- > include
- > lib



在include中,公共头和lib是编译库。 p>

希望有人可以帮助我 - 提前感谢



编辑:
目前的解决方法是,在搜索库之前执行此操作:

 设置(CUSTOM_LIBRARY_PATH D:/ develop / cmake / libs)
文件(gbb-dir $ {CUSTOM_LIBRARY_PATH} / *)
foreach(dir $ {sub-dir})
if(IS_DIRECTORY $ {dir})
set(CMAKE_PREFIX_PATH $ {CMAKE_PREFIX_PATH}; $ {dir})
endif()
endforeach()

方式boost的默认模块不会找到它,直到它,因为boost的目录structore是有点不同。

  boost  - > include  - > boost-1_50  - > * .hpp 

当我将内容boost-1_50移动到include

解决方案

我看到两个人把这个问题放到他们的收藏夹所以我会尽力回答为我工作的解决方案:
而不是使用find模块,我为所有安装的库编写配置文件。这些文件极端简单,也可以用于设置非标准变量。 CMake将(至少在Windows上)在 中搜索这些配置文件

  CMAKE_PREFIX_PATH /<< package_name> ;< version>> /> /< package_name>> -config.cmake 

其可以通过环境变量设置)。
例如,boost配置位于路径中

  CMAKE_PREFIX_PATH / boost-1_50 / boost-config.cmake 

在该配置中可以设置变量。我的boost配置文件看起来像这样:

 设置(boost_INCLUDE_DIRS $ {boost_DIR} / include)
设置(boost_LIBRARY_DIR $ {boost_DIR} / lib)
foreach(component $ {boost_FIND_COMPONENTS})
set(boost_LIBRARIES $ {boost_LIBRARIES} debug $ {boost_LIBRARY_DIR} / libboost _ $ {component} -vc110-mt-gd-1_50。 lib)
set(boost_LIBRARIES $ {boost_LIBRARIES} optimized $ {boost_LIBRARY_DIR} / libboost _ $ {component} -vc110-mt-1_50.lib)
endforeach()
add_definitions(-D_WIN32_WINNT = 0x0501 )

很简单的+可以缩小配置文件的大小,帮助函数。我唯一的问题,我有这个设置是我没有找到一种方法给配置文件的优先级高于查找模块 - 所以你需要删除查找模块。



希望这对其他人有帮助。


I'm currently trying to get CMake running for my project (on windows). I want to use a custom location where all libraries are installed. To inform CMake about that path I tried to do that:

set(CMAKE_PREFIX_PATH D:/develop/cmake/libs)

But when I try to find the library with

find_library(CURL_LIBRARY NAMES curl curllib libcurl_imp curllib_static)

CMake can't find it. When I set my prefix path to

set(CMAKE_PREFIX_PATH D:/develop/cmake/libs/curl)

... the library is located.

So my question is: How can I configure CMake properly to work with a directory structore at a custom location which looks like that:

D:/develop/cmake/libs/
-> libA
   -> include
   -> lib
-> libB
   -> include
   -> lib
-> ...
   -> include
   -> lib

In "include" lie the public headers and in "lib" are the compiled libraries.

Hope someone can help me - Thanks in advance

edit: The current workaround for me is, to do this before i search for libraries:

set(CUSTOM_LIBRARY_PATH D:/develop/cmake/libs)
file(GLOB sub-dir ${CUSTOM_LIBRARY_PATH}/*)
foreach(dir ${sub-dir})
    if(IS_DIRECTORY ${dir})
        set(CMAKE_PREFIX_PATH ${CMAKE_PREFIX_PATH};${dir})
    endif()
endforeach()

But that way the default module for boost wont find it until it because the directory structore of boost is a bit different.

boost -> include -> boost-1_50 -> *.hpp

When I move the content if "boost-1_50" to "include" the library can be found but that way it's not possible to handle multiple versions right?

解决方案

I saw that two people put that question to their favorites so I will try to answer the solution which works for me: Instead of using find modules I'm writing configuration files for all libraries which are installed. Those files are extremly simple and can also be used to set non-standard variables. CMake will (at least on windows) search for those configuration files in

CMAKE_PREFIX_PATH/<<package_name>>-<<version>>/<<package_name>>-config.cmake

(which can be set through an environment variable). So for example the boost configuration is in the path

CMAKE_PREFIX_PATH/boost-1_50/boost-config.cmake

In that configuration you can set variables. My config file for boost looks like that:

set(boost_INCLUDE_DIRS ${boost_DIR}/include)
set(boost_LIBRARY_DIR ${boost_DIR}/lib)
foreach(component ${boost_FIND_COMPONENTS}) 
    set(boost_LIBRARIES ${boost_LIBRARIES} debug ${boost_LIBRARY_DIR}/libboost_${component}-vc110-mt-gd-1_50.lib)
    set(boost_LIBRARIES ${boost_LIBRARIES} optimized ${boost_LIBRARY_DIR}/libboost_${component}-vc110-mt-1_50.lib)
endforeach()
add_definitions( -D_WIN32_WINNT=0x0501 )

Pretty straight forward + it's possible to shrink the size of the config files even more when you write some helper functions. The only issue I have with this setup is that I havn't found a way to give config files a priority over find modules - so you need to remove the find modules.

Hope this this is helpful for other people.

这篇关于cmake - find_library - 自定义库位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆