CMake:链接生成的库?

发布于 2024-11-30 14:38:18 字数 261 浏览 0 评论 0原文

我有一个巨大的项目,有两个主要目录: - /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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

歌枕肩 2024-12-07 14:38:18

find_library() 比名字暗示的要多一点,它不仅查找(预安装的)lib 的路径,而且通常还准备很多变量和函数。此外,find_library() 仅适用于驻留在 cmake/share 目录中的特定库模块。

当您构建自己的库时,必须使用 add_library() 命令将其添加到 CMakeLists.txt,该命令的工作方式与 add_executable() 命令完全相同。

完成此操作后,您实际上可以使用 target_link_libraries() 命令将库添加到可执行文件中。

总结一下:

add_library(myLib libsourceA.c libSourceB.c)

add_executable(myProgram prgsourceA.c prgsourceB.c)
target_link_libraries(myProgram myLib)

您实际上不必知道或指定库文件的确切位置,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:

add_library(myLib libsourceA.c libSourceB.c)

add_executable(myProgram prgsourceA.c prgsourceB.c)
target_link_libraries(myProgram myLib)

You actually don't have to know or specify the exact location of the library-file, cmake will manage that for you.

半衾梦 2024-12-07 14:38:18

在 CMake 中,有 add_subdirectory() 命令,您可以使用它在项目中包含子目录的构建。使用这种方式,您可以确认库是在依赖它们的可执行文件之前构建的。
以下是如何构建项目的基本示例:

假设您的项目结构如下所示:

myproject/
|-- CMakeLists.txt
|-- src/
|   |-- CMakeLists.txt
|   |-- lib1/
|   |   |-- source files for lib1
|   |-- lib2/
|   |   |-- source files for lib2
|-- app/
|   |-- CMakeLists.txt
|   |-- app1/
|   |   |-- source files for app1
|   |-- app2/
|   |   |-- source files for app2
|-- lib/
|-- bin/

以下是如何设置 CMakeLists.txt 文件:

根级别 CMakeLists.txt(myproject/CMakeLists.txt)

cmake_minimum_required(VERSION 3.12)
project(MyProject)

# Add the source files directory for src
add_subdirectory(src)

# Add the source files directory for app
add_subdirectory(app)

src/CMakeLists.txt

# Add 1st library lib1
add_library(lib1 STATIC lib1/source_files.cpp)
target_include_directories(lib1 PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/lib1)

# Add 2nd library lib2
add_library(lib2 STATIC lib2/source_files.cpp)
target_include_directories(lib2 PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/lib2)

# Mention the output directory for libraries 
set(LIB_OUTPUT_PATH ${CMAKE_CURRENT_SOURCE_DIR}/../lib)

app/CMakeLists.txt

# Add executable name app1
add_executable(app1 app1/source_files.cpp)

# Link application app1 with librairies lib1 and lib2
target_link_libraries(app1 PRIVATE lib1 lib2)

# Mention the output directory for executables
set(EXEC_OUTPUT_PATH ${CMAKE_CURRENT_SOURCE_DIR}/../bin)

# Add executable app2
add_executable(app2 app2/source_files.cpp)

# Link application app2 with lib1 and lib2
target_link_libraries(app2 PRIVATE lib1 lib2)

此结构确认库 lib1lib2 是在依赖它们的可执行文件之前构建的。
输出文件分别放置在libbin目录中。

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:

myproject/
|-- CMakeLists.txt
|-- src/
|   |-- CMakeLists.txt
|   |-- lib1/
|   |   |-- source files for lib1
|   |-- lib2/
|   |   |-- source files for lib2
|-- app/
|   |-- CMakeLists.txt
|   |-- app1/
|   |   |-- source files for app1
|   |-- app2/
|   |   |-- source files for app2
|-- lib/
|-- bin/

Here is how you can set up your CMakeLists.txt files:

Root level CMakeLists.txt(myproject/CMakeLists.txt)

cmake_minimum_required(VERSION 3.12)
project(MyProject)

# Add the source files directory for src
add_subdirectory(src)

# Add the source files directory for app
add_subdirectory(app)

src/CMakeLists.txt

# Add 1st library lib1
add_library(lib1 STATIC lib1/source_files.cpp)
target_include_directories(lib1 PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/lib1)

# Add 2nd library lib2
add_library(lib2 STATIC lib2/source_files.cpp)
target_include_directories(lib2 PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/lib2)

# Mention the output directory for libraries 
set(LIB_OUTPUT_PATH ${CMAKE_CURRENT_SOURCE_DIR}/../lib)

app/CMakeLists.txt

# Add executable name app1
add_executable(app1 app1/source_files.cpp)

# Link application app1 with librairies lib1 and lib2
target_link_libraries(app1 PRIVATE lib1 lib2)

# Mention the output directory for executables
set(EXEC_OUTPUT_PATH ${CMAKE_CURRENT_SOURCE_DIR}/../bin)

# Add executable app2
add_executable(app2 app2/source_files.cpp)

# Link application app2 with lib1 and lib2
target_link_libraries(app2 PRIVATE lib1 lib2)

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文