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.
Asked
Active
Viewed 1.9k times
3 Answers
25
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.

Fraser
- 74,704
- 20
- 238
- 215
-
1Thx. I am wondering if they didn't add smthg like add_doc() in the 2.8 version ? – Eric Jun 11 '12 at 08:09
-
@Eric Not as far as I know. You could try asking on the mailing list if there are any plans to do this. – Fraser Jun 11 '12 at 09:56
-
1What about interface library, e.g.: add_library(${PROJECT_NAME} INTERFACE README.md) it doesn't allow do it at all: "add_library INTERFACE library requires no source arguments" – Mykola Khyliuk Jul 01 '20 at 07:32
6
Instead of adding files which are not directly needed to build your library or executable, you can create a custom target to make these files appear in you IDE:
add_custom_target(myapp-doc
SOURCES readme.txt)

Florian
- 81
- 1
- 4
4
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.

Marek R
- 32,568
- 6
- 55
- 140