使用 cmake 中的自动工具构建库
这是我第一次尝试使用 cmake,如果可能的话,我希望得到一些关于我所做的事情的反馈,因为仍然存在一些问题。
在库文件夹的 CMakeLists.txt 中,我创建了两个 makefile 目标:configure-antlr3c
和 antlr3c
。第一个目标运行 autotools 配置 shell 脚本,第二个目标运行 make 可执行文件来构建库:
# CMakeLists.txt in libantlr3c-3.1.3
add_custom_target(
configure-antlr3c
${SHELL_EXECUTABLE} configure
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)
add_custom_target(
antlr3c
${MAKE}
DEPENDS configure-antlr3c
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)
主要问题是 configure-antlr3c
目标始终“过时”,因此它始终会即使没有发生任何变化也会被执行。此外,我必须在单独的目录(而不是在我的项目的根目录中)生成我的cmake makefile,以避免覆盖库的autotools Makefile...
有人遇到过这个问题吗(使用cmake构建autotools项目)?如果是这样,您的解决方案是什么?
谢谢。
编辑:解决方案 在根 CMakeLists.txt 中:
include(ExternalProject)
ExternalProject_Add(
libantlr3c
SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/lib/libantlr3c-3.1.3
CONFIGURE_COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/lib/libantlr3c-3.1.3/configure --prefix=${CMAKE_CURRENT_SOURCE_DIR}/lib/libantlr3c-3.1.3
PREFIX ${CMAKE_CURRENT_SOURCE_DIR}/lib/libantlr3c-3.1.3
BUILD_COMMAND make
BUILD_IN_SOURCE 1
)
This is my first try with cmake and I would like to have, if possible, some feedbacks about what I did since some problems remain.
In the CMakeLists.txt of the library folder, I created two makefile targets: configure-antlr3c
and antlr3c
. The first target runs the autotools configuration shell script, the second one runs the make executable to build the library:
# CMakeLists.txt in libantlr3c-3.1.3
add_custom_target(
configure-antlr3c
${SHELL_EXECUTABLE} configure
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)
add_custom_target(
antlr3c
${MAKE}
DEPENDS configure-antlr3c
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)
The main problem is thatconfigure-antlr3c
target is always "out of date", so it will always be executed even if no changes happened. Moreover, I necessarily need to generate my cmake makefiles in a separate directory (not in the root directory of my project) to avoid overriding the autotools Makefile of the library...
Has anyone had this problem (building autotools projects with cmake) ? And if so, what have been your solutions ?
Thank you.
EDIT : Solution
In the root CMakeLists.txt:
include(ExternalProject)
ExternalProject_Add(
libantlr3c
SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/lib/libantlr3c-3.1.3
CONFIGURE_COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/lib/libantlr3c-3.1.3/configure --prefix=${CMAKE_CURRENT_SOURCE_DIR}/lib/libantlr3c-3.1.3
PREFIX ${CMAKE_CURRENT_SOURCE_DIR}/lib/libantlr3c-3.1.3
BUILD_COMMAND make
BUILD_IN_SOURCE 1
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为你最好使用 ExternalProject< /a> cmake 的功能。我猜你有你的项目并且在子目录中有 libantrl ?
如果是这种情况,您可以在顶层 CMakeLists.txt 中执行类似的操作:
扩展为类似 libantlr-prefix 的内容,因此内容会安装在您的构建树中,而不是在 /usr/local 中,这就是 autotools 在没有前缀的情况下所做的事情。I think that you'd be better off using the ExternalProject feature of cmake. I guess you have your project and have libantrl in a sub directory?
If that's the case, you can do something like this in the top level CMakeLists.txt:
The
<INSTALL_DIR>
is expanded to something like libantlr-prefix, so things are installed in your build tree rather than in /usr/local, which is what autotools would do without a prefix.我在大约四年前发布了下面的答案,似乎仍然偶尔会被点赞。答案“有效”,但在几个方面存在问题。此外,与此同时,CMake 现在拥有
FetchContent
,它应该在大多数(如果不是全部)用例中取代ExternalProject
。事实上,任何阅读本文的人都应该考虑使用 CMake Package Manager (CPM),它包装了FetchContent
并提供了一些非常好的功能,同时与FetchContent
相比,复杂性相对较小。下面的新示例使用FetchContent
,但可以轻松修改为使用CPM
。此代码安装
libgpiod
,但只需稍加更改即可用于安装使用autotools
构建的其他软件包。下面是 4 年前的答案,可能作为参考很有用。
我需要做类似的事情,但发现很难找到可行的解决方案,尽管这里提供了示例并提供了已接受的答案,以及其他几篇博客文章中提供的代码片段、CMake 支持电子邮件 listserv 档案等。为了让遇到此问题的其他人受益,这里是我的解决方案。
我们想要使用的外部项目是 libmodbus,尽管我相信我的解决方案足够通用,可以与使用
./autoconf 的标准
。autoconf
配方配置的任何项目一起使用.sh &&配置.sh &&制作&&进行安装我们想要添加 libmodbus 作为 git 存储库的子模块。我们将其添加到存储库的路径
/opt/libmodbus
中。配置它的CMake
代码位于/cmake/modbus.cmake
中,它包含在我们的根CMakeLists.txt
中使用cmake/modbus.cmake
的内容是:使用
libmodbus
的组件可以照常声明其依赖项:一些注意事项:
DOWNLOAD_COMMAND
代码>执行autogen.sh
步骤。git clean -dfX
可能不是必需的(它是使用BUILD_IN_SOURCE
选项的早期版本的剩余部分。如果您确实想下载代码而不是使用git 子模块,您需要适当地修改此行,BUILD_BYPRODUCTS ${MODBUS_STATIC_LIB}
声明,用于设置IMPORTED_LOCATION
的set_target_properties
命令将会失败。set_target_properties
命令 也会失败。如果没有file(MAKE_DIRECTORY) 设置 INTERFACE_INCLUDE_DIRECTORIES
将失败${MODBUS_INCLUDES})。I posted the answer below almost four years ago and it seems that it is still occasionally being upvoted. The answer "works" but it problematic in several ways. Also, in the meantime CMake now has
FetchContent
which should replaceExternalProject
in most if not all use cases. In fact, anyone reading this should consider using the CMake Package Manager (CPM) which wrapsFetchContent
and provides some very nice features while being a relatively minimal step up in complexity overFetchContent
. The new example below usesFetchContent
but could easily be modified to useCPM
.This code installs
libgpiod
but should be usable with only minor changes to for installing other packages built withautotools
.The 4-year old answer is below, which might be useful as reference.
I needed to do something similar but found it surprisingly difficult to get a working solution, despite the example provided here with the accepted answer, and code snippets provided in several other blog posts, the CMake support email listserv archives, etc. For the benefit of others who come across this question, here is my solution.
The external project we wanted to use is
libmodbus
, though I believe my solution is general enough to work with any project configured with the standardautoconf
recipe of./autoconf.sh && configure.sh && make && make install
.We wanted to add
libmodbus
as a submodule of our git repository. We added to our repository at the path<root>/opt/libmodbus
. TheCMake
code to configure it is located in<root>/cmake/modbus.cmake
, which is included from our rootCMakeLists.txt
usingThe content of
cmake/modbus.cmake
is:A component that uses
libmodbus
can declare its dependency as usual:A few notes:
DOWNLOAD_COMMAND
to perform theautogen.sh
step. Thegit clean -dfX
is probably not necessary (it is a leftover from an earlier version that used theBUILD_IN_SOURCE
option. If you really want to download the code instead of using a git submodule, you'll need to modify this line appropriately.configure
command line if you want shared libraries.set_target_properties
command to set theIMPORTED_LOCATION
will fail without theBUILD_BYPRODUCTS ${MODBUS_STATIC_LIB}
declaration.set_target_properties
command to set theINTERFACE_INCLUDE_DIRECTORIES
will fail without thefile(MAKE_DIRECTORY ${MODBUS_INCLUDES})
.