3

I have a folder structure like this: Project/Libraries/Math, Project/Libraries/Math2.

In the Project folder I have the main.cpp, and the CMakeLists.txt with the following content:

cmake_minimum_required (VERSION 2.6)
project (CppMain)
add_executable(CppMain main.cpp)

include_directories(${CMAKE_CURRENT_SOURCE_DIR))

In the Math folder I have the header MyVectors.h and in the Math2 folder I have MyMatrices.h, that I would like to include in the main.cpp file, and this works as:

#include "Libraries/Math/MyVectors.h"
#include "Libraries/Math2/MyMatrices.h"

The problem is that also the header MyVectors.h includes the header MyMatrices.h at the same way, but the linker doesn't find it. What can I modify in the CMakeLists to fix this problem?

charles
  • 713
  • 1
  • 6
  • 16

1 Answers1

1

This seems to be a classical case of relative paths. You have done this to include the files in main.cpp

#include "Libraries/Math/MyVectors.h"
#include "Libraries/Math2/MyMatrices.h"

In order for a file to access is parent directory you can use ../ The following lines should be introduced to MyVectors.h:

#include "../Math2/MyMatrices.h"

What does it signify? Well your file MyVectors.h is in Math folder when you use ../ it takes you to the parent directory of Math which is Libraries. From there you can simply follow the path to your required directory.

More detail can be found on this answer by me on another question: https://stackoverflow.com/a/35910234/2555668

Community
  • 1
  • 1
usamazf
  • 3,195
  • 4
  • 22
  • 40