By fferri

2019-03-11 10:22:42 8 Comments

I have a C header as part of a C++ library.

This C header would only make sense compiled by a C compiler, or by a C++ compiler within an extern "C" { ... } block, otherwise unresolved link errors would happen.

I thought to add a block such as:

#ifdef __cplusplus
#error "Compiling C bindings with C++ (forgot 'extern \"C\"'?)"

in the C header, but unfortunately the __cplusplus macro is defined also within an extern "C" { ... } block.

Is there another way to detect this condition correctly?


@StoryTeller 2019-03-11 10:27:03

The common practice is not to demand client code wraps your header in extern "C", but to do so conditionally yourself. For instance:

#ifdef __cplusplus
extern "C" {

  // Header content

#ifdef __cplusplus

That way client code is automatically correct without doing anything beyond including the header.

@pmg 2019-03-11 11:14:06

I don't know C++ ... Is there any issue with nested extern "C" { /* ... */ extern "C" { /* ... */ } /* ... */ }

@StoryTeller 2019-03-11 11:15:12

@pmg - Not in C++. If you nest language linkage specifications, the innermost "wins". The macro is just to prevent a C compiler from rightfully rejecting the extern "C".

@Rakete1111 2019-03-11 11:36:27

Such a shame that C doesn't support extern "C"...

@StoryTeller 2019-03-11 12:00:40

@Rakete1111 - We shouldn't throw stones from our glass house that has no restrict qualifier :P

@Joshua 2019-03-11 15:44:33

@StoryTeller: You mean you haven't thrown right through restrict yet?

@Señor CMasMas 2019-03-11 16:09:05

Such a shame that C doesn't support extern "C"? This makes zero sense to me. Doesnt the keyword tell C++ to not use name mangling for the C symbols?

@StoryTeller 2019-03-11 16:21:09

@SeñorCMasMas - It also tells it to adhere to a C calling convention (those may differ). And the point that was made is that it'd be nice if C compilers didn't reject the syntax outright, but instead treated it as a no-op. So the macro business won't be needed.

@jxh 2019-03-11 22:08:48

@StoryTeller: Well, the syntax is strange. I don't think there is much surprise that C doesn't want to support it. C++ could have adopted a new keyword, e.g. _C_extern or something like that, and it would have been easier for C to treat that as a synonym for extern (maybe with a macro...), and then support the extern{} grouping syntax.

@vsz 2019-03-12 06:19:29

@StoryTeller : It would then probably break old code which used it as a function or variable name...

@RiaD 2019-03-12 08:43:57

names with _Capital are not allowed to use anyway

@StoryTeller 2019-03-12 08:53:19

@RiaD - I don't know to which of my comments you are referring to. I don't believe I touched on reserved identifiers at any point.

@joH1 2019-03-12 09:17:04

@StoryTeller I believe RiaD was replying to vsz's comment about breaking old code. I'd like to add that one can definitely use identifier with leading underscore and capital, but at their own peril, because doing this produces non-compliant code that may be broken in the future.

@Angew 2019-03-12 10:42:24

@jxh Language linkage supports more than just "C" and "C++"; compilers are free to support any toerh language linkage specification. I'd say using a string literal for this is better than introducing a new keyword for each.

@jxh 2019-03-12 17:48:35

@Angew: That's an opinion I don't happen to share. An alternative syntax could have involved using a #pragma, that would have been ignored by a C compiler.

@jxh 2019-03-12 17:49:12

@RiaD: My suggestion was for the language to make it a standard.

@RiaD 2019-03-12 18:46:36

as joH1 said my comment was to vsz

Related Questions

Sponsored Content

10 Answered Questions

[SOLVED] Improve INSERT-per-second performance of SQLite?

12 Answered Questions

[SOLVED] How can I profile C++ code running on Linux?

  • 2008-12-17 20:29:24
  • Gabriel Isenberg
  • 452629 View
  • 1614 Score
  • 12 Answer
  • Tags:   c++ unix profiling

13 Answered Questions

[SOLVED] What is the effect of extern "C" in C++?

16 Answered Questions

[SOLVED] How do I use extern to share variables between source files?

23 Answered Questions

[SOLVED] Why do we need virtual functions in C++?

32 Answered Questions

4 Answered Questions

[SOLVED] Combining C++ and C - how does #ifdef __cplusplus work?

19 Answered Questions

1 Answered Questions

Sponsored Content