Author Topic: build issue  (Read 5943 times)

Offline wiseguyin

  • Single posting newcomer
  • *
  • Posts: 6
build issue
« on: June 08, 2009, 09:46:45 am »
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:
Code
#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

Offline Dr.Optix

  • Multiple posting newcomer
  • *
  • Posts: 30
  • I'm studying OS Developing.
    • DrOptix Blog
Re: build issue
« Reply #1 on: June 08, 2009, 10:05:10 am »
When you create a Project/File choose C++ as the language you use. See screenshot.

[attachment deleted by admin]
Occupation: Hobby Programmer
IDE: Code::Blocks Nightly / CodeLite Latest Stable
Compiler: TDM's GCC/mingw32
Blog: DrOptix.WordPress.Com

Offline stahta01

  • Lives here!
  • ****
  • Posts: 7582
    • My Best Post
Re: build issue
« Reply #2 on: June 08, 2009, 10:07:44 am »
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
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 64 bit.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline Jenna

  • Administrator
  • Lives here!
  • *****
  • Posts: 7255
Re: build issue
« Reply #3 on: June 08, 2009, 10:16:58 am »
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.

Offline wiseguyin

  • Single posting newcomer
  • *
  • Posts: 6
Re: build issue
« Reply #4 on: June 08, 2009, 05:43:38 pm »
@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.

Offline jomeggs

  • Multiple posting newcomer
  • *
  • Posts: 92
Re: build issue
« Reply #5 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++.

Offline thomas

  • Administrator
  • Lives here!
  • *****
  • Posts: 3979
Re: build issue
« Reply #6 on: June 10, 2009, 01:27:02 pm »
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.
"We should forget about small efficiencies, say about 97% of the time: Premature quotation is the root of public humiliation."

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13413
    • Travis build status
Re: build issue
« Reply #7 on: June 10, 2009, 01:57:45 pm »
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
Code
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...
« Last Edit: June 10, 2009, 06:16:58 pm by oBFusCATed »
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline Sphere

  • Single posting newcomer
  • *
  • Posts: 4
  • Dum de Dum
Re: build issue
« Reply #8 on: September 08, 2009, 10:00:08 am »
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

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13413
    • Travis build status
Re: build issue
« Reply #9 on: September 08, 2009, 10:28:21 am »
You need to surround your C headers with
Code
#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.
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline Sphere

  • Single posting newcomer
  • *
  • Posts: 4
  • Dum de Dum
Re: build issue
« Reply #10 on: September 08, 2009, 12:36:01 pm »

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 :-)