Can one add further source files to an executable once defined?

杀马特。学长 韩版系。学妹 提交于 2020-12-27 12:01:24

问题


Given I have defined an executable with its main source file in a CMakeList.txt file:

ADD_EXECUTABLE(MyExampleApp main.cpp)

Can I add further source files to this executable after this line but in the same or an included CMakeList.txt file?


回答1:


Use target_sources, available since cmake 3.1

eg. target_sources(MyExampleApp PRIVATE ${extra_file})

https://cmake.org/cmake/help/v3.1/command/target_sources.html




回答2:


I think you may use:

add_executable(MyExampleApp main.cpp)
add_library(library STATIC ${ADDITIONAL_SOURCES})
set_target_properties(library PROPERTIES
     LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
target_link_libraries(MyExampleApp library)



回答3:


It should be noted that for more recent versions of CMake (> 3.1 I think) one can append files to the SOURCES property on targets.

http://www.cmake.org/cmake/help/v3.3/prop_tgt/SOURCES.html



来源:https://stackoverflow.com/questions/9339851/can-one-add-further-source-files-to-an-executable-once-defined

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!