Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++: extern and inline functions

Tags:

c++

c

inline

I have a couple of files written in C, and I want them to be C++-compatible, so for my C headers I use;

#ifdef __cplusplus
extern "C" {
#endif

at the beginning of the file and of course

#ifdef __cplusplus
}
#endif

...at the end. But it seems to create problems with the 'inline' keyword. My solution is to simply remove the inline keyword for C++, but I guess it could have a bad effect on C++ programs (these functions are called gazillions of times).

Is there a better solution ?

like image 812
Suugaku Avatar asked Nov 25 '22 19:11

Suugaku


1 Answers

If I understand correctly, I would do:


 #ifdef __cplusplus
 #define D_INLINE static
 extern "C" {
 #else
 #define D_INLINE inline
 #endif

And use the D_INLINE for the functions that I think should need inline. As delnan said, the compiler will optimize it anyway and the inline keyword is just a hint to the compiler that the programmer thinks that the compiler should inline the function. It doesn't force the compiler to inline the function.

like image 198
Simon Avatar answered Dec 19 '22 01:12

Simon