fheoosghalzr
fheoosghalzr

Reputation: 243

How to add existing source and headers file to the CLIon project

I am trying to add existing source files to my Clion project, but after adding (Copy and pasting) them to the project, these files were not added to the CMakeLists file. Also, the folder is semitransparent (gray colored).

How can I automatically add new files to the CMakeList ?

Upvotes: 19

Views: 27503

Answers (2)

Hana Kovačević
Hana Kovačević

Reputation: 1

I'm aware this question was posted 7 years ago, but I just encountered a similar problem. I followed the instructions on the tutorials and the answers given here, but I got strange errors. I managed to resolve this by manually editing the CMakeLists.txt file.

cmake_minimum_required(VERSION 3.23)
project(uni)

set(CMAKE_CXX_STANDARD 14)

add_executable(uni cmake-build-debug/theQuote.cpp)
#add_executable(uni main.cpp)

It's a silly solution, because each time I wish to run a new program, I have to edit it in the file, and comment out the one I don't wish to run. But it works for me.

Upvotes: 0

J Agustin Barrachina
J Agustin Barrachina

Reputation: 4090

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

Upvotes: 10

Related Questions