Keil Logo

Keil C99 & C++ mix

Next Thread | Thread List | Previous Thread Start a Thread | Settings

Details Message
Author
Marc Crandall
Posted
28-Apr-2011 21:49 GMT
Toolset
ARM
New! Keil C99 & C++ mix

Hello,

I have a base project that uses C99. (compiler option --c99)

Basically c99 was used so that an aggregate type could be initialized with non-constant data as such:

#define WRITE_OP_CODE (34)

void test_fn(unsigned char param1, unsigned char param2)
{
    unsigned char writebuff[3] = {WRITE_OP_CODE, param1, param2};
    // no error on one compiler
    // and a "expression must have a constant value" error on another

    ...
}

The problem is I am now mixing in some c++ (third part library) and I get all sort of errors for the C linkage (extern "c")

Does anyone have a suggestion as to how to mix C99 and C++ with various extern c linkage statements?

Author
scott douglass
Posted
3-May-2011 13:43 GMT
Toolset
ARM
New! RE: Keil C99 & C++ mix

C99 uses the same linkage as C90 (aka C89) so the extern "C" issues should be the same.

I can't tell what errors you are getting. Are the compiler errors or linker errors? Are you feeding 'extern "C"' to C99? You can't do that. The usual pattern for a header file is:

...
#ifdef __cplusplus
extern "C" {
#endif

extern int myfn(int, int);

...
#ifdef __cplusplus
}
#endif
...

That way the 'extern "C"' is not seen in C or C99.

Another style is

#ifdef __cplusplus
#define EXTERN_C extern "C"
#else
#define EXTERN_C extern
#endif

EXTERN_C int myfn(int, int);
...
Author
Marc Crandall
Posted
3-May-2011 16:07 GMT
Toolset
ARM
New! RE: Keil C99 & C++ mix

Hi Scott,

Thanks for the information. I don't think this is the problem though. I think it's more of a setup problem.

My project compiles as expected without any errors. It is a mix of C (*.c) and C++ (*.cpp). When I add the "--c99" flag to the compiler arguments it seems as though it is trying to compile the C++ source as C99.

For example I get this type of error:
error: #20: identifier "class" is undefined

How do I set up the project options such that *.c files are compiled with "--c99" and *.cpp files are compiled as C++?

Thanks

M

Author
scott douglass
Posted
4-May-2011 10:46 GMT
Toolset
ARM
New! RE: Keil C99 & C++ mix

Ah, I see. I don't think there is a way to use the same options and get C99 with .c files and C++ with .cpp files. You might find using C++ (--cpp) for your .c files gives you what you want.

Author
PerV Westermark
Posted
4-May-2011 11:47 GMT
Toolset
ARM
New! RE: Keil C99 & C++ mix

How about opening the options for the individual C files and manually give the -c99 option instead of giving it globally for the project?

This really seems to be a Keil bug to be officially reported. A project should be able to mix C and C++ without having the language-specific options fight each other.

Author
Kevin From Support
Posted
9-Nov-2018 16:15 GMT
Toolset
ARM
New! RE: Keil C99 & C++ mix

See also this article:

http://www.keil.com/support/docs/3223.htm

Author
Robert McNamara
Posted
9-Nov-2018 17:34 GMT
Toolset
ARM
New! RE: Keil C99 & C++ mix

1) use extern "C" instead of extern "c". You may have it right in your code, but the 1st post had it wrong.

2) Most likely you have the word class in a .h file. If you include a .h file in a .c file that has c++ syntax, you will get the error you indicate. This is not an issue of a .cpp file being compiled as a .c file, it is a .c file including c++ syntax that is being compiled as a .c file (the default) and generating an error when it sees c++ syntax.

If you think it is a tool issue, you need to provide more details. I mix C and C++ all the time, and setting the --c99 flag does not change anything (compiles and links to the identical executable code)

This is really not yet looking even a little bit like a tool issue. If you want help, post more details of the errors and the files being compiled at the time of the error. Any small sections of code just before and just after the error might help. My guess is the .h file with C++ code in it being included in the .c file is your issue. Then maybe an issue with getting your extern "C" correct.

Next Thread | Thread List | Previous Thread Start a Thread | Settings

  Arm logo
Important information

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies.

Change Settings

Privacy Policy Update

Arm’s Privacy Policy has been updated. By continuing to use our site, you consent to Arm’s Privacy Policy. Please review our Privacy Policy to learn more about our collection, use and transfers
of your data.