-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 Petrik
  • 9,701
  • 5
  • 41
  • 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 Petrik 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
  • 29,204
  • 9
  • 82
  • 118
Vivek Sadh
  • 4,230
  • 3
  • 32
  • 49
2

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

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
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
  • 39,972
  • 7
  • 52
  • 94