0

I have a library written by a not very meticulous coder, which includes the following code in a C header file:

/* SomeCHeaderFile.h */
...
#define local static  
#define package       
#define global        

When this file is included in a C++ header, for example: EDIT: I forgot to mention that I include the header like so:

// SomeCPlusplusSourceFile.cpp
extern "C" {
   #include "SomeCHeaderFile.h"
}
...

the compiler gives the following error:

error: constructor cannot be static member function
error: 'std::locale::locale(const std::locale&)' cannot be overloaded
error: with 'std::locale::locale(const std::locale&)'

I only say the coder was not meticulous because she/he never tested it in a C++ code. But I don't know the reasoning behind this to why is this causing the build to fail?

gcc version 4.4.3 | ubuntu linux

ArmenB
  • 2,125
  • 3
  • 23
  • 47
  • 3
    The compiler isn't "crashing", it's given you a straightforward error message. Note that `static` is a keyword and I think your program code is using it incorrectly. It looks like you're creating your own keywords, please don't do this. – Dai Apr 11 '14 at 01:34
  • Where is code, where you are using all these #defines – Pranit Kothari Apr 11 '14 at 01:34
  • Your right, the title was horribly wrong! – ArmenB Apr 11 '14 at 01:35
  • 1
    So many people indicate C and C++ are totally different languages, so why should there be any concern that a C header does not work in C++? You can't have it both ways.... – Jiminion Apr 11 '14 at 01:54
  • I think it is good practice to include all standard headers before any user-defined headers, just to avoid things like that. However, I also think that the Standard mandates that headers only use reserved tokens, so perhaps this could be a compiler bug. For interest's sake, can you show the original code in the header file that is throwing the error? – M.M Apr 11 '14 at 04:07

2 Answers2

2

It seems the troublesome C header file is redefining tokens in use by the standard C++ header files. If you want to use the C header file in your C++ code, you may be required to include it after the standard header files to prevent this kind of problem.

This may not sufficiently guard you from problems if the C++ code defines its own macros that also redefine the same tokens. If that happens, you will have to segregate your C++ code with files dedicated to C++ that uses the troublesome C header file and C++ code that does not. The C++ code that does use the troublesome C header file makes sure to not use any C++ header file that would cause problems.

jxh
  • 69,070
  • 8
  • 110
  • 193
2

You can use C header files in CPP files as follows:

extern "C" {

//Headers go here

}

More details here:

In C++ source, what is the effect of extern "C"?

Community
  • 1
  • 1
Prabhu
  • 3,443
  • 15
  • 26
  • unfortunately I cannot modify the header file, however I forgot to mention that I do include the header like so `extern "C" { #include "SomeCHeaderFile.h" }` – ArmenB Apr 13 '14 at 19:27