2

I have a pretty big file structure of a project which I need to convert into a multiplatform cmake project. Now it seams that cmake requires ever single cpp file be added individually to the executable. But is there a script that automates this? That snoopes through the file structure and just adds every source file automatically? Since the project will probably get a lot more source files and I probably wont be able to manually add every single one.

5
  • 1
    file(GLOB ...) is your friend. Commented Aug 9, 2017 at 9:59
  • thanks, I'll look into it Commented Aug 9, 2017 at 10:22
  • 2
    Unfortunatley they explicitly state in the wiki that it should not be used to search for source files, since cmake doesn't rebuild the makefiles if no cmake files have changed. But I'll probably do it anyway and create a shellscript wich forces cmake to rebuild every time I add a file to the project. Thanks for your help Commented Aug 9, 2017 at 10:35
  • "I probably wont be able to manually add every single one" -- Why not? Commented Aug 9, 2017 at 12:23
  • It was supposed to be a case were I would just be able to create the CMake script once and then never think about it again for the most party. it was supposed to be possible to just create new files on the fly and only edit the files when new librarys were necessary. But it seams thats just not possible Commented Aug 9, 2017 at 13:40

1 Answer 1

3

You could use execute_process() with a cmake -P script that uses globbing to recursively scan for source files which writes to an included file in your CMakeLists.txt i.e. something like:

"CMakeLists.txt":

execute_process(COMMAND ${CMAKE_COMMAND}
  -D "RDIR=${CMAKE_CURRENT_SOURCE_DIR}"
  -P "scansources.cmake"
  WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}")
include("sources.cmake")
add_executable(myexe ${sources})

"scansources.cmake" (generates "sources.cmake"):

file(GLOB_RECURSE sourcelist
  *.c
  *.cc
  *.cpp
  *.cxx)
string(REGEX REPLACE "${RDIR}/" "" relative_sources "${sourcelist}")
string(REPLACE ";" "\n" sources_string "${relative_sources}")
set(sources_string "set(sources\n${sources_string})")
file(WRITE sources.cmake "${sources_string}")

The reason why this works is because execute_process() occurs at configure time.

You could, of course, generate sources.cmake via some other tool or IDE then you wouldn't need scansources.cmake or execute_process().

Sign up to request clarification or add additional context in comments.

1 Comment

so basicly set up a script that executes cmake everytime I want to compile my programm, followed by make -j4, and then have rescan the files. I know that cmake doesn't search them automaticly if you just cmake again on its own, globbing for the source files, but this might actually solve this, thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.