Author Topic: mixed project C99 and C++  (Read 8469 times)

Offline Audiodroid

  • Single posting newcomer
  • *
  • Posts: 7
mixed project C99 and C++
« on: November 18, 2013, 02:13:05 pm »
Hello,

I have a project where I have cpp-files and C99/plain c-files. To prevent compiler errors for the C99-files I've added "-std=c99" to "Project=>Build options...=>Compiler Settings=>Other options". Doing this makes the code compile.

But I get a huge list of warnings for each cpp-file, the compiler says that "-std=c99" is not valid for C++ code. Point taken. But how can I then apply the "-std=c99" setting only to the c99-files?

Right-clicking on a file in the "Management=>Projects"-view, then choosing "Properties=>Advanced" there is a "Custom build"-interface. But I can't just put "-std=c99" there while removing it from the project build options, can I?

I would be very happy to get some advice on this, because currently my other warnings get drowned in the huge list of the above mentioned warnings.

I looked elsewhere and on this forum to find an answer to my question but with no luck. Maybe I lack the right keywords. If this has already been addressed elsewhere, my apologies.

ToApolytoXaos

  • Guest
Re: mixed project C99 and C++
« Reply #1 on: November 18, 2013, 03:02:22 pm »
Out of curiosity, what kind of C99 code is that, that you cannot compile with C++ compiler? Unless you use something very specific to C99, I'm sure g++ can compile the code just fine as long as you enclose your C code within extern "C".


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

/* C functions here */
void demo(void);
int something_else(int);

#ifdef __cplusplus
}
#endif

Offline Audiodroid

  • Single posting newcomer
  • *
  • Posts: 7
Re: mixed project C99 and C++
« Reply #2 on: November 18, 2013, 03:16:37 pm »
If I have a line like this:

Code
for (int i = 0; i < max; i++)
{
   ///...
}

not using the option "-std=c99" I get an error from the compiler: "error: 'for' loop initial declarations are only allowed in C99 mode".

I only noticed now (sorry): This problem might only apply to header implementations. I get this e.g. for a static c-function implemented(!) in a header-file. I "inherited" this code. At the moment it's "ugly" as this might tell you, but I can't change all this mess at once. :-/

All cpp-file are including the c-files as you suggest though, without the "#ifdef __cplusplus" though.

Might the answer be, header implementation are ambigious in this case and therefore there is no way around this problem?

Or might the missing "ifdef" cure my problem?

I will surely try both things out, but if this already give you another hint and you can point me to one of the two, even better.

I'm very thankful for this answer already!

ToApolytoXaos

  • Guest
Re: mixed project C99 and C++
« Reply #3 on: November 18, 2013, 03:24:27 pm »
If I have a line like this:

Code
for (int i = 0; i < max; i++)
{
   ///...
}

not using the option "-std=c99" I get an error from the compiler: "error: 'for' loop initial declarations are only allowed in C99 mode".

I only noticed now (sorry): This problem might only apply to header implementations. I get this e.g. for a static c-function implemented(!) in a header-file. I "inherited" this code. At the moment it's "ugly" as this might tell you, but I can't change all this mess at once. :-/

All cpp-file are including the c-files as you suggest though, without the "#ifdef __cplusplus" though.

Might the answer be, header implementation are ambigious in this case and therefore there is no way around this problem?

Or might the missing "ifdef" cure my problem?

I will surely try both things out, but if this already give you another hint and you can point me to one of the two, even better.

I'm very thankful for this answer already!


The sample code you have showed above is a valid C++ code. How many files are there on C project by the way?

Your C files should have CC in their properties and CPP your C++ files, respectively. Also, remove the -std=c99 flag and try to compile it with G++.

Offline Audiodroid

  • Single posting newcomer
  • *
  • Posts: 7
Re: mixed project C99 and C++
« Reply #4 on: November 18, 2013, 03:38:20 pm »
There are about 100 c-files. I have checked the c-files have "CC" set in the options as their compiler variable. And the cpp-files have "CPP". However the c-header files as they have the same extention as the cpp-header files are marked "CPP".

I changed this for one header that is causing problems (when not using "-std=c99"), it doesn't help. It still get the compiler errors.

I am using "GNU GCC Compiler". Looking at "Compiler and debugger settings=>Global compiler settings=>Program Files" it says: "C compiler: mingw32-gcc.exe" and "C++ compiler: mingw32-g++.exe".

ToApolytoXaos

  • Guest
Re: mixed project C99 and C++
« Reply #5 on: November 18, 2013, 04:16:33 pm »
I wish I could see your project, because I have created a header file with extern C linkage, an implementation file, and called my function within C++ main() function.

Is your project heavily using dot operations within structures to initialize values? If yes, that would indeed going to cause problems.

For more information read the following article: Incompatibilities Between ISO C and ISO C++
« Last Edit: November 18, 2013, 04:18:26 pm by ToApolytoXaos »

Offline Audiodroid

  • Single posting newcomer
  • *
  • Posts: 7
Re: mixed project C99 and C++
« Reply #6 on: November 18, 2013, 04:23:41 pm »
Thank you so much for looking into this. For the moment is has to remain a mystery I guess.

As soon as I'm not under that much time pressure, I will make a dummy project to illustrate the problem. Then either I get a hang of it and post the solution here, or I'll post the code and if I'm lucky you have the answer. ;-)

Thanks again for all your help so far!!!

Offline thomas

  • Administrator
  • Lives here!
  • *****
  • Posts: 3979
Re: mixed project C99 and C++
« Reply #7 on: November 18, 2013, 05:20:25 pm »
There's no easy or pretty way to get this right, unluckily. You can compile a project build target with or without some compiler flag (so either you're breaking it for C++ files or you're breaking it for C files), and you can assign individual command to each single file, but this isn't practical for a hundred files (nor is is very straightforward or plays well with project options or portability).
If the compiler's extension-based auto-detection doesn't work, you're kind of at loss with mixed files.

The best thing you can do is make 2 build targets, one with all the C files, and one with all the C++ files (give the appropriate -std=... flag to each), and link the object files together.
« Last Edit: November 18, 2013, 05:22:15 pm by thomas »
"We should forget about small efficiencies, say about 97% of the time: Premature quotation is the root of public humiliation."

Offline osdt

  • Multiple posting newcomer
  • *
  • Posts: 63
Re: mixed project C99 and C++
« Reply #8 on: November 18, 2013, 10:47:35 pm »
Which C::B version do you use? At least the current SVN version filters out C-Only flags when calling g++ and CPP-Only flags when calling gcc. I'm using '-std=c++11 -std=c11' within 'Compiler->Other Options' for mixed projects and it works perfectly.

- osdt