我尽力添加了一个自定义cmake目标,该目标可以编译来自特定文件夹的.cpp文件列表,并为每个文件生成可执行文件,但没有成功。基本上我的情况如下:
foreach (FILE ${EXAMPLE_FILES})
add_executable(${TARGET_NAME} ${FILE})
endforeach ()
当前,当我键入时cmake .. && make
,一切正常,列表${EXAMPLE_FILES}
中的所有示例文件都将被编译,并为每个文件生成一个可执行文件。但是,我想从make all
目标中删除它,即,我只想在键入eg时生成那些可执行文件make examples
。任何想法如何实现这一目标?
通过添加EXCLUDE_FROM_ALL
关键字来add_executable
调用,可执行文件的构建将从中排除make all
。要使一个目标创建多个可执行文件,只需使该目标依赖于这些可执行文件即可。
# Create the custom target before loop.
add_custom_target(examples)
foreach (FILE ${EXAMPLE_FILES})
# Every executable target should have unique name.
# E.g. if every example source is given in form '<name>.c'
# Then we could use '<name>' as the executable target
string(REPLACE ".c" "" TARGET_NAME "${FILE}")
# Create an executable
add_executable({$TARGET_NAME} EXCLUDE_FROM_ALL ${FILE})
# And make 'examples' to depend from that executable
add_dependencies(examples ${TARGET_NAME})
endforeach ()
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句