How to add existing source and headers file to the CLIon project
Asked Answered
R

2

19

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 ?

Rafaelrafaela answered 11/11, 2015 at 14:51 Comment(4)
Possible duplicate of CMake - Automatically add all files in a folder to a target?Cuddy
How is this related to C and C++?Ailssa
Try and keep C separated from C++ language. Despite their likeness, they're different languages, each with its own, different, ups and downs.Roue
Possible duplicate of #33628326Christ
L
10

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

List answered 4/2, 2017 at 18:24 Comment(4)
How on god's earth did we go from Visual Studio, Delphi, and CodeWarrior to this pathetic excuse for an IDE? Hand configuration is tantamount to going back to the dark ages of software development.Culex
Not really. Keeping the config in files (like cmake) makes your build reproducible across platforms, instead of locked in your IDE's proprietary config format. It's the same idea as infrastructure as code.Calorific
@Culex I was thinking the same! 1 step forward, 2 steps back... All in the name of cross platform.Empathize
@ALT_DEV I used Visual Studio for a long time. I have code I do not know anymore how to compile on Linux for example. CLion helped me a lot. The Cmake I explained was used to compile my code (written in VS) into Linunx. I used CLion for Linux and as I liked it more than VS I now moved compleately.List
Q
0

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.

Quizzical answered 6/4, 2023 at 9:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.