Including extern c header results in broken preprocessor output

76 Views Asked by At

I have a source code structured as follows

foo.h (header from c library)

#pragma once
struct foo {...};

bar.h

#pragma once
extern "C" {
   #include "foo.h"
}
class Bar {
  foo val;
  ...
};

bar.cpp

// some Bar methods implementation

user.cpp

#include "bar.h"
...
extern "C" {
   #include "foo.h"
}
...
/* some additional foo usage */

What I expect to happen is that if foo.h included multiple times, #pragma once shall make it so it's included first time only. But what happens is that in bar.h it is not included at all, and include works in user.cpp only. So preprocessor output of user.cpp.i looks somewhat like that:

extern "C" {/* nothing is here, literally empty braces */}
class Bar {
  foo val;
  ...
};
extern "C" {/*actual header content*/}
...
/*the rest of user.cpp content*/

I'd expect foo.h pasted at very first include, but what happens is that struct foo is undefined in bar.h but defined in user.cpp.

I can solve the issue for now by reordering the includes, but why might that happen at all?

UPDATE:

And rearranging includes does nothing. Since bar.cpp still gets no include added by preprocessor.

1

There are 1 best solutions below

0
Roman On

So. My set up was: Windows + Wsl. Source code lying on windows host and compiled on wsl. So I was writing with linux in mind and didn't notice that I ended up having c_functionality.h and C_FUNCTIONALITY.h (second one was supposed to be wrapper).

As far as I recall, that wouldn't be an issue on most linux FS'es, since most widely used of them do support case. BUT! Source code is on ntfs.

And GCC won't tell you that you have multiple headers with conflicting names. But clang will.