Lets say we have a project with only a main.cpp and we wanto to add foo.cpp:
The original CMakeList.txt is the following:
cmake_minimum_required(VERSION 3.6)
project(ClionProject)
set(CMAKE_CXX_STANDARD 11)
set(SOURCE_FILES main.cpp)
add_executable(ClionProject ${SOURCE_FILES})
Now we have to add foo.cpp
cmake_minimum_required(VERSION 3.6)
project(ClionProject)
set(CMAKE_CXX_STANDARD 11)
set(SOURCE_FILES main.cpp foo.cpp)
add_executable(ClionProject ${SOURCE_FILES})
So we changesd the line set(SOURCE_FILES main.cpp foo.cpp)
to add the .cpp
We can also add .h files in there.
BEWARE! ALL THE FILES SHOULD BE ON THE CMakeList.txt folder! if not, remember to add the path in there.
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
add_executable(ClionProject ${SOURCE_FILES} ${SOURCES})
In this example: ClionProject is actually the name of the project. SOURCES_FILES and SOURCES can be changed yo 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