0

I have created a project which consists of a main.cpp file and another class (named Physical) that is broken up into a Physical.hpp file and Physical.cpp file.

The project file structure looks like this:

main.cpp
header/Physical.hpp
src/Physical.cpp

The project compiles fine, but only if I include Physical.hpp in different ways depending on whether or not I'm including from the Physical.cpp or main.cpp file.

From the main file, I have to use:

#include "header/Physical.hpp"

Whereas, from the Physical.cpp file I have to use:

#include "../header/Physical.hpp"

Why is there this discrepancy? I expect it has something to do with my cmake configuration file, although I'm really not sure. Below is my cmake config file.

cmake_minimum_required(VERSION 2.8.4)
project(gravity_simulator ${PROJECT_BINARY_DIR})

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

set(SOURCE_FILES main.cpp src/Physical.cpp)
include_directories("${PROJECT_BINARY_DIR}")
add_executable(gravity_simulator ${SOURCE_FILES})

# Detect and add SFML
set(CMAKE_MODULE_PATH "/usr/share/SFML/cmake/Modules/" ${CMAKE_MODULE_PATH})
#Find any version 2.X of SFML
#See the FindSFML.cmake file for additional details and instructions
find_package(SFML 2 REQUIRED system window graphics network audio)
if(SFML_FOUND)
  include_directories(${SFML_INCLUDE_DIR})
  target_link_libraries(gravity_simulator ${SFML_LIBRARIES})
endif()
Alidaco
  • 171
  • 1
  • 8
  • You should try to add file(GLOB HEADER_FILES "${CMAKE_SOURCE_DIR}/header/*.hpp") to collect headers. And include_directories(${CMAKE_SOURCE_DIR}/header) to provide search path. Don't forget about your target and specify headers variable also add_executable(gravity_simulator ${SOURCE_FILES} ${HEADER_FILES}) – Alexander Borodulya Nov 27 '14 at 16:54
  • @AlexanderBorodulya Globbing for source or header files is frowned upon in CMake. See http://stackoverflow.com/a/18538444/393701 for an overview of the reasons why. Also, it is not needed to list header files, because CMake is able to detect include file dependencies automatically by analyzing source files. – SirDarius Nov 27 '14 at 17:00

1 Answers1

0

Everything works as expected.

For your main.cpp file the header is located at header/Physical.hpp while for your Physical.cpp the header is located at ../header/Physical.hpp.

The mistake here is, that you think the relative path would always start from a specific directly, while instead every source file (*.cpp) gets compiled independently and thus the header search is happening for each file independently.

Personally if I don't write a library, I simply put all my source and header files in a src directory and everything will be fine. However if I'm writing a library, I use the <header.hpp> style and add the header directory to the include directory (include_directories("header")). The whole reason why one usually splits up things into header and source files is, so when one ships a library one can easily just ship the headers and doesn't have manually filter them out between the source files.

tl;dr When writing an application put everything a source directory. When writing a library, use the <> for the inclusion add the header directory to the include directories.

Lukas
  • 2,461
  • 2
  • 19
  • 32