如何将非CMake库添加到一个CMake项目中?

huangapple go评论61阅读模式
英文:

How can I add a non-CMake library to a CMake project?

问题

I use CMake for my project and would like to add a dependency to it in the form of a library that does not use cmake. I want to make sure that the library can be used later with the target_link_libraries command. That is, in order for cmake to know which file to link, to know which directory is include for the library.

For example, some libraries use autotools to build and install applications and the build process involves the following set of commands installing library files in the $INSTALL directory:

$ ./configure --prefix=$INSTALL
$ make -j
$ make install

After that, the library files can be found in the $INSTALL directory:
headers in $INSTALL/include (for example, $INSTALL/include/foobar.h)
and library in $INSTALL/lib (for example, $INSTALL/lib/foobar.a)

The question is how to use cmake to describe a third-party library so that it can be easily used in the project in the future.

I tried to use add_custom_target, but I couldn't get a working build (for example, target_include_directories does not work with custom target).

英文:

I use CMake for my project and would like to add a dependency to it in the form of a library that does not use cmake. I want to make sure that the library can be used later with the target_link_libraries command. That is, in order for cmake to know which file to link, to know which directory is include for the library.

For example, some libraries use autotools to build and install applications and the build process involves the following set of commands installing library files in the $INSTALL directory:

$ ./configure --prefix=$INSTALL
$ make -j
$ make install

After that, the library files can be found in the $INSTALL directory:
headers in $INSTALL/include (for example, $INSTALL/include/foobar.h)
and library in $INSTALL/lib (for example, $INSTALL/lib/foobar.a)

The question is how to use cmake to describe a third-party library so that it can be easily used in the project in the future.

I tried to use add_custom_target, but I couldn't get a working build (for example, target_include_directories does not work with custom target).

答案1

得分: 3

如果你想使用CMake来驱动非CMake项目的构建和安装,请使用[ExternalProject](https://cmake.org/cmake/help/latest/module/ExternalProject.html)模块。例如:

ExternalProject_Add(foobar
  SOURCE_DIR # TODO
  INSTALL_DIR "${INSTALL}"
  CONFIGURE_COMMAND configure "--prefix=${INSTALL}"
  BUILD_COMMAND make -j
  INSTALL_COMMAND make install
)

关于使用非CMake构建系统构建的目标,你可以通过定义导入库以及调用相应的命令,告诉CMake生成的构建系统有关它们的信息。参见文档以获取有用的信息。例如:

add_library(foobar STATIC IMPORTED GLOBAL)
set_properties(foobar PROPERTIES
  IMPORTED_LOCATION "${INSTALL}/lib/foobar${CMAKE_STATIC_LIBRARY_SUFFIX}"
)
target_include_directories(foobar INTERFACE "${INSTALL}/include")

然后只需[`target_link_libraries`](https://cmake.org/cmake/help/latest/command/target_link_libraries.html)到导入的目标。

作为替代方案,你也可以考虑使用非系统包管理器,比如Conan([CMake支持文档](https://docs.conan.io/1/reference/conanfile/tools/cmake.html#conan-tools-cmake)),或者vcpkg([CMake支持文档](https://learn.microsoft.com/en-us/vcpkg/users/buildsystems/cmake-integration))。
英文:

If you want to drive the build and install of the non-CMake project using CMake, use the ExternalProject module. Ex.

ExternalProject_Add(foobar
  SOURCE_DIR # TODO
  INSTALL_DIR "${INSTALL}"
  CONFIGURE_COMMAND configure "--prefix=${INSTALL}"
  BUILD_COMMAND make -j
  INSTALL_COMMAND make install
)

As for using the targets built by the non-CMake buildsystem, you can tell the CMake-generated buildsystem about them by defining IMPORTED libraries with add_library(<name> <type> IMPORTED [GLOBAL]), and then calling the appropriate commands. See the docs for useful info. Ex.

add_library(foobar STATIC IMPORTED GLOBAL)
set_properties(foobar PROPERTIES
  IMPORTED_LOCATION "${INSTALL}/lib/foobar${CMAKE_STATIC_LIBRARY_SUFFIX}"
)
target_include_directories(foobar INTERFACE "${INSTALL}/include")

and then just target_link_libraries to the IMPORTED target.

You could also as an alternative, look at using a non-system package manager, such as Conan (CMake support docs), or vcpkg (CMake support docs).

huangapple
  • 本文由 发表于 2023年4月17日 07:34:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76030829.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定