Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

cmake

clion

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 ?

like image 505
fheoosghalzr Avatar asked Nov 11 '15 14:11

fheoosghalzr


People also ask

How do I upload files to CLion?

In the Project tool window, right-click a file or folder, then select Deployment | Upload to from the context menu, and choose the target deployment server or server group from the list. If the default server or server group is appointed, you can also select Upload to <default deployment server or server group>.

How do I open a header file in CLion?

To invoke Go to Header/Source, press F10 or call Navigate | Header/Source from the main menu.

How do I add a header in CMake?

To include headers in CMake targets, use the command target_include_directories(...) . Depending on the purpose of the included directories, you will need to define the scope specifier – either PUBLIC , PRIVATE or INTERFACE .


1 Answers

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

like image 167
Agustin Barrachina Avatar answered Sep 17 '22 12:09

Agustin Barrachina