1

Is there any way to compile C code like this using a C++(g++) compiler.

For instance, is it possible to compile and run the following code:

int main()
{
    int private;
    private = 9;
    return private;
}

it has private variable name, which is a C++ keyword.


The real project is, mixed with C and C++ codes, the driver is written by C with some structure having member name like private, but application is written in C++, and I've tried using extern "C" but doesn't work.

mip
  • 8,355
  • 6
  • 53
  • 72
Leslie Li
  • 407
  • 7
  • 14
  • 2
    Nope. Either use a C compiler to build the program or rename the variable and _hope_ the C++ compiler accepts the rest of your code. – Captain Obvlious Dec 26 '14 at 14:20
  • Use `gcc` to compile instead of `g++` – πάντα ῥεῖ Dec 26 '14 at 14:20
  • 3
    You could work around it by adding a preprocessor definition to your compile command, something like `-Dprivate=private_mangled`. It makes for more complex build rules, which is usually uncomfortable to maintain, so I recommend against it. – Magnus Hoff Dec 26 '14 at 14:24
  • Concerning the question in your topic, the answer is yes. Concerning the idea of using macros to replace keywords, I believe that this is not allowed by the C++ standard, so your results are at best coincidence. – Ulrich Eckhardt Dec 26 '14 at 14:25
  • 3
    Your question title is inconsistent with the body. What are you really asking? – juanchopanza Dec 26 '14 at 14:25
  • 1
    The real project is, mixed with c and c++ codes, the driver is written by c with some structure having member name like *private*, but application is written in c++, and I've tried using extern "C" but doesn't work. – Leslie Li Dec 26 '14 at 14:41
  • 1
    `extern "C"` only affects name mangling and things like this; you're still writing C++. I'm not sure if the macro hack mentioned by @MagnusHoff is allowed by the standard (in C, IIRC, it's OK^H^Htechnically allowed to do such things); and do you have a good reason not to rename the member? Even if it's possible, you'll complicate the build process, and cause bad things ranging from confusing readers of your code to wrong syntax highlighting by your editor. In general, your life is probably easier if you write "C++-safe" headers even when writing C. – mafso Dec 26 '14 at 14:57
  • most drivers are ported from linux drivers, they got many structure member named *private* in the driver structures. thanks for the replies, seems the only way is to change the name to avoid using c++ keywords. – Leslie Li Dec 26 '14 at 15:14
  • 3
    @LeslieLi in addition to @MagnusHoff proposal, you can redefine keyword right before including C header in your C++ code (e.g `#define private C_private`) , and after inclusion undefine `#undef private`. This seems legal in C++11 (see this answer http://stackoverflow.com/a/9110053/205955)... You should be able to refer to that struct field using `C_private` identifier in C++ code. – mip Dec 26 '14 at 16:08

1 Answers1

3

You can use g++ with option -x c, or use gcc.

live example You can specify the input language explicitly with the `-x' option:

-x language Specify explicitly the language for the following input files (rather than letting the compiler choose a default based on the file name suffix). This option applies to all following input files until the next `-x' option. Possible values for language are:

c  c-header  cpp-output
c++  c++-cpp-output
objective-c  objc-cpp-output
assembler  assembler-with-cpp
f77  f77-cpp-input  ratfor
java

-x none Turn off any specification of a language, so that subsequent files are handled according to their file name suffixes (as they are if `-x' has not been used at all).

Quote from here

ForEveR
  • 55,233
  • 2
  • 119
  • 133