C++ CMake (add non-built files)

て烟熏妆下的殇ゞ 提交于 2019-11-27 17:08:00

问题


I am using cmake to configure my project. I visualize project's files using qtcreator which read the CMakeLists.txt. I have a few text files (non-code: config files, log, ..) and I would like to add them to my cmake project without (of course) compiling/linking them. Is it possible ? The main goal it to open them automatically in the tree of my project with qtcreator and edit them ... Thanks for help.


回答1:


You should be able to just add them to your list of sources in whichever add_executable or add_library call is appropriate and they will appear in the IDE.

I believe CMake uses the files' extensions to determine if they are actual source files, so if yours have extensions like ".txt" or ".log" they won't be compiled.




回答2:


Hi I've created this kind of function:

cmake_minimum_required(VERSION 3.5)
# cmake_parse_arguments needs cmake 3.5

##
# This function always adds sources to target, but when "WHEN" condition is not meet
# source is excluded from build process.
# This doesn't break build, but source is always visible for the project, what is 
# very handy when working with muti-platform project with sources needed
# only for specific platform
#
# Usage:
#      target_optional_sources(WHEN <condition> 
#                              TARGET <target>
#                              <INTERFACE|PUBLIC|PRIVATE> [items2...]
#                              [<INTERFACE|PUBLIC|PRIVATE> [items2...] ...])
##
function(target_optional_sources)
    set(options OPTIONAL "")
    set(oneValueArgs WHEN TARGET)
    set(multiValueArgs PUBLIC PRIVATE INTERFACE)

    cmake_parse_arguments(target_optional_sources 
                          "${options}" "${oneValueArgs}" "${multiValueArgs}"
                          ${ARGN})

    target_sources(${target_optional_sources_TARGET}
                   PUBLIC ${target_optional_sources_PUBLIC}
                   PRIVATE ${target_optional_sources_PRIVATE}
                   INTERFACE ${target_optional_sources_INTERFACE})

    if (NOT ${target_optional_sources_WHEN})

        set_source_files_properties(${target_optional_sources_PUBLIC}
                                    PROPERTIES HEADER_FILE_ONLY TRUE)
        set_source_files_properties(${target_optional_sources_PRIVATE}
                                    PROPERTIES HEADER_FILE_ONLY TRUE)
        set_source_files_properties(${target_optional_sources_INTERFACE}
                                    PROPERTIES HEADER_FILE_ONLY TRUE)

    endif(NOT ${target_optional_sources_WHEN})

endfunction(target_optional_sources)

On one hand it works as it is desired, on other hand some error is reported, so still working on that. Issu turn out to be problem how I used the function not how it is written. Now it works perfectly.



来源:https://stackoverflow.com/questions/10972577/c-cmake-add-non-built-files

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