如何使用CMake获取多配置生成器和基于Makefile的目标文件的路径? [英] How to get path to object files with CMake for both multiconfiguration generator and makefile based ones?
问题描述
我想基于所有内容生成一个模块定义文件目标文件中以动态方式提供的符号(例如 GTKMM 的 gendef )。
I would like to generate a module definition file based on all symbols available in object files in dynamic fashion (think of GTKMM's gendef).
为此,我想 add_custom_command
用于目标的 PRE_LINK
步骤。但是,似乎没有 easy 方法可以使用CMake来获取所有目标文件的路径,该方法适用于普通Makefile以及适用于Visual Studio的多配置生成器。
For this, I would like to add_custom_command
for PRE_LINK
step of a target. However, it looks like there is no easy way to get path to all object files with CMake that would work for plain makefiles as well as for multi-configuration generators like Visual Studio.
现在,我有以下
add_custom_command(TARGET tgt PRE_LINK
COMMAND gendef ${CMAKE_CURRENT_BINARY_DIR}/tgt.def $<TARGET_FILE_NAME:tgt> ${CMAKE_CURRENT_BINARY_DIR}/$<$<BOOL:${CMAKE_BUILD_TYPE}>:${CMAKE_FILES_DIRECTORY}>/tgt.dir/${CMAKE_CFG_INTDIR}/*.obj
)
但是,这很笨拙,我必须在我的计算机中使用生成器表达式意见。有没有更好的方法来达到这种效果,即为每个构建配置调用特定的外部程序?
However this is quite awkward and bulky as I have to use generator expression in my opinion. Is there a better way to achieve this effect, i.e. call a certain external program for each build configuration?
这是用于普通makefile的CMake错误(功能?),所有目标文件都移至 CMakeFiles / tgt.dir
文件夹,而对于多配置生成器,所有目标文件均移至CMakeFiles的兄弟,即 tgt.dir / $< CONFIG>
?我是否错过了一些简单的变量,可以直接将我指向正确的位置?
Is it a CMake bug (feature?) that for plain makefiles, all object files go to CMakeFiles/tgt.dir
folder while for multiconfiguration generators all goes to a sibling of CMakeFiles, i.e. tgt.dir/$<CONFIG>
? Did I miss some simple variable that would point me to the right place directly?
推荐答案
将我的评论变成答案
由CMake生成的Makefile项目的内部结构与为Visual Studio生成的解决方案/项目完全不同。我认为这既不是bug,也不是功能,这些结构只是针对用例进行了优化。
Makefile projects generated by CMake have a totally different internal structure then solutions/projects generated for Visual Studio. I think this is neither a bug nor a feature, those structures are just optimized for their usecases.
据我所知,没有简单的CMake内部方法来获取目标文件列表或中间文件目录的路径,例如
And as far as I know there is no easy CMake internal way to get the list of object files or the path to the intermediate files directory with e.g. reading a target property.
因此,我以您的代码示例为例,并使用 Visual Studio 14 2015使用CMake 3.3.2对替代方法进行了一些测试。
和 NMake Makefiles
生成器。
So I have taken your code example and have done some testing for alternatives with CMake 3.3.2 using Visual Studio 14 2015
and NMake Makefiles
generators.
替代方案
-
关于CMake邮件列表的一个相关讨论名为 CMake:是否有一种优雅的方法可以获取参与库的目标文件列表? 是否建议使用中间静态库:
One related discussion on the CMake mailing list named "CMake: Is there an elegant way to get list of object files participating into a library?" does suggest using an intermediate static library:
add_library(tgtlib STATIC tgt.c)
add_custom_command(
OUTPUT tgt.def
COMMAND gendef tgt.def $<TARGET_FILE_NAME:tgt> $<TARGET_FILE:tgtLib>
)
file(WRITE dummy.c "")
add_library(tgt SHARED dummy.c tgt.def)
target_link_libraries(tgt tgtlib)
您可以在 PRE_LINK
步骤中添加特定于构建环境的元素:
You could add build environment specific elements to your PRE_LINK
step:
if(CMAKE_CONFIGURATION_TYPES)
set(_obj_files "$(IntermediateOutputPath)*.obj")
else()
set(_obj_files "$?")
endif()
add_custom_command(
TARGET MainProject
PRE_LINK
COMMAND gendef tgt.def $<TARGET_FILE_NAME:tgt> ${_obj_files}
)
参考
- NMAKE: Filename Macros
这篇关于如何使用CMake获取多配置生成器和基于Makefile的目标文件的路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!