-1

I would like to include one header file in both C and C++, and I have a function defined in C code and few functions defined in external library.

#if defined(__cplusplus)
extern "C" {
#endif

void func0();

#if !defined(__cplusplus)
extern {
#endif

void func1();
int func2(int);

} /* extern */

This code produces compilation error when compiled from C source file

error C2059: syntax error : '{'

Is it possible to fix syntax error directly or I have to use some macros?

EXTERNCPP void func0();
EXTERNC void func1();
EXTERNC int func2(int);

Edit 1: I do not ask about Effects of the extern keyword on C functions, I just ask if it is possible to fix syntax in easy way. If it is not possible, i could still remove it completely for C part

Edit 2: To clarify what I want to get. If header is included

from C++:

extern "C" void func0(); 
extern "C" void func1(); 
extern "C" int func2(int);

from C:

void func0(); 
extern void func1(); 
extern int func2(int);
Community
  • 1
  • 1
Peter
  • 9,045
  • 4
  • 39
  • 65
  • Function declarations declare functions with external linkage by default in both C and C++. What are you trying to achieve? – CB Bailey Feb 05 '14 at 12:17
  • @CharlesBailey It's a matter of name mangling and calling conventions rather than of external linkage – SomeWittyUsername Feb 05 '14 at 12:35
  • @icepack: So he has that covered with the outer `extern "C"`. I don't understand what the intent of second (invalid) `extern {` construct is. – CB Bailey Feb 05 '14 at 12:42
  • What are you trying to do with `extern {`? Where's the matching close brace supposed to go? Which functions are supposed to have C language linkage? – CB Bailey Feb 05 '14 at 14:02
  • @CharlesBailey the header already had an "extern" defined for some functions and I am not perfectly convinced if it has/had a reason. The matching close brace should be same for extern "C" and extern, but there is a syntax error. – Peter Feb 05 '14 at 14:15
  • There's no difference between the declarations `void func1();` and `extern void func1();` in C (or C++) for that matter. They both declare functions with external linkage. – CB Bailey Feb 07 '14 at 00:27

3 Answers3

3

extern { is not needed.You need to remove it:-

#if defined(__cplusplus)
extern "C" {
#endif

void func0();

#if !defined(__cplusplus)

#endif

void func1();
int func2(int);

#if defined(__cplusplus)
}
#endif
4pie0
  • 28,488
  • 8
  • 76
  • 116
Vivek Sadh
  • 4,190
  • 2
  • 29
  • 47
2

You can simply omit the extern { line when compiling as a C header.

John Zwinck
  • 223,042
  • 33
  • 293
  • 407
1
#if defined(__cplusplus)
extern "C" {
void func0();
#endif

#if !defined(__cplusplus)
void func1();
int func2(int);
#endif

#if defined(__cplusplus)
}
#endif

* [EDIT] answering your last edit:

void func0(); /* included in both versions */

#if defined(__cplusplus)
extern "C" {
void func1(); 
int func2(int);
#endif

#if !defined(__cplusplus)
extern void func1(); 
extern int func2(int);
#endif

#if defined(__cplusplus)
}
#endif

If you want to use func0 as extern in C:

#if defined(__cplusplus)
extern "C" {
void func0();
void func1(); 
int func2(int);
#endif

/* C block */
#if !defined(__cplusplus)
extern void func0();
extern void func1(); 
extern int func2(int);
#endif

#if defined(__cplusplus)
}
#endif

If you don't want to use it at all (from C) remove it from the C block

David Ranieri
  • 37,819
  • 6
  • 48
  • 88
  • This header would not not declare func0 for C and func1 and func2 for C++. This is not equivalent to the behaviour with question's header. – Peter Feb 06 '14 at 08:58
  • No, declares func0 for c++ and func1, func2 for c, as you mention in your EXTERNC and EXTERNCPP requirements – David Ranieri Feb 06 '14 at 09:26
  • `EXTERNCPP void func0(); EXTERNC void func1(); EXTERNC int func2(int);` ? – David Ranieri Feb 06 '14 at 09:46
  • From C++: extern "C" void func0(); extern "C" void func1(); extern "C" int func2(int) – Peter Feb 06 '14 at 10:10
  • From C: void func0(); extern void func1(); extern int func2(int) – Peter Feb 06 '14 at 10:10
  • func0 has no extern "C" – Peter Feb 06 '14 at 10:48
  • `func0` is included in both versions without the `extern` keyword, do you understand the difference between `extern`(specifies that it has external linkage) and `extern "C"`(makes a function-name in C++ have 'C' linkage)? – David Ranieri Feb 06 '14 at 11:04