How to add an existing source and header file to a CLIon project

I am trying to add existing source files to my Clion project, but after adding (copying and pasting) them to the project, these files were not added to the CMakeLists file. In addition, the folder is translucent (gray).

How can I automatically add new files to CMakeList?

+8
cmake clion
source share
1 answer

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

+1
source share

All Articles