-1

How do I test to see if I am linking in C++ or C using macros? Say I have code that should link as C in a C++ file, I would use extern "C"{//code here}which would make my code in a link as C . How would I set up my name.c file to work for both C and C++. Something like...

#ifdef C++//or other macro
extern "C"{
#endif

#ifdef C++
}
#endif

What is the proper macro to replace the "C++" I have above, and will it be cross platform? or how can I set it up to be cross platform?

Also, What is the significance of having to do the extern "C"{} for C code in a C++ file?

Any help is appreciated. Thanks in advance.

Alex Zywicki
  • 2,173
  • 1
  • 17
  • 32

1 Answers1

2

"What is the proper macro to replace the "C++""

It's #ifdef __cplusplus

#ifdef __cplusplus
extern "C"{
#endif

//.....

#ifdef __cplusplus
}
#endif

For "...What is the significance of having to do....?"

Read : In C++ source, what is the effect of extern “C”?

Community
  • 1
  • 1
P0W
  • 44,365
  • 8
  • 69
  • 114