Search code examples
c++cmakecodelite

How can i include header files from a directory into cmake


I have a cmake file which generates my helloworld.workspace to work from. How do i get my cmake to include header files from a directory and external libraries example. -glfw3 like how i would normally use when using gcc directly. Note: i am on ubuntu. Any help is appreciated.

CMakeLists.txt

cmake_minimum_required (VERSION 3.5)

project (HelloWorld)

set (CMAKE_CXX_fLAGS "${CMAKE_CXX_FLAGS} -Wall -Werror -std=c++14")
set (source_dir "${PROJECT_SOURCE_DIR}/src/")

add_executable (HelloWorld ${source_files})

build.sh (To run cmake)

#!/bin/sh

cmake . -G "CodeLite - Unix Makefiles" -DCMAKE_BUILD_TYPE=Debug

My folder looks like this

-HelloWorldProject
   -CMakeLists.txt
   -build.sh
   -src
      -main.cpp
   -includes
      -(heres where i will put my .h and .cpp includes)

Solution

  • You need to tell CMake to add includes/to your program's include path. Usually this is done with either include_directories or target_include_directories

    add_executable (HelloWorld ${source_files})
    
    # Assuming this is meant to be a public directory
    target_include_directories(HelloWorld PUBLIC "includes/")