Hi all,
I am getting a compile error. Yes, I have read the general notice before submitting issue. I am not going to ask a programming question.
The error I am getting is:
/home/xyz/Hello.h|7|error: expected '=', ',', ';', 'asm' or '__attribute__' before 'Hello'|
The code at that point is:
#ifndef HELLO_H
#define HELLO_H
class Hello { //Error happens here
public:
char greeting[6];
};
#endif
So the default gcc compiler is not recognizing the keyword "Class".
So My question is: how do I change the compiler from gcc to g++ using Code::Blocks?
I tried hand-editing the cbp file but it complained that the compiler could not located.
Suggestions?
PS: If it helps: I am writing a Nautilus extension and will be mixing a lot of C and C++ code.
thanks,
w|z0
When you create a Project/File choose C++ as the language you use. See screenshot.
[attachment deleted by admin]
Also, do not use .c as the file extension; it is preferred to use .cpp, under windows; but, not using .c should help.
Tim S
You can also try to configure g++ as c-compiler in the toolchain-executables tab.
But I don't know, if that has any side-effects.
@stahta01:
Since this 'sounds' the simplest, I will try this first. I have not tried the file renaming feature of code::Blocks yet.
@Dr.Optix:
This , I will do, if all else fails. I created the project as a shared object project/ not a console application, though.
@jens:
This is new; I will try this second.
Thanks guys.
If you have a mix of .c and .cpp files, there is another way. Right click your .c file in your CB project, select "Properties" and afterwards the "Advanced" tab. Set the "Compiler variable" field to CC for .c for standard C or CPP for C++.
The only acceptable solution to the apparent problem of including this header from a .c file is not to do it.
Name files properly to solve the problem instead of working around it, and don't even think about tampering with toolchain executables or changing individual file properties. This will only, in the average case, cause more problems, for example when you try to compile entirely legal third party C programs.
A year ago, I had to work on a pure C project.
I had created (I think) a console project, than in the wizard I had chosen that the project is C, not C++.
Than every time I had added a file (through ctrl + shift(alt) + n) it was considered a C++ file, not C.
The way I had it fixed: choose the file in the tree -> properties -> switch the type of the file in the dialog
I don't know if this is a known and fixed problem, I will check it tonight, when I get home.
Edit:
I've done a test -> created console c project and added a file, the compiler is correctly chosen, but g++ is used for linking
Here is the full log
gcc -Wall -g -Werror=return-type -Woverloaded-virtual -c /home/obfuscated/projects/tests/test_c_proj/main.c -o .obj/Debug/main.o
gcc -Wall -g -Werror=return-type -Woverloaded-virtual -c /home/obfuscated/projects/tests/test_c_proj/test.c -o .obj/Debug/test.o
g++ -o bin/Debug/test_c_proj .obj/Debug/main.o .obj/Debug/test.o
cc1: warning: command line option "-Woverloaded-virtual" is valid for C++/ObjC++ but not for C
cc1: warning: command line option "-Woverloaded-virtual" is valid for C++/ObjC++ but not for C
Output size is 10,04 KB
Is that correct? I'm on linux 64bit gcc 4.3.3.
Maybe the GCC compiler should be split in two GCC C and GCC C++ compiler...
Quote from: jomeggs on June 10, 2009, 12:01:20 PM
If you have a mix of .c and .cpp files, there is another way. Right click your .c file in your CB project, select "Properties" and afterwards the "Advanced" tab. Set the "Compiler variable" field to CC for .c for standard C or CPP for C++.
Good Morning,
I also have a project that contains both C and C++ files, however, I get linker errors when I call functions from the C files in classes defined in the C++ files. The only workaround I have found so far is to make both C and C++ files use the CPP compiler variable. What I would like to know is whether this is a Codeblocks issue, or MinGW issue, since it does not make sense (to me) to have to manually switch the compiler for each C file I add to my C++ project which uses both C and C++ code.
The reason I ask this is because I develop C code for embedded systems but often want to interface with the system using some sort of PC based software which uses common C files between both projects.
System Details: Codeblocks svn 5535 (though I have had this problem from at least 8.02); MinGW - all versions I have tried including the version that comes with 8.02, TDM's gcc builds with GCC 4.4.0 and 4.4.1; Windows XP, Vista and 7.
Thanks for any insight,
Chris
You need to surround your C headers with
#ifdef __cplusplus
extern "C" {
#endif
Your code
#ifdef __cplusplus
}
#endif
Name mangling in C and CPP is diffrent, with this you tell the c++ compiler that the files are C files and he knows what to do.
Quote from: oBFusCATed on September 08, 2009, 10:28:21 AM
Name mangling in C and CPP is diffrent, with this you tell the c++ compiler that the files are C files and he knows what to do.
Thanks a lot, that makes sense. I figured it was something to do with the mangling, but I completely forgot about the extern C command and thought it was a compiler setting I was missing.
Thanks again for the quick reply :-)