如何正确添加资源到项目中?
我有一个项目组织如下:
include/
src/
share/myprogram/
其中 share/myprogram/
包含资源。 我的程序使用相对路径访问这些资源。可执行文件希望在 ../share/myprogram/
中找到它们。
我希望当我运行时:
mkdir build
cd build
cmake ..
make
发生以下情况:
- 编译
bin
目录 - 并将可执行文件放入
bin/
- 复制
share
build
目录中的目录
我正在寻找一种干净的方法来执行此操作。理想情况下,我希望 CMake 能够意识到资源作为资源。
我知道我可以使用复制自定义命令。这是实现这一目标的唯一方法吗?
奖励
如果使用 Xcode 生成器时资源可以出现在 Xcode 中的资源下,并且副本是 mybin 目标下的干净复制阶段,那就太棒了(这就是我所说的 CMake 意识到的意思)资源作为资源。)
更新:
到目前为止我所拥有的:
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/bin)
add_executable(mybin ${Headers} ${Sources})
add_custom_target(
Resources ALL
${CMAKE_COMMAND} -E copy_directory ${PROJECT_SOURCE_DIR}/share ${PROJECT_BINARY_DIR}/share
SOURCES ${Resources}
)
I have a project organized as follows:
include/
src/
share/myprogram/
where share/myprogram/
contains resources.
My program is accessing these resources using relative paths. The executable expects to find them in ../share/myprogram/
.
I would like when I run:
mkdir build
cd build
cmake ..
make
to have the following to happen:
- make a
bin
directory - compile and put the executable in
bin/
- copy the
share
directory in thebuild
directory
I am looking for a clean way of doing this. Ideally, I would like CMake to be aware of the resources as resources.
I know that I could use a copy custom command. Is this the only way to achieve this?
Bonus
If the resources could appear under Resources in Xcode when using the Xcode generator, and the copy be a clean copy phase under the mybin target, that would be awesome (and that's what I mean by CMake being aware of the resources as resources.)
Update:
What I have thus far:
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/bin)
add_executable(mybin ${Headers} ${Sources})
add_custom_target(
Resources ALL
${CMAKE_COMMAND} -E copy_directory ${PROJECT_SOURCE_DIR}/share ${PROJECT_BINARY_DIR}/share
SOURCES ${Resources}
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用Configure_file将文件从源目录复制到二进制目录。它有参数Copyonly。
You may use Configure_file for copying files from source dir to binary dir. it has parameter Copyonly.
我正在做一个 unix 命令行工具,我不是在做 Mac OS X 捆绑包,并且 Xcode 的资源文件夹仅用于捆绑包资源,所以忘记奖金吧。
我意识到我没有做正确的事情。
使用相对路径访问资源并不可靠,因为程序可以从任何地方执行。
我所做的是在文件夹层次结构中查找资源,从用户指定的位置开始,到环境变量,到相对目录,最后到标准 unix 目录。
所以实际上,构建过程中的复制阶段不再是必要的了。只有安装很重要,而且相当简单:
仅供参考,我保持了
custom_target
不变,因为我喜欢在 Xcode 中看到资源,并将其称为Resources
让它变得很漂亮:)I am doing a unix command line tool, I am not doing a Mac OS X bundle and Xcode's resource folder is only for bundle resources so forget the bonus.
I realized that I wasn't doing things correctly.
Using a relative path to access a resource is not reliable since the program can be executed from anywhere.
What I did is to look for resources in a hierarchy of folder, starting from user specified, to environment variable, to relative directories and finally to standard unix directories.
So actually, the copy phase during the build is not necessary anymore. Only the installation matters and that is fairly easy:
FYI, I kept my
custom_target
as is since I like having resources visible in Xcode, and calling itResources
makes it pretty :)