views:

376

answers:

7

(A case of over relying on an IDE)

I have some legacy C code that I compile as C++ for the purpose of Unit testing. The C source is C++ aware in that it conditionally defines based on environment.

E.g. (PRIVATE resolves to static):

#if!defined __cplusplus
#define PRIVATE1 PRIVATE
#endif

...

PRIVATE1 const int some_var;

The problem is I just can't seem to find out what PRIVATE1 resolves to or is in C++, the compiler complains of redefinition if I add a declaration but doesn't indicate where?

I have searched my MinGW/gcc include path, the C++ ISO specification and the C++ books available to me has been to no avail.

Edit:

Sure I checked the command line and makefiles before posting.

+4  A: 

If PRIVATE1 resolves to PRIVATE, and PRIVATE resolves to static, then PRIVATE1 resolves to static.

eduffy
Can't beat that logic... :-)
McWafflestix
You missed the fact that this define is in `#if !__cplusplus` block. I.e. it doesn't run for a C++ compiler, yet the latter somehow figures out what `PRIVATE` is - so the question is, how it does that.
Pavel Minaev
+5  A: 

There's nothing like this in ISO C++ spec. Most likely, PRIVATE1 (as well as PRIVATE) are defined elsewhere in the project. Note that this doesn't need to be a #define in an .h file - it can also be defined via compiler switches in the makefile. I'd suggest doing a full grep on the project directory.

Pavel Minaev
"the C++ ISO specification and the C++ books available to me has been to no avail."
Oliver
+2  A: 

It's unlikely (but not impossible) that they are defined by MinGW itself. Macros defined by the C++ or C implementation should begin with an underscore.

anon
A: 

Most likely in C++ it's defined to, well, "private:".

Andreas Bonini
Mosdt likely not, as the OP's code would then expand to a syntax error.
anon
Sorry, I meant "private:" which would be valid
Andreas Bonini
Then it would need to be part of a class. Since this is compilable in C and C++, it's most likely *not* part of a class.
Rob Kennedy
It can be part of a struct. The only difference between structs and classes in C++ is that structs default to public, while classes default to private.
Andreas Bonini
+1  A: 

Your best bet is to look at the preprocessor output. You didn't post what compiler you are using, but if you check the docs, most have an option to "Preprocess to file" which will create a file with all the macros substituted. This might be able to help you figure out what is happening. In Visual Studio you use the /E option (under C/C++->Preprocessor->Generate Preprocessed File) which will turn foo.c into foo.i. This file will generally be HUGE compared to the original source file, so scroll down to the bottom to see your code.

Dolphin
A: 

Can you declare a function with PRIVATE1? If so, just write a function like so:

PRIVATE1 void Foo() {
    // __FUNCSIG__ in Visual Studio, not sure about GCC
    std::cout << __FUNCSIG__ << std::endl;
}
kitchen
+1  A: 

Eclipse C++ managed project's are a little, well stupid!

If a project is declared C++ it still bases it's build on file extension, hence .h file preprocessed as C and not C++ header which pulls in a #define PRIVATE1 from another header file similarly wrapped by:

#ifdef __cpluplus.

The project is then linked by g++.

Oliver