6
votes

I have a header-only library project. In my CMakeLists.txt I use INTERFACE library type

I wanted to import this project into CLion, but when I open any of the header files the IDE complains that this file does not belong to any project target

So is there a way to develop a header-only project in CLion?

Test project layout is pretty simple:

% tree foo
foo
├── CMakeLists.txt
└── foo.hpp

And CMakeLists content is

cmake_minimum_required(VERSION 3.8)
project(foo)

add_library(foo INTERFACE)
target_include_directories(foo INTERFACE ${PROJECT_SOURCE_DIR})
target_sources(foo INTERFACE ${PROJECT_SOURCE_DIR}/foo.hpp)

CLion 2017.2 + CMake 3.8

1
Using target_sources should solve the problem. I had the same problem and was able to resolve it by adding target_sources. I am using ${CMAKE_CURRENT_SOURCE_DIR} which resolves to the location where the CMakeLists.txt is located. I then specify the location relative to that directory, something like: ${CMAKE_CURRENT_SOURCE_DIR}/path/to/foo.hpp - lanoxx

1 Answers

2
votes

I had the same issue after upgrading my CLion. You're very close but need to add one more line:

add_library(target INTERFACE)
target_sources(target INTERFACE ${my_header_list})
target_include_directories(target INTERFACE ${CMAKE_SOURCE_DIR})

This may look different depending on your project layout. Mine looks like this:

|-- myLib
  |-- CMakeLists.txt
  |-- myLib
    |-- foo.hpp
    |-- bar.hpp
    |-- etc...

I got to this solution by reading this.