16

This seems really basic. How can i add files to a project without having to manually edit the CMakeLists.txt.

For example source files in another directory

dev-masih
  • 4,188
  • 3
  • 33
  • 55
jkj yuio
  • 2,543
  • 5
  • 32
  • 49

4 Answers4

8

CLion parses the CMakeLists.txt and uses it to generate a project view, but I believe the only way to add files to the project is to edit the CMakeLists.txt to include those files. I expect that eventually this will change similar to the way IntelliJ integrates with a pom.xml file in a Java project, but for now you edit the CMakeLists.txt.

legalize
  • 2,214
  • 18
  • 25
  • 2
    +1 You really don't want clion editing your `CMakeLists.txt`. You can look into `file(GLOB ...)` and `file(GLOB_RECURSE ...)` to automatically find newly added source. But you'll have to force CMake to be re-run in order for them to be added. – kmac Nov 21 '15 at 18:06
  • @kmac I'm not a fan of the `GLOB` approach, particularly if you have platform specific sources and you want to conditionally add them to the project. For simple projects, it's fine, but I prefer adding the files explicitly. – legalize Nov 21 '15 at 20:13
  • Yes, this tends to be a hotly debated subject. I think that it is a very useful tool, though not always appropriate. Even in complex projects, if components are broken down into smaller libraries (or even source sub-folders), GLOB can still work well if used judiciously. We're happily working away like this on a project with 400k+ lines of code -- no regrets :). – kmac Nov 22 '15 at 05:25
5

There is also a way to make CLion to add any cpp and h files (I don't know why don't they do it by default) and is to add this line:

file(GLOB SOURCES
    *.h
    *.cpp
)

and also edit the line of:

add_executable(ClionProject ${SOURCE_FILES} ${SOURCES})

In this example: ClionProject is actually the name of the project. SOURCES_FILES and SOURCES can be whatever you want.

Another good idea is to go to File -> Settings -> Build, Execution, Deployment -> CMake and tick on "Automatic reload CMake project on editing"

Here is a good starting tutorial: https://www.jetbrains.com/help/clion/2016.3/quick-cmake-tutorial.html

J Agustin Barrachina
  • 3,501
  • 1
  • 32
  • 52
0

No other option. You have to edit the CMakeLists.txt. CLion is completely cmake based IDE. Even if you need to link external libraries, you need to edit the above-mentioned file. It doesn't work like in GUI based code-blocks for example.

vbenara
  • 81
  • 1
  • 5
-1

How about right clicking the CMakeLists.txt editor tab, clicking "Open in -> terminal", typing ls or find or whatnot, and copy-and-pasting from there?

Kees-Jan
  • 518
  • 4
  • 16