Header only classes in Clion
I've gotten used to the CMakeLists.txt based project management, even though I'm not crazy about it.
One thing bothers me a lot so far though. I write a lot of "header only" classes where there is no cpp file for implementation, it is all in the header. But I have not figured a way to make these show up as "project members" in the project tree view. Also, when I edit the hpp file, I see a warning "This file does not below to any project target, code insight features might not work properly". Is there a way to fix this? I don't have this problem when using Eclipse CDT.
Update: I discovered the target_sources command in CMake, which was apparently added to CMake partially to deal with just this issue. However, adding a target_sources(my target INTERFACE my header.hpp) statement does not make either problem above go away.
请先登录再写评论。
Hi, Andrew.
You can explicitly add header files to some CMake target. Please note that when you create new file in CLion (using the Project tool window), it automatically suggests you to add this file into one of the existing CMake target.
Actually starting from 2018.2, CLion automatically treats included files as project files. So if a header file is used in some project source file, it will be automatically considered as a part of the project by CLion.
BTW we have a quick CMake tutorial, which can be helpful: https://www.jetbrains.com/help/clion/quick-cmake-tutorial.html.
I'm confused, because this is not how the IDE is behaving, as indicated in my original post.
Perhaps it is because the header file is not in the project folder, or referenced by a cpp file in the project folder? Most of my source files are not in the project folder. The only way I know to make the header file "part of the project" is by using CMakeLists.txt. I did that with a target_sources statement as indicated in the post.
CLion seems to REALLY NOT CARE FOR source files not in the project folder. IMO, this is the single biggest issue I'm encountering with CLion. Everything is pretty good otherwise, but this is a really really big problem. I've searched for alternative products several time because of this. If there weren't major issues with code composition in Xcode and STL debugging in Eclipse Mac I would even be here.
It's very odd, because I can't imagine that any serious developer doesn't need to include source file which are located elsewhere into a project. I wish I understood what I am missing.
>Perhaps it is because the header file is not in the project folder, or referenced by a cpp file in the project folder?
A header file will be treated as a part of the project if you include the header file into any project file. Detailed are described in the blog post which was mentioned earlier: https://blog.jetbrains.com/clion/2018/06/clion-2018-2-eap-automatically-treats-included-files-as-project-files/.
>CLion seems to REALLY NOT CARE FOR source files not in the project folder.
Yes, in CLion non-project files are served with limited code insight.
For now any project in CLion should be encapsulated within the project directory (details in this response).
OK, I get it.
Do you know what the priority is for a fix for this issue with source files non within the project folder? It's a major issue for me going forward. Right now I'll figure out a workaround (I've read all the links provided), but it would be good to know if I can really settle in with CLion or keep me eye out for other IDE solutions.
Andrew, sorry for the late response. Recently we are doing a lot of rework in the project model subsystem of CLion (decoupling CMake and related changes). We plan to fix this issue as part of this rework, but I can't give you exact estimation, unfortunately. Not earlier then CLion 2019.1.