0

I'm having a problem with eclipse CDT.

There I am having a C++ project that uses the C FatFs library. I'm trying to implement the fatfs files. Question: In multiple files, I'm adding this wrapper:

#ifdef __cplusplus 
extern "C" 
{ 
#endif 

// code..

#ifdef __cplusplus
}
#endif

But for some reason, in the one .h file __cplusplus is defined, and in the other .h file __cplusplus is not defined.

Any suggestions?

9
  • 3
    @RobbeVanAssche: It's always better to copy-and-paste the actual code. Commented Jan 1, 2015 at 1:18
  • @Keith: Will do more example code in the future. But i like to keep it simple, so my application doesn't clutter the actual question.. Commented Jan 1, 2015 at 1:20
  • 2
    Or in fact a self-contained testcase. It baffles me that nobody does this. How can you debug your code without a testcase?!?! Commented Jan 1, 2015 at 1:20
  • 3
    @RobbeVanAssche: The best approach is not to show us your entire application. Just trim it down to a minimal test case that illustrates the problem, and copy-and-paste that into your question. Read this: sscce.org Commented Jan 1, 2015 at 1:23
  • 3
    @RobbeVanAssche: I don't think you understand what I meant by testcase. sscce.org You should not be debugging individual language features by examining the entire contents of your entire real-world application. That's just silly! Divide and conquer, my friend. Commented Jan 1, 2015 at 1:32

2 Answers 2

8

Whether __cplusplus is defined or not depends on how the file that includes the header is being compiled. If the file is being compiled as C source (.c) it will not be defined. if the file is being compiled as C++ source (.cpp, .cc, or any other extension associated as a C++ source file) then __cplusplus will be defined.

Double check the file extensions and if necessary the settings in your project to ensure that the files are being compiled correctly.

Sign up to request clarification or add additional context in comments.

3 Comments

But it is a header file. Does the compilation of the header file depends on whether it is included in a .c or .cpp file ? What happends when i include the header file in both a .c and a .cpp file?
Yes it matters. It's the .cpp or .c file that is the primary file being compiled and that's what IDE's and build systems look at to determine how to compile it.
Remember that an #include statement is simply bringing the content of the specified file into the content of the file that has the #include statement. So yes, if the header file is included by a .c file then the header code will be interpreted as C code, whereas if the header file is included by a .cpp or other C++ file then the header code will be interpreted as C++ code instead.
1

Look here: Combining C++ and C — how does #ifdef __cplusplus work?

extern "C" doesn't really change the way that the compiler reads the code. If your code is in a .c file, it will be compiled as C, if it is in a .cpp file, it will be compiled as C++ (unless you do something strange to your configuration).

What extern "C" does is affect linkage. C++ functions, when compiled, have their names mangled -- this is what makes overloading possible. The function name gets modified based on the types and number of parameters, so that two functions with the same name will have different symbol names.

Code inside an extern "C" is still C++ code. There are limitations on what you can do in an extern "C" block, but they're all about linkage.

Also, you probably want two #ifdef __cpluspluss:

#ifdef __cplusplus 
extern "C" { 
#endif 
    // ...
#ifdef __cplusplus
}
#endif

Otherwise, your C code will never see your definitions.

2 Comments

So example: i have header foo.h. I include header foo.h into foo.c and bar.cpp. In the first case, the header is processed by the c compiler and in the second case, the header is processed by the c++ compiler ?
It is the same compiler, just running in different modes. So yes, __cplusplus will not be defined when compiling foo.c by default because that file gets compiled in C mode instead of in C++ mode. Unless you force the compiler to compile it in C++ mode, which some compilers do provide an option for.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.