Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CLion and add_library(target INTERFACE)

Tags:

cmake

clion

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

like image 761
witosx Avatar asked Jul 20 '17 09:07

witosx


People also ask

What does add_library do in CMake?

To add a library in CMake, use the add_library() command and specify which source files should make up the library. Rather than placing all of the source files in one directory, we can organize our project with one or more subdirectories. In this case, we will create a subdirectory specifically for our library.

What is a CMake target?

In general, targets comprise executables or libraries which are defined by calling add_executable or add_library and which can have many properties set. They can have dependencies on one another, which for targets such as these just means that dependent ones will be built after their dependencies.

What is Cmakelist?

CMake is a meta build system that uses scripts called CMakeLists to generate build files for a specific environment (for example, makefiles on Unix machines). When you create a new CMake project in CLion, a CMakeLists. txt file is automatically generated under the project root.


1 Answers

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.

like image 108
moarCoffee Avatar answered Sep 23 '22 18:09

moarCoffee