Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How locate .cpp Files in a "src directory" using CMake?

Tags:

cmake

I am learning c++ and cmake at the moment. I have my source files in the main directory where CMakeLists.txt is located. I want to store all source files in a /src directory, but i have no idea how to locate them in CMake.

My CMake File

cmake_minimum_required(VERSION 2.8)
project(game)

set(GAME_ALL_SOURCES
    main.cpp check.cpp
)

add_executable(game ${GAME_ALL_SOURCES})
target_link_libraries(game sfml-graphics sfml-window sfml-system)

Anyone a suggestion how to handle it?

best regards

like image 480
user2664310 Avatar asked Aug 13 '13 15:08

user2664310


People also ask

Where does CMake look for include files?

cmake is searched first in CMAKE_MODULE_PATH , then in the CMake module directory. There is one exception to this: if the file which calls include() is located itself in the CMake builtin module directory, then first the CMake builtin module directory is searched and CMAKE_MODULE_PATH afterwards.

What is the source directory in CMake?

The source directory is where the source code for the project is located. This is also where the CMakeLists files will be found. The binary directory is sometimes referred to as the build directory and is where CMake will put the resulting object files, libraries, and executables.

Where is CMakeLists TXT located?

The CMakeLists. txt file is at the top directory of the ROOT git repository.

What does Add_subdirectory do in CMake?

Add a subdirectory to the build. Adds a subdirectory to the build. The source_dir specifies the directory in which the source CMakeLists.


2 Answers

If you want to locate all .cpp files in the src directory, you could do

file(GLOB SOURCES src/*.cpp)

and use ${SOURCES} wherever you need to. For example:

add_executable(game ${SOURCES})
like image 151
Nafis Zaman Avatar answered Sep 21 '22 15:09

Nafis Zaman


Try

set(GAME_ALL_SOURCES
src/main.cpp src/check.cpp
)
like image 43
newman Avatar answered Sep 17 '22 15:09

newman