如何在使用CMake安装C++库时实现可配置的程序数据位置?

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

How can I implement a configurable program data location for a C++ library installation using CMake?

问题

我正在使用CMake构建一个C++静态库,用于更大项目中的其他内部开发团队。该库作为GIT存储库对其他团队可用,他们可以克隆或包括为子模块。

这个库在运行时依赖于一个包含一些数据的文件夹。这个文件夹需要位于使用该库的主可执行文件旁边。

我希望我的库的任何用户都可以通过在其CMakeLists.txt中执行以下操作来使用它:

add_subdirectory(<path-to-my-library>)
add_executable(BigExe <...>)
target_link_library(BigExe MyLibrary)
install(TARGETS BigExe)

安装目录应该包含BigExe二进制文件,然后紧挨着它是我的库所需数据的文件夹。

我的库的CMakeLists.txt中的install(...)命令应该是什么样子的?

我也准备接受一个“更广泛”的方法,涉及对我的库分发方式的更深度重构。

到目前为止,我的库的CMakeLists.txt类似于以下内容:

add_library(MyLibrary STATIC <...>)

target_include_directories(MyLibrary 
	PUBLIC 
		${CMAKE_CURRENT_SOURCE_DIR}/include)

target_compile_definitions(MyLibrary PUBLIC <...>)

install(DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/MyData" DESTINATION bin)

这个解决方案仅在BigExe安装在相同的bin子文件夹时才有效,迫使我的库的用户遵循我选择的文件夹结构。我希望反过来:我的库应该将其内容放在其用户放置其内容的地方...

该库需要在Windows和Linux系统上进行编译。

(注意:这个回答是原文的中文翻译,只包括翻译的部分,没有额外的内容。)

英文:

I'm using CMake to build a C++ static library, used by other internal dev teams in bigger projects. The library is available to the other teams as a GIT repository, which they can clone or include as submodule.

This library depends, at runtime, by a folder of files, containing some data. This folder needs to be located next to the main executable which is using the library.

I would like that any user of my library could use it by simply doing this in their CMakeLists.txt

add_subdirectory(&lt;path-to-my-library&gt;)
add_executable(BigExe &lt;...&gt;)
target_link_library(BigExe MyLibrary)
install(TARGETS BigExe)

The install directory should contain the BigExe binary file and then, right next to it, the folder with the data needed by my library.

How should the install(...) command look like in ther CMakeLists.txt of my library?

I'm also ready to accept a "broader" approach, involving a more deep restructuring of the way my library is distributed.

So far, the CMakeLists.txt of my library is something like this

add_library(MyLibrary STATIC &lt;...&gt;)

target_include_directories(MyLibrary 
	PUBLIC 
		${CMAKE_CURRENT_SOURCE_DIR}/include)

target_compile_definitions(MyLibrary PUBLIC &lt;...&gt;)

install(DIRECTORY &quot;${CMAKE_CURRENT_SOURCE_DIR}/MyData&quot; DESTINATION bin)

This solution works if and only if the BigExe is installed in the same bin subfolder, forcing the users of my library to follow the folder structure I chose. I would like to do the reverse: my library should place its things where its users place their things...

The library needs to be compiled for both Windows and Linux systems.

答案1

得分: 1

> 此库在运行时依赖于一个包含一些数据的文件夹。这个文件夹需要位于使用该库的主可执行文件旁边。

> 我的库的CMakeLists.txt中的install(...)命令应该如何写?

所以您是在安装您的库,而是一些数据文件。请参阅https://cmake.org/cmake/help/latest/command/install.html#installing-files

通常,在Linux上,如果它们是与机器无关的文件,它们会放在/usr/share/<library>/,如果它们是与机器相关的文件,它们会放在/usr/lib/<library>/

set(yourdatafiles ...)
install(
   FILES ${yourdatafiles}
   TYPE DATA
)

程序应该打开/usr/share/yourlibrary/并从那里读取。

英文:

> This library depends, at runtime, by a folder of files, containing some data. This folder needs to be located next to the main executable which is using the library.
>
> How should the install(...) command look like in ther CMakeLists.txt of my library?

So you are not installing your library, but some data files. Read https://cmake.org/cmake/help/latest/command/install.html#installing-files

Typically, on Linux they go into /usr/share/&lt;library&gt;/ if they are machine-independent files, or /usr/lib/&lt;library&gt;/ if they are machine dependent files.

set(yourdatafiles ...)
install(
   FILES ${yourdatafiles}
   TYPE DATA
)

The program should open /usr/share/yourlibrary/ and read from there.

huangapple
  • 本文由 发表于 2023年3月8日 19:00:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/75672157.html
匿名

发表评论

匿名网友

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

确定