C++ 在 CLion 中,仅标头库:文件“不属于任何项目目标,代码洞察功能可能无法正常工作”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46245738/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 15:40:56  来源:igfitidea点击:

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

c++cmakeclionheader-only

提问by xaxxon

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

我有一个使用 cmake 命令设置的仅头文件库项目:

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?

什么是设置它以便 CLion 在仅标头库上提供其常用功能的正确方法?

回答by sjaustirni

Little background

小背景

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

我遇到了同样的问题,尽管该项目不是仅标题,但是inc,即使 CMake 文件清楚地将该文件夹标记为include_directory.

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

*.hpp 文件不属于 ${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.

由于这是一个完全有效的 CMake 文件,并且将包含文件添加到源文件不是惯用的,因此我不想修改 CMake 文件。

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.

如官方JetBrains 论坛所述,CMake 文件确实有效,并且由于 CLion 无法正确索引头文件而显示警告。从链接中提取的建议解决方法是右键单击该文件夹,然后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.

因此,此标头不包含在可执行文件中,并且 CLion 会通知您某些代码洞察功能可能无法正常工作。作为解决方法,您可以使用“将目录标记为”库文件/项目源和文件夹的标题。

回答by Valdemar

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.

Clion 从 CMake 构建系统获取有关源文件的信息。当您将任何 cpp 文件添加到源列表时,CMake 会自动告知具有相同名称的标题。因此,如果 cpp/h 名称不同(或者您根本没有 cpp 文件),您应该手动包含标题。

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

如果你没有任何可执行文件,你可以省略最后一行,CLion 仍然会知道文件

回答by Azurespot

You can add the header files to your project like this:

您可以像这样将头文件添加到您的项目中:

set(SOURCE_FILES main.cpp MyClass1.cpp MyClass1.h MyClass2.cpp MyClass2.h)

You can also set it in multiple steps like so:

您还可以通过多个步骤进行设置,如下所示:

set(SOURCE_FILES main.cpp)
set(SOURCE_FILES ${SOURCE_FILES} MyClass1.cpp MyClass1.h)
set(SOURCE_FILES ${SOURCE_FILES} MyClass2.cpp MyClass2.h)

Though as mentioned in the comments, you probably shouldn't be adding the header files to your project at all.

尽管如评论中所述,您可能根本不应该将头文件添加到您的项目中。