0

We are trying a modify some existing C projects i.e. we are trying to make some C++ function calls from the C code. We tried changing compiler from gcc to g++ but there were several compilation errors because of incompatibality. What we are trying to do is to call some C++ functions in C code without making any change to existing code. Simply changing the compiler didn't seem to do the trick. Hence we tried following:

#include <stdio.h>

extern "C"
{
    int func(int new ) {
        printf("in new func()\n");
    }
}

When i compile it using command

g++ -c hello.c -o hello

we get following errors

hello.c:9: error:expected ‘,’ or ‘...’ before ‘new’.

Now we are aware that new is c++ keyword. As mentioned before, we are trying not to make any modifications to existing C code. Any suggestions ?

These aren't the only errors. There are other errors related to structure declaration.

attr.c:75: error: expected primary-expression before ‘.’ token

At attr.c, line 75 is

static post_op_attr error_attr = {.attributes_follow = FALSE };

The problem is that there are other C styled structure declaration and initialization done in the code so even if we rename the variable name, we would still have to modify other parts of the C program. Hence we are looking for a way to add C++ function calls to C code without modifying existing C code.

Raheel
  • 21
  • 2
  • 4
  • 2
    If you don't want to make any modifications to the C code, why not carry on compiling it as C? From C++ you just need `extern "C"` declarations of the functions that you need to call. – CB Bailey Dec 18 '13 at 08:44
  • 1
    To clarify Charles' point, note that in the declaration within your `C++` program, you don't need to give the argument a name. – BoBTFish Dec 18 '13 at 08:46
  • @BoBTFish Nor is it necessary in C. –  Dec 18 '13 at 08:48
  • To OP: "Error in compiling C code with with g++" - exactly, that's an error. C code should only compiled with a C compiler, which g++ isn't, because it's a C++ compiler. –  Dec 18 '13 at 08:49
  • @H2CO3 True, but I was trying to point out that at the point where you *declare* it (which will be in `C++` code), you don't need to give it a name. At the *definition*, the name is necessary, but this is ok as it will be in `C` code. – BoBTFish Dec 18 '13 at 08:51

7 Answers7

7

new is a keyword so you can't call a variable new. But even if you could I'm not sure why you would because it is incredibly hard to read.

Even though you are using extern "C" you are still compiling with g++ which is the c++ compiler and it is going to complain about this.

Basically C code cannot always run unchanged in c++ because c++ introduced some keywords. This is one of those times where you have no choice but to change some of the existing code C code to get it to compile as c++ code.

shuttle87
  • 15,466
  • 11
  • 77
  • 106
  • Sorry, Understand now – Chinna Dec 18 '13 at 08:43
  • if we dont use c++ compiler i.e g++ we wont be able to use C++ functions in the existing C code. – Raheel Dec 18 '13 at 08:46
  • 3
    @user3114360 : That's because C is a different language than C++. If you want to convert your C to C++, then now's the time to fix the variable names. – Joe Z Dec 18 '13 at 08:47
  • @user3114360 I thought you didn't want to change the existing `C` code at all? If you are changing it anyway (to add some calls to `C++` functions), just change the name. – BoBTFish Dec 18 '13 at 08:47
  • The answer is just to change the old C code to not have any c++ keywords in it. You really don't have any other choice I'm afraid. – shuttle87 Dec 18 '13 at 08:47
  • @shuttle87 The preprocessor gives him a choice. It's not a pleasant one, though. – Roddy Dec 18 '13 at 08:48
  • @Roddy, I think preprocessing `new` to be something else then risks breaking c++ code, but even if it didn't it really seems like a maintenance nightmare waiting to happen. – shuttle87 Dec 18 '13 at 08:51
  • @shuttle87: The preprocessor solution can/should be restricted to the "C" part of the source. – MSalters Dec 18 '13 at 11:11
3

As you say new is a C++ keyword, so can't be used as a variable name.

If your C code is calling C++ functions, than you should really take the view that it isn't C and more, but C++. And that will mean more (much, much more if you want to make it idiomatic C++) then just replacing a few badly named variables.

But, if you're intent on minimal changes to your C codebase, you could try this hack:

#define new new__ 

But, I really wouldn't recommend that as a long term fix.

Roddy
  • 66,617
  • 42
  • 165
  • 277
  • 1
    While the `#define` will probably work on most compilers, it is illegal from a Standard `C++` point of view. So if you *do* use this, you aren't writing `C++` any more (although it will probably work). – BoBTFish Dec 18 '13 at 08:49
  • static post_op_attr error_attr = {.attributes_follow = FALSE }; error is : attr.c:75: error: expected primary-expression before ‘.’ token The problem is that there are other C styled structure declaration and initialization done in the code so even if we rename the variable name, we would still have to modify other parts of the C program – Raheel Dec 18 '13 at 08:56
  • @user3114360, Well, that's a different problem. Those new-fangled C99 struct initializers aren't going to play nicely with C++. – Roddy Dec 18 '13 at 10:03
  • @Roddy Well is there any compiler flag related to it to solve that, or any other solution ?? – Raheel Dec 18 '13 at 10:04
  • @BoBTFish Only illegal if you're using C++ standard library, if I understand this correctly. http://stackoverflow.com/questions/9109377/is-it-legal-to-redefine-a-c-keyword – Roddy Dec 18 '13 at 10:09
  • @Roddy If you are looking at Alok Save's answer, he doesn't say so, but his quote is from the 2003 Standard. In The (current, 2011) Standard, the wording has changed: `17.6.4.3.1 Macro names [macro.names] ... 2 A translation unit shall not #define or #undef names lexically identical to keywords, to the identifiers listed in Table 3, or to the attribute-tokens described in 7.6.` – BoBTFish Dec 18 '13 at 10:17
  • Also, it says "A translation unit that includes a header", not "a **standard** header". Which is odd anyway. – BoBTFish Dec 18 '13 at 10:20
1

new is keyword, do not use g++ if you do not want to modify the code. Use gcc instead.

Note: if it's c code, it should never include extern "C", if it's c++ code, it should never use new as variable.

Mine
  • 4,123
  • 1
  • 25
  • 46
  • static post_op_attr error_attr = {.attributes_follow = FALSE }; error is : attr.c:75: error: expected primary-expression before ‘.’ token The problem is that there are other C styled structure declaration and initialization done in the code so even if we rename the variable name, we would still have to modify other parts of the C program – Raheel Dec 18 '13 at 08:51
1

The rules in C++ for identifiers are :

1.Only Alphabets,Digits and Underscores are permitted.

2.Identifier name cannot start with a digit.

3.Key words cannot be used as a name.

4.Upper case and lower case letters are distinct.

5.Special Characters are not allowed

6.Global Identifier cannot be used as “Identifier”.

EDIT: regarding your requirement to call C++ functions from C code, without modifying the C code,

one way is to write a wrapper around your C++ code in C and then compile with gcc, it is not easy to call c code directly from g++ as discussed here :

How to call C++ function from C?

also you might like to see this:

http://www.thegeekstuff.com/2013/01/mix-c-and-cpp/

http://research.engineering.wustl.edu/~beardj/Mixed_C_C++.html

and also

http://www.parashift.com/c++-faq-lite/c-calls-cpp.html

Community
  • 1
  • 1
gaurav5430
  • 12,934
  • 6
  • 54
  • 111
  • static post_op_attr error_attr = {.attributes_follow = FALSE }; error is : attr.c:75: error: expected primary-expression before ‘.’ token The problem is that there are other C styled structure declaration and initialization done in the code so even if we rename the variable name, we would still have to modify other parts of the C program – Raheel Dec 18 '13 at 08:54
  • @Raheel: Indeed. No amount of magic is going to turn a C99 program into C++11. The two languages forked in the early 90s. Today they're mostly link-compatible. – MSalters Dec 18 '13 at 11:13
0

Every computer language has some reserve keywords which it doesn't allow to be used as variable names. Here is a list of c language reserved key words which you should not use as variable names http://www.tutorialspoint.com/ansi_c/c_reserved_keywords.htm

One more thing as you are compiling on a c++ compiler so you should also avoid using c++ reserved keywords here is a link for c++ keywords http://cs.smu.ca/~porter/csc/ref/cpp_keywords.html

Change new with something else like change new with num or anything else.

Taimour
  • 459
  • 5
  • 21
0

C and C++ are different language, so it makes not much sense to compiler C code with a C++ compiler.

But you don't need to, people do combine C and C++ in different compilation units on a daily base. Just create a common header file toto.h that you will include in both. Have all functions there inside an extern "C" clause that you make invisible for C and visible for C++.

#ifdef __cplusplus
extern "C" {
#endif

   // your function declarations (not definitions) go here

#ifdef __cplusplus
}
#endif

Now you can implement your functions in separate compilation units (.c files). And these functions may call each other without problems.

Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177
0

Migrated from your deleted question.

What extern "C" does

extern "C" will not make that code to be treated as a C code, it's just a linkage option.

Check this post here on SO for more information about what it means. To make it short (and simple) it changes how function name is stored in binary files (C++ compiler mangles - decorate - function names because of function overloads: same name but different parameters). You'll need it only when you're linking a C library to a C++ module (or when you're exporting a C interface from your C++ module).

C and C++ compilers

Because you're using a C++ compiler (g++ is C++ GCC frontend, confused? Take a look to this post about what's difference between gcc and g++) your file will be compiled as C++ code and you can't use C++ keywords (as posted in this answer).

Solution

To compile C source files (assuming you can't simply call gcc C frontend) using g++ C++ frontend you have to use -std=c99 (see this documentation for C99 GCC support).

g++ -c -std=c99 hello.c -o hello
Community
  • 1
  • 1
Adriano Repetti
  • 65,416
  • 20
  • 137
  • 208