Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CMake with include and source paths - basic setup

Tags:

c++

cmake

I'm trying to set up a test project looking like my own project just to get things working first and it looks like this:

/MainProject/inc/main.h /MainProject/src/main.cpp /LibProject/inc/test.h /LibProject/src/test.cpp 

I've found some tutorials, but I cant find out how to set up this when I have the inc and src folder? How would the CMakeLists.txt files look? Would I have one in /, one in each of the project folders? It seems like I dont need to have one in the inc and src folders?

like image 477
bitgregor Avatar asked Nov 29 '11 00:11

bitgregor


People also ask

How add include path CMake?

First, you use include_directories() to tell CMake to add the directory as -I to the compilation command line. Second, you list the headers in your add_executable() or add_library() call.

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.

How do I build from source CMake?

In order to build CMake from a source tree on Windows, you must first install the latest binary version of CMake because it is used for building the source tree. Once the binary is installed, run it on CMake as you would any other project.

What does Include_directories do in CMake?

Add include directories to the build. Add the given directories to those the compiler uses to search for include files. Relative paths are interpreted as relative to the current source directory.


2 Answers

You need a CMakeLists.txt for each source subdirectory. Your structure should look something like this:

root |-MainProject | |-inc | | '-main.h | |-src | | |-main.cpp | | '-CMakeLists.txt | '-CMakeLists.txt  |-LibProject | |-inc | | '-test.h | |-src | | |-test.cpp | | '-CMakeLists.txt | '-CMakeLists.txt '-CMakeLists.txt 

Content of root/CMakeLists.txt:

project(MyProject) add_subdirectory(MainProject) add_subdirectory(LibProject) 

Content of LibProject/CMakeLists.txt and MainProject/CMakeLists.txt:

add_subdirectory(src) 

Content of LibProject/src/CMakeLists.txt:

# Notice name prefix of this variable, set by CMake according # to value given with "project()" in the root CMakeLists.txt. include_directories(${MyProject_SOURCE_DIR}/LibProject/inc) add_library(LibProject test.cpp) 

Content of MainProject/src/CMakeLists.txt:

include_directories(${MyProject_SOURCE_DIR}/MainProject/inc) # I assume you want to use LibProject as a library in MainProject. include_directories(${MyProject_SOURCE_DIR}/LibProject/inc) link_directories(${MyProject_SOURCE_DIR}/LibProject/src) add_executable(MainProject main.cpp) target_link_libraries(MainProject LibProject) 

Then configure and build with:

$ cd root $ mkdir build $ cd build $ cmake .. $ make 
like image 71
Good Night Nerd Pride Avatar answered Sep 23 '22 03:09

Good Night Nerd Pride


You could do it like following.

  • CMakeLists.txt in your MainProject directory:

    project(MainProject)  add_subdirectory(LibProject/src) add_subdirectory(MainProject/src) 
  • CMakeLists.txt in your LibProject/src directory:

    include_directories(${PROJECT_SOURCE_DIR}/LibProject/inc/) add_library(LibProject test.cpp) 
  • CMakeLists.txt in your MainProject/src directory:

    include_directories(${PROJECT_SOURCE_DIR}/MainProject/inc/) add_executable(MainProject main.cpp) target_link_libraries(MainProject LibProject) 
like image 35
Roman Byshko Avatar answered Sep 25 '22 03:09

Roman Byshko