Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid adding every source file to CMake in CLion?

Tags:

clion

Most people that switched from some other IDE that had an option to build single file are a bit confused by CLion which doesn't happen to have an option.

So how can you avoid adding every name of every source file into CMake?

like image 503
Dusan J. Avatar asked May 05 '16 10:05

Dusan J.


2 Answers

What you are looking for is something like file(GLOB_RECURSE *.cpp). You can customize this command to filter out directories, if you need to.

like image 153
Dmitri Nesteruk Avatar answered Nov 03 '22 21:11

Dmitri Nesteruk


I had the same problem with c++ and CMake using CLion. And this how I managed to solve it:

Here is my project structure :

├── CMakeLists.txt
├── README.md
└── src
    └── ch01
        ├── ex01
        │   └── main.cpp
        └── ex02
            └── main.cpp

Here are the contents of CMakeLists.txt :

cmake_minimum_required(VERSION 3.5)

project(CPPTutorial)

set(CMAKE_CXX_STANDARD 14)

set(sourceDir "${PROJECT_SOURCE_DIR}/src")

file(GLOB_RECURSE sourceFiles "${sourceDir}/*.cpp")

FOREACH (sourceFile ${sourceFiles})
    MESSAGE(STATUS "Process file: ${sourceFile}")

    get_filename_component(dir1 ${sourceFile} PATH)
    get_filename_component(dir1 ${dir1} NAME)

    get_filename_component(dir2 "${sourceFile}/../../" ABSOLUTE)
    get_filename_component(dir2 ${dir2} NAME)

    MESSAGE(STATUS "New target: ${dir2}_${dir1}")
    add_executable("${dir2}_${dir1}" ${sourceFile})
endforeach (sourceFile)

It creates a new target for each source in the directory .. actually it needs more work but it does the job for me.

Restrictions:

  1. One source file for each sub sub directory
  2. the structure needs to be the same as AAA/BBB where AAA is a parent directory and BBB is it's sub directory.
  3. You can add more AAA in the same level and also more BBB

Hope it helps

like image 39
abd3lraouf Avatar answered Nov 03 '22 22:11

abd3lraouf