Suppose we have a project with only main.cpp, and we want to add foo.cpp: The source 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 need 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 change the set(SOURCE_FILES main.cpp foo.cpp) line set(SOURCE_FILES main.cpp foo.cpp) to add .cpp. We can also add .h files there.
BEWARE! ALL FILES MUST BE INCLUDED in the CMakeList.txt folder! if not, remember to add the path there.
There is also a way to make CLION to add cpp and h files (I donβt know why they donβt do this by default) and should add this line:
file(GLOB SOURCES *.h *.cpp )
as well as add_executable (ClionProject $ {SOURCE_FILES} $ {SOURCES})
In this example: ClionProject is actually the name of the project. SOURCES_FILES and SOURCES can be changed for you, whatever you wish.
Another good idea is to go to File β Settings β Build, Execution, Deployment β CMake and check "Automatically restart the CMake project when editing"
Here is a good starting tutorial: https://www.jetbrains.com/help/clion/2016.3/quick-cmake-tutorial.html
Agustin barrachina
source share