Cmake - creating of a new class and deletion of class, method are not handled properly


Please see the included Cmake file below, when non standard source root is used the Clion is not handling addition and deletion of the classes, methods, etc. It works more or less after Cmake refresh, but not as good as default config. Tested on Clion EAP-2018-3.



cmake_minimum_required(VERSION 3.12)


file(GLOB_RECURSE sources src/main/cpp/*.cpp src/main/cpp/*.h)
file(GLOB_RECURSE data src/main/resources/*)
file(COPY ${data} DESTINATION resources)

add_executable(CppTest-1 ${sources} ${data})

#IF (Boost_FOUND)
# include_directories(${Boost_INCLUDE_DIR})
#endif ()

#set(Boost_USE_STATIC_LIBS OFF) # enable dynamic linking
#set(Boost_USE_MULTITHREAD ON) # enable multithreading

set(INSTALL_DIR "$ENV{HOME}/Documents/workspace/CLion/CppTest/install/cpp-test-1")
install(DIRECTORY src/main/resources DESTINATION ${INSTALL_DIR}
PATTERN "resources/*"
## now comes everything we need, to create a package
## there are a lot more variables you can set, and some
## you need to set for some package types, but we want to
## be minimal here
## we don't want to split our program up into several things


Hi, Vladimir! Not sure that I got the issue correctly.

So there are two directories with project sources - src/main/cpp and src/main/resources. Which directory is your project root in CLion (i.e. in which directory the .idea folder is located)? And where and how do you try to create a class? What happens when you do?



Here is the git link to the sample project including .idea folder

I'm Creating  a new cpp file using CMD+N or CTRL+Insert shortcut and selecting "C++ Class", I'm not adding classes to the target since all .cpp and .h files files are part of the target already, I try to select the checkbox "Add To Target" but then files a literally added and this not what we want.






> I'm not adding classes to the target since all .cpp and .h files files are part of the target already

The New > C++ class option is intended for creation of two (or only one, it depends on the Create header only checkbox) new files from templates - the source and header files for the new class. Please see for more details.

Are you saying that you don't want a ".cpp, .h" pair of files to be created? You want to create a new class in an already existing file, don't you?


I'm fine with the creation of the file/files. The problem is that newly created file (let's say abc.h) is not recognizable by the Clion IDE until Cmake refresh is performed by clicking the refresh button or menu item.

The default project that Clion creates works properly and automatically traces the newly created files, classes and methods. But in my sample Cmake need to be explicitly refreshed in order for changes to be synced with the Clion.




I think I finally got this (that by "since all .cpp and .h files files are part of the target already" you mean that GLOB_RECURSE using in the file() commands), thank you for the details. 

Yes, for now CLion doesn't reload a CMake project automatically if you don't select the "Add To Target" option (and therefore the CMakeLists.txt is not changed).

Feel free to comment or upvote


May Clion should check that if add to target checkbook is checked and if the file is already part of the target based on some other rule then it should not add it again, but at least refresh the project to be in sync. Added my few cents to the youtrack


Thank for your help.




Vladimir, thanks for the comment!


Please sign in to leave a comment.