Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is extern "C" required in C files ? (to solve odd Eclipse behaviour)

Tags:

c++

c

gcc

eclipse

g++

I am relatively new to Mixed C/C++ applications using the gcc and g++ tools. To get the hand of it I have downloaded the iperf source code and built it with success using Eclipse.

There is one oddity: If I look into the file "Thread.c" using Eclipse it thinks there is a syntax error and underlines almost the whole file as if the code is flawed, which it is absolutely not.

This is the piece of code that triggers it:

#ifdef __cplusplus
extern "C"
{
#endif

I know what extern "C" is used for in C++ but Eclipse has problems with it when present in .c files. It happily accepts it in .cpp and .h files though.

I have checked out how the Make utility builds the iperf application and I see it always uses gcc to compile c files and g++ to compile cpp files which makes me wonder if extern "C" is actually required. It most definitely does not hurt because the gcc compiler happily deals with it but if it is not strictly required I could omit it and get better readability in Eclipse.

I have also looked at a possible solution for Eclipse's behaviour but have not really found one, if anybody can point out how to solve it that would be equally good.

like image 227
user3489567 Avatar asked Oct 23 '25 14:10

user3489567


1 Answers

If you are using Eclipse, what you seeing in editor has to do with Eclipse internal indexer/parser. Your mixed C/C++ project may compile and build with no issues, but when you open a source file which has

#ifdef __cplusplus
extern "C" {
#endif

you may see a syntax error marked on the extern "C" line. This means that "__cplusplus" symbol is defined for your C compiler in your project settings.

In order to get rid of that syntax error in the Eclipse editor, go to project Properties -> C/C++ General -> Paths and Symbols -> # Symbols tab, and select "GNU C" (or whatever C compiler you use) under "Languages" column. Scroll through Symbol/Value list on the right, find the "__cplusplus" symbol and delete it. Make sure you do that only for C compiler.

Click OK to close the project Properties window and the syntax error warning will be gone in Eclipse editor.

like image 92
nidalpres Avatar answered Oct 26 '25 04:10

nidalpres



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!