CMake在构建主目录之前从子目录构建并安装共享库 [英] CMake build and install shared library from subdirectory before building main directory
问题描述
这是我的源代码结构:
cd my_git_repo/
CMakeLists.txt
src/
main.cpp
mylibrary/
a.hpp
b.hpp
a.cpp
b.cpp
CMakeLists.txt
根 CMakeLists.txt
:
cmake_minimum_required(VERSION 3.9)
project(myexe CXX)
add_subdirectory(src/mylibrary)
find_library(mylib NAMES mylibrary.so PATHS "./src/mylibrary/mylibrary.so")
add_executable(myexe src/main.cpp)
target_link_libraries(myexe ${mylib})
mylibrary / CMakeLists.txt
非常简单。它构建一个共享库并安装它们。
理想情况下,应在构建 myexe
之前构建并安装目标,并安装目标。但这不会发生。在构建 mylibrary
之后是 myexe
。安装将在稍后进行。因此, find_library
失败。 pkg_check_modules()
适用于其他共享库,但由于相同的原因而在此处失败。
mylibrary/CMakeLists.txt
is very simple. It builds a shared library and installs them.
Ideally, mylibrary
target should be built and installed before myexe
is built. But this doesn't happen. mylibrary
is built followed by myexe
. Installation happens later. Because of this, find_library
fails. pkg_check_modules()
works for other shared libraries but fails here because of the same reason.
感谢您的帮助。
编辑:
这个问题与重复的问题有所不同,因为发布到该问题的答案似乎是在静态链接库 target_link_libraries(game引擎)
。我想动态链接 .so
库。
This question differs from the duplicate because the answers posted to that question seem to be statically linking the library target_link_libraries(game engine)
. I want to dynamically link the .so
library.
推荐答案
在CMake中,是先构建模块,然后将它们链接在一起。
The idea in CMake is to build modules and then link them together.
您尚未共享 CMakeLists.txt
用于我的库,因此我们无法确定它在做什么。但是,假设它是这样的:
You haven't shared the CMakeLists.txt
for my library, so we cannot tell what it is doing. However, assuming that it is something like:
ADD_LIBRARY(mylibrary
file1.cpp
file2.cpp
)
由于您指定要 mylibrary
始终链接为共享链接,您还需要通过将 BUILD_SHARED_LIBS
设置为 ON
或通过在SHARED > add_library
:
Since you specified that you want mylibrary
to always be linked as shared, you need to tell CMake that as well by either setting BUILD_SHARED_LIBS
TO ON
or by specifying SHARED
in add_library
:
ADD_LIBRARY(mylibrary SHARED
file1.cpp
file2.cpp
)
这是您的图书馆模块。我们现在将使其保持简单,而不必担心在此处打包库归档文件和安装。
This is your library module. We will keep it simple for now and not worry about packing the library archive and installation here.
现在,回到您的主 CMakeLists.txt
以及如何使 myexe
使用它。由于您已经有 add_subdirectory(src / mylibrary)
,因此CMake知道 mylibrary
。因此,只需使用模块名称链接即可。既然已经定义了模块,就无需 find_library
。
Now, back to your main CMakeLists.txt
and how to make myexe
consume it. Since you have already add_subdirectory(src/mylibrary)
, CMake knows about mylibrary
. So simply link it using the module name. There is no need to find_library
as you have already defined the module.
add_executable(myexe src/main.cpp)
target_link_libraries(myexe mylibrary)
但是请注意,这是一个非常基本的示例,向您解释CMake是如何工作的。如果您没有构建该库,并且已经安装了该库,则可以调用 find_library
。现代CMake稍微复杂一些,并使用生成器表达式,因此在进行更复杂的项目时,请务必仔细阅读。
Do note, however, this is a very basic example to explain to you how CMake is designed to work. If you aren't building the library, and it is already installed, you would call find_library
. Modern CMake is a bit more sophisticated and uses generator expressions, so be sure to read up on that as you progress to more complex projects.
这篇关于CMake在构建主目录之前从子目录构建并安装共享库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!