问题描述
我的C ++项目 doggo
在 doggo / external /
目录中有第三方代码。目前,它包含和CMakeLists.txt:
My C++ project doggo
has a doggo/external/
directory for third-party code. Currently it contains gtest and a CMakeLists.txt:
# Google gtest for unit testing.
add_subdirectory(gtest)
message("gtest include dir: ${gtest_SOURCE_DIR}")
include_directories(${gtest_SOURCE_DIR})
我的顶级doggo / CMakeLists.txt包含 add_subdirectory(external)
行,以查找并构建第三个党库。一切都像魅力一样-我可以在 #include< gtest / gtest.h>
中包含gtest。现在,我想将randomkit C库添加到 doggo / external /
,如此处所示:。
My top-level doggo/CMakeLists.txt contains the line add_subdirectory(external)
to find and build the third-party libraries. Everything works like a charm -- I can include gtest with #include <gtest/gtest.h>
. Now I'd like to add the randomkit C library to doggo/external/
, as is done here: randomkit from numpy.
如何获取随机套件来构建我的 doggo / external /
目录? doggo / external / CMakeLists.txt
应该是什么样?
How can I get randomkit to build in my doggo/external/
dir? What should the doggo/external/CMakeLists.txt
look like?
然后我应该能够包含C通过在外部 C {...}
块()。
I should then be able to include the C headers for use in my x.cpp files by including the headers inside an extern "C" { ... }
block (details here).
更新:如何在此处安装randomkit?
我已经像上面那样包含了CMakeLists.txt条目,但对于randomkit,目录看起来像这样,
UPDATE: How do I install randomkit here?I've included a CMakeLists.txt entry like that above but for randomkit, and the directory looks like,
external
├── CMakeLists.txt
├── gtest
│ └── ...
└── randomkit
├── CMakeLists.txt
├── distributions.c
├── distributions.h
├── randomkit.c
└── randomkit.h
和 randomkit / CMakeLists.txt
:
project(randomkit)
file(GLOB SOURCES "*.c")
add_library(randomkit SHARED ${SOURCES})
INSTALL(
DIRECTORY ${CMAKE_SOURCE_DIR}/
DESTINATION "/usr/local/"
#DESTINATION ""
FILES_MATCHING PATTERN "*.h*")
(第二个目的地已注释掉,表明我也尝试过)
(second DESTINATION commented out to show I tried that as well)
当我为顶级项目 doggo
运行构建步骤时,尝试执行 #include< randomkit / distributions.h> 时出现错误code>:
Yet when I run the build steps for my top-level project doggo
I get an error trying to #include <randomkit/distributions.h>
:
doggo/src/random_fooz.cpp:10:37: fatal error: randomkit/distributions.h: No such file or directory
更新2 :doggo / CMakeLists.txt:
UPDATE 2: doggo/CMakeLists.txt:
project(doggo)
# Find and build third-party libraries
add_subdirectory(external)
# Add source dirs to the search path so cmake can find headers
include_directories(${CMAKE_SOURCE_DIR}/include/)
# Collect source files and build
file(GLOB_RECURSE doggo_srcs ${CMAKE_SOURCE_DIR}/src/*.cpp)
add_library(doggo ${doggo_srcs})
# Setup executables
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_SOURCE_DIR}/bin/)
add_subdirectory(exec)
# Tests
add_subdirectory(test)
推荐答案
在 randomkit / CMakeLists.txt
写入:
project(randomkit)
file(GLOB SOURCES "*.c")
add_library(randomkit SHARED ${SOURCES})
target_include_directories(randomkit PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
INSTALL(
DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/
DESTINATION "include" # this a the subdirectory with ${CMAKE_INSTALL_PREFIX}
FILES_MATCHING PATTERN "*.h*")
在主 CMakeLists.txt
,您可以这样做:
add_library(doggo ${doggo_srcs})
target_link_libraries(doggo PUBLIC randomkit)
target_include_directories(doggo PUBLIC ${CMAKE_SOURCE_DIR}/include/)
请勿使用 include_directories
。
现在,因为 randomkit
目标具有 PUBLIC
属性具有正确的包含目录,则在构建doggo库时将自动使用这些包含目录。同样,由于doggo库在其公共界面中包含包含目录和库,因此链接到doggo的可执行文件将自动链接到这些库,并找到其包含文件。
Now, because the randomkit
target has the PUBLIC
property with the right include directories, those include directories will be automatically used when building the doggo library. And again, because the doggo library has include directories and libraries in its public interface, executables that you link to doggo will automatically be linked to these libraries, and find their include files.
请注意, randomkit / CMakeLists.txt $ c $中的
INSTALL
命令仅当您实际运行安装目标时才执行c>。构建时,必须在源代码树中找到包含文件。
Note that the INSTALL
command in randomkit/CMakeLists.txt
is only executed when you actually run the install target. When building, the include files must be found in the source tree.
这篇关于CMakeLists.txt,用于C ++项目中的第三方C文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!