CMake:如何链接库没有自动搜索功能FIND_PACKAGE? [英] CMake : how to link a library WITHOUT automatic search function FIND_PACKAGE?

查看:187
本文介绍了CMake:如何链接库没有自动搜索功能FIND_PACKAGE?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何查找/链接一个没有任何FIND_PACKAGE的库。



假设我们有一个名为testlib的个人库:

  /perso/testlib/include/testlib1.h 
/perso/testlib/include/testlib2.h
/ perso / testlib / lib / testlib1.a
/perso/testlib/lib/testlib2.a

如何链接它与CMake?



1)有什么功能直接链接到CMakeLists.txt的代码?



2)如何允许用户选择文件在哪里?



3)我难以理解什么是解释和什么不是由CMake。例如,如果你定义一个变量$ {MYVARIABLE_INCLUDE_DIR}或$ {MYVARIABLE_LIBRARIES}是INCLUDE_DIR或LIBRARIES,CMake解释的扩展或没有区别,如果我调用这个变量$ {MYVARIABLE_INCDIR}?



4)如果你有一个库在lib目录中包含十个库文件或更多库,那么如何做相同的过程(包括一个个人库)?



5)最后,当你输入 TARGET_LINK_LIBRARIES(myexecutable gmp)时,如何知道库的名称是gmp。为什么不Gmp或GMP?在这个函数中放置的库的名称是否等于.a文件减去lib和.a?例如:libgmp.a - > gmp?如果我想链接一个名为libtestlolexample.a的库,我必须键入 TARGET_LINK_LIBRARIES(myexecutable testlolexample)



解决方案

您可以使用 target_link_libraries(myexecutable mylib)链接到库mylib。编译器将使用其默认方式来查找指定的库(例如,它将在Linux上查找libmylib.a)。编译器只会查找 link_directories(directory1 directory2 ...),因此您可以尝试该命令将所需的目录添加到搜索路径。



当mylib也用CMake编译时,这将被识别,一切都应该自动工作。



指定一个目录可以使用一个缓存的CMake变量。 set(MYPATHNOT-DEFINEDCACHE PATHdocstring)



建议写一个可以与 find_package 一起使用的CMake查找模块。我建议你看看 FindALSA.cmake ,这可以作为一个好的起点。



有趣的部分是结尾:

  if(ALSA_FOUND)
set(ALSA_LIBRARIES $ {ALSA_LIBRARY})
set(ALSA_INCLUDE_DIRS $ {ALSA_INCLUDE_DIR})
endif()

mark_as_advanced(ALSA_INCLUDE_DIR ALSA_LIBRARY)

ALSA_LIBRARY ALSA_INCLUDE_DIR 变量可由用户配置并存储在缓存中, code> ALSA_LIBRARIES 和 ALSA_INCLUDE_DIRS 以及 ALSA_FOUND



通常,使用find模块,如下所示:

  find_package(ALSA REQUIRED)
include_directories($ {ALSA_INCLUDE_DIRS})
target_link_libraries(myexe $ {ALSA_LIBRARIES})

我相信您可以根据您的个人资料库进行调整。


I wonder how to find/link a library without any FIND_PACKAGE.

Assume that we have a "personal" library called testlib :

/perso/testlib/include/testlib1.h
/perso/testlib/include/testlib2.h
/perso/testlib/lib/testlib1.a
/perso/testlib/lib/testlib2.a

How to link it with CMake ?

1) What are the functions to link it directly in the code of the CMakeLists.txt ?

2) How to allow the user to select where are the files ?

3) I have difficulties to understand what is interpreted and what it's not by CMake. For example if you define a variable ${MYVARIABLE_INCLUDE_DIR} or ${MYVARIABLE_LIBRARIES} is "INCLUDE_DIR" or "LIBRARIES" an extension interpreted by CMake or there is no difference if I call this variable ${MYVARIABLE_INCDIR} ?

4) How to do the same procedures (including a "personal" library) if you have a library that contains ten library files or more in the lib directory ?

5) And finally, when you type TARGET_LINK_LIBRARIES(myexecutable gmp), how do you know that the name of the library is "gmp". Why not "Gmp" or "GMP" ? Is the name of the library to put in this function just equal to the .a file minus "lib" and ".a" ? For example libgmp.a -> gmp ? If I want to link a library called libtestlolexample.a, do I have to type TARGET_LINK_LIBRARIES(myexecutable testlolexample) ?

Thank you very much.

解决方案

You can use target_link_libraries(myexecutable mylib) to link to the library "mylib". The compiler will use its default way to find the specified library (e.g. it will look for libmylib.a on Linux). The compiler will only look in the link_directories(directory1 directory2 ...), so you could try that command to add the required directories to the search path.

When "mylib" is also compiled with CMake this will be recognized and everything should work automatically.

When you want the user to specify a directory you can use a cached CMake variable. set(MYPATH "NOT-DEFINED" CACHE PATH "docstring").

For more complex stuff it is very advisable to write a CMake find module that can be used with find_package. I suggest you take a look at the FindALSA.cmake which can be used as a good starting point.

The interesting part is at the end:

if(ALSA_FOUND)
  set( ALSA_LIBRARIES ${ALSA_LIBRARY} )
  set( ALSA_INCLUDE_DIRS ${ALSA_INCLUDE_DIR} )
endif()

mark_as_advanced(ALSA_INCLUDE_DIR ALSA_LIBRARY)

The ALSA_LIBRARY and ALSA_INCLUDE_DIR variables are user configurable and stored in the cache, while ALSA_LIBRARIES and ALSA_INCLUDE_DIRS as well as ALSA_FOUND get computed and are the ones that the user of the find module is supposed to use.

Typically one would use the find module like this:

find_package(ALSA REQUIRED)
include_directories(${ALSA_INCLUDE_DIRS})
target_link_libraries(myexe ${ALSA_LIBRARIES})

I'm sure you can adapt this for your personal library.

这篇关于CMake:如何链接库没有自动搜索功能FIND_PACKAGE?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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