1

I'm having a problem with eclipse CDT. Where 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

#ifdef __cplusplus 
extern "C" 
{ 
#endif 

// code..

#ifdef __cplusplus
}
#endif

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

Any suggestions?
Can send screenshot for clarification.

  • I assume that in the real code you are clear about the number of underscores in __cplusplus? – Thomas Padron-McCarthy Jan 01 '15 at 00:56
  • Yes, i have an example where two different .h files are using the same code: __cplusplus. One is defined, one is not. – Robbe Van Assche Jan 01 '15 at 01:07
  • Do you really have `#ifdef __cplusplus` and `extern "C" {` on the same line? – Keith Thompson Jan 01 '15 at 01:15
  • No. Three lines. It would have been quicker if i could've linked an image in. – Robbe Van Assche Jan 01 '15 at 01:17
  • 2
    @RobbeVanAssche: It's always better to copy-and-paste the actual code. – Keith Thompson Jan 01 '15 at 01: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.. – Robbe Van Assche Jan 01 '15 at 01:20
  • 1
    Or in fact a self-contained _testcase_. It baffles me that nobody does this. How can you debug your code without a testcase?!?! – Lightness Races in Orbit Jan 01 '15 at 01:20
  • @Lightness: I can send you a .zip file with the eclipse project in. But i'm not sure you want to put your time into figuring out embedded stm32 stuff.. – Robbe Van Assche Jan 01 '15 at 01:22
  • 1
    @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: http://sscce.org/ – Keith Thompson Jan 01 '15 at 01:23
  • 1
    @RobbeVanAssche: I don't think you understand what I meant by _testcase_. http://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. – Lightness Races in Orbit Jan 01 '15 at 01:32
  • @Lightness: Thanks for one more abbreviation in my belt ;). No, seriously: i now understand what you mean. I'll try and do this in the future. Maybe good with an online compiler then ? It seems that eclipse has some strange bugs that occur from one time to another. Like that it doesn't recognize typedef bool.. That makes it hard to see if it was a manmade programming error or just a quirk.. – Robbe Van Assche Jan 01 '15 at 01:42
  • @RobbeVanAssche: This is a perfect example of the sort of quirk that can be very quickly isolated by making a testcase. Then in one feel swoop you've just divided your possible causes down to like 5%. And then we don't have to do that for you. :) – Lightness Races in Orbit Jan 01 '15 at 01:44
  • @Lightness: i found the quirk just now. Learned a lot by this thread. Thanks for everything! Ref: http://stackoverflow.com/questions/11866873/eclipse-cdt-c-enum-could-not-be-resolved?answertab=active#tab-top – Robbe Van Assche Jan 01 '15 at 02:01

2 Answers2

5

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.

Captain Obvlious
  • 19,754
  • 5
  • 44
  • 74
  • 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? – Robbe Van Assche Jan 01 '15 at 01:08
  • 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. – Captain Obvlious Jan 01 '15 at 01:10
  • 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. – Remy Lebeau Jan 01 '15 at 01:36
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.

L. F.
  • 19,445
  • 8
  • 48
  • 82
FoggyDay
  • 11,962
  • 4
  • 34
  • 48
  • 1
    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 ? – Robbe Van Assche Jan 01 '15 at 01:26
  • 1
    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. – Remy Lebeau Jan 01 '15 at 01:34