Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In CLion, header only library: file "does not belong to any project target, code insight features might not work properly"

I have a header-only library project set up with the cmake command:

add_library(my_library INTERFACE) 

and I also added

target_sources(my_library INTERFACE ${MY_LIRBARY_HEADER_FILES}) 

but when I open a source file, I get the warning:

This file does not belong to any project target, code insight features might not work properly

and I lose a lot of the functionality on things like code completion.

What is the proper way to set this up so CLion provides its usual functionality on a header-only library?

like image 405
xaxxon Avatar asked Sep 15 '17 18:09

xaxxon


People also ask

How do I add a target project to CLion?

Click File | Open, point CLion to compile_commands. json, and choose Open as Project. to add a new target. This dialog is also accessible from the Custom Build Application editor.

How do I add a library to CLion?

Since CLion relies on CMake build system, you can do this with CMake commands. To add libraries to your project, use find_package (if you use separate libraries, for example, installed in the system) and target_link_libraries CMake commands.

How do I open a header file in CLion?

Go to Header/Source To invoke Go to Header/Source, press F10 or call Navigate | Header/Source from the main menu.


2 Answers

Little background

I was having the same problem, albeit the project was not header-only, nevertheless, the open files from inc folder were throwing the aforementioned warning, even though the CMake file clearly marked that folder to be include_directory.

*.hpp files do not belong to ${SOURCE}

include_directories("${PROJECT_SOURCE_DIR}/inc/") add_subdirectory(src) add_executable(${EXECUTABLE_NAME} main.cpp ${SOURCE}) 

Since this is a perfectly valid CMake file and adding the include files to source files is not idiomatic, I did not want to amend the CMake file.

The solution

As described on the official JetBrains Forum, the CMake file is indeed valid and the warning is shown because of the inability of CLion to properly index header files. The suggested workaround extracted from the link is to right-click the folder and Mark directory as | Library Files/Project Sources and Headers.

So, this header isn't includes in executables and CLion notifies you that some code insight features might not work properly. As workaround you can use "Mark directory as" Library Files/Project Source and Headers for folder.

like image 147
sjaustirni Avatar answered Sep 24 '22 13:09

sjaustirni


Clion takes information about source files from CMake build system. When you add any cpp file to sources list CMake automatically tell about header with same name. So if cpp/h names differs (or you don't have cpp file at all) you should include header manually.

set(Sources my_lib.cpp) set(Headers header_of_my_lib.h) add_executable(superlib ${Sources} ${Headers}) 

If you don't have any executable you can omit last line, CLion will still know about files

like image 36
Valdemar Avatar answered Sep 22 '22 13:09

Valdemar