Redefinition complaints when using #pragma once

Answered

My c++ project builds fine. But CLion annoyingly underlines in red the variables and classes declared in my header files, with the tool-tip hover claiming that they are redefinitions.

The problematic header files look like:

// Foo.h
#pragma once

...

#include <FooINLINES.cpp>

with the corresponding FooINLINES.cpp looking like:

// FooINLINES.cpp
#include <Foo.h>

...

When I replace my `#pragma once` header guards with `#ifdef - #define - #endif` guards, these complaints disappear. Alternatively, if I remove the include at the top of the INLINES file, the complaints disappear. I'd prefer not to do either of these.

Can someone advise on how to resolve this issue? I am using CLion 2022.1.1 on Ubuntu, with built-in clangd 15.0.0.

0
6 comments

Hello!

Could you please explain why you need #include <FooINLINES.cpp> in your header file?

0

Foo.h contains function declarations, and FooINLINES.cpp contains the corresponding (inlined) function definitions. This is a common idiom, described for example here.

0

The mentioned documentation suggests to create a separate .inl file and include it in the header file: 

#include "Value.inl"

And please note that Value.inl doesn't include the header.

While in your case you include FooINLINES.cpp in Foo.h and at the same time include Foo.h in FooINLINES.cpp. Which is kind of recursion.

0

The #pragma once guard prevents the recursion.

As I note in my original post, changing the #pragma once to an #ifndef - #define - #endif guard fixes the issue. My complaint is that #pragma once should also work, but it doesn't.

The inclusion of Foo.h in the inlines file is for my own convenience, so that CLion's auto-complete and related features works properly when I am working on the inlines file.

0

Somehow, the problem appears to have disappeared on my end. Perhaps the CLion indexer was in some sort of corrupt state previously? I will comment back here if I am able to reproduce later.

0

Please sign in to leave a comment.