CMake:链接生成的库?
我有一个巨大的项目,有两个主要目录: - /myproject/src
- /myproject/app
该策略是 src 在 /myproject/lib
目录中生成库,然后应用程序使用这些库在 /myproject/bin 中生成可执行文件目录。
但问题如下。链接库的经典策略是使用 FIND_LIBRARY()。但是如何链接尚未生成的库呢?
谢谢。
I have a huge project with 2 main directories :
- /myproject/src
- /myproject/app
The strategy is that src produces libraries in the /myproject/lib
directory and then apps use these libraries to produce executables in /myproject/bin
directory.
But the problem is the following. The classic stategy to link libraries is to use the FIND_LIBRARY(). But how to link a library that is not already produced ?
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
find_library() 比名字暗示的要多一点,它不仅查找(预安装的)lib 的路径,而且通常还准备很多变量和函数。此外,find_library() 仅适用于驻留在 cmake/share 目录中的特定库模块。
当您构建自己的库时,必须使用 add_library() 命令将其添加到 CMakeLists.txt,该命令的工作方式与 add_executable() 命令完全相同。
完成此操作后,您实际上可以使用 target_link_libraries() 命令将库添加到可执行文件中。
总结一下:
您实际上不必知道或指定库文件的确切位置,cmake 将为您管理它。
find_library() is a little more than the name suggests, it not only finds the path to a (preinstalled) lib, but often also prepares a lot of variables and functions. Also find_library() only works with specific library modules, which reside in the cmake/share directory.
When you build your own library you have to add it to the CMakeLists.txt with the add_library() command, which works exactly like the add_executable() command.
When you have done that, you can actually add the library to the executable using the target_link_libraries() command.
To sum it up:
You actually don't have to know or specify the exact location of the library-file, cmake will manage that for you.
在 CMake 中,有 add_subdirectory() 命令,您可以使用它在项目中包含子目录的构建。使用这种方式,您可以确认库是在依赖它们的可执行文件之前构建的。
以下是如何构建项目的基本示例:
假设您的项目结构如下所示:
以下是如何设置 CMakeLists.txt 文件:
根级别 CMakeLists.txt(myproject/CMakeLists.txt)
src/CMakeLists.txt
app/CMakeLists.txt
此结构确认库 lib1 和 lib2 是在依赖它们的可执行文件之前构建的。
输出文件分别放置在lib和bin目录中。
In CMake, There is add_subdirectory() command which you can use to include the build of subdirectories in your project. Using this way, you can confirm that the libraries are built before the executables that depend on them.
Here's a basic example of how you might structure your project:
Assuming your project structure looks like this:
Here is how you can set up your CMakeLists.txt files:
Root level CMakeLists.txt(myproject/CMakeLists.txt)
src/CMakeLists.txt
app/CMakeLists.txt
This structure confirms that libraries lib1 and lib2 are built before the executables that depend on them.
The output files are placed in the lib and bin directories, respectively.