2

I am trying to integrate spdlog, a header only logging library into a C++ project but I am having trouble getting CMake to recognise the include paths properly. I am using the latest CLion with CMake 3.10.2 on Ubuntu 18.04.

My project structure looks like this: Project Dir |- libs | |- spdlog #this is the include directory taken straight from GitHub | |- src | |-... |- CMakeLists.txt In the CMakeLists.txt file I define the include directory:
include_directories("libs/")

Now when I attempt to write #include <spdlog/spdlog.h> in a header file located in src/ CLion complains that it cannot find spdlog.h even though I have checked and the file is definitely in the spdlog folder. Using quotation marks instead of angled brackets in the include statement does not fix the problem however using the path relative to the file (e.g. ../libs/spdlog/spdlog.h) works as it should. What is more confusing to me is that in the source file corresponding to the header I can include the file no problem.

I haven't been able to find anything like this issue anywhere and I'm struggling to understand what is causing CMake or CLion to behave like this.

EDIT: As per Matthieu Brucher's suggestion I have tried using fully qualified paths to the include folder but it still does not work. The problem seems to be that folders seem to not be recognised in headers, as they work in source files.

EDIT2: Here is the entire CMakeLists.txt I am using. It is nested into a different directory than the top level CMakeLists.txt for the entire project as this was the only solution I have found online to get something akin to VS's multiple projects in a solution in CLion. However since all paths are relative I don't think this would be a problem. I also know that spdlog is a C++11 library but I will need some C++14 features elsewhere in the near future.

cmake_minimum_required(VERSION 3.10 FATAL_ERROR)

project(TokenEngine VERSION 0.0.1 LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 14)

set(SOURCE_FILES src/Application.cpp src/Application.hpp src/EntryPoint.hpp src/Logger.cpp src/Logger.hpp)

include_directories("${CMAKE_CURRENT_SOURCE_DIR}/libs/")
add_library(TokenEngine SHARED ${SOURCE_FILES})

#Expose the public API of the engine to any project that might use it
target_include_directories(TokenEngine PUBLIC include)

EDIT3: When I attempted to recreate the error to show the full message given somehow the it was gone, being replaced by a different one totally unrelated to CMake...

TokenGuard
  • 29
  • 2
  • 5
  • Please, show **more code**: call for `include_directories`, `add_executable`, relative position between them and so on. Also, show the **exact error message** you got. With current wording, we can only suggest that *something* wrong with your code. – Tsyvarev Nov 13 '18 at 13:57

2 Answers2

0

You may want to use fully qualified paths:

include_directories(${CMAKE_SOURCE_DIR}/libs/)
Matthieu Brucher
  • 21,634
  • 7
  • 38
  • 62
  • Thanks for the answer! I have tried using fully qualified names and it still doesn't find the file. – TokenGuard Nov 13 '18 at 13:50
  • In that case, you need to provide more information, with a small example that reproduces the issue. Perhaps you put the include_directories after the add_subdirectory? – Matthieu Brucher Nov 13 '18 at 13:52
0

You also have to add the include folder to the include paths to be able to include via #include <spdlog/spdlog.h>. This should look like

include_directories(${CMAKE_SOURCE_DIR}/libs)

But I prefer using

target_include_directories(yourTarget PUBLIC ${CMAKE_SOURCE_DIR}/libs)

where yourTarget is the target where you want to use spdlog. Like that you will have the include directories also available if you are going to link your yourTarget to something else. If you need spdlog just in yourTargets cpp files you can change the PUBLIC to PRIVATE. See cmake doc.

FreshD
  • 2,914
  • 2
  • 23
  • 34
  • Thanks for the advice! The problem somehow is gone now seemingly without me changing anything. I gotta admit I am pretty new to CMake so it was probably me doing something extremely wrong. – TokenGuard Nov 13 '18 at 14:12
  • From your provided code you are using `include_directories(${CMAKE_CURRENT_SOURCE_DIR}/libs/)` but you have to use `include_directories(${CMAKE_CURRENT_SOURCE_DIR}/libs/include)`. Thats what I tried to explain in my answer. – FreshD Nov 13 '18 at 14:15
  • Ah sry, just read that you just copied the include directory. I updated my answer. – FreshD Nov 13 '18 at 14:18