User forums > Using Code::Blocks
mixed project C99 and C++
Audiodroid:
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:
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
--- End code ---
Audiodroid:
If I have a line like this:
--- Code: ---for (int i = 0; i < max; i++)
{
///...
}
--- End code ---
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:
--- Quote from: Audiodroid on November 18, 2013, 03:16:37 pm ---If I have a line like this:
--- Code: ---for (int i = 0; i < max; i++)
{
///...
}
--- End code ---
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!
--- End quote ---
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++.
Audiodroid:
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".
Navigation
[0] Message Index
[#] Next page
Go to full version