GCC - is it "C" or "C++" ?

General questions regarding the usage of CodeLite
GCB-Toronto
CodeLite Enthusiast
Posts: 16
Joined: Tue Dec 20, 2011 10:20 pm
Genuine User: Yes
IDE Question: C++
Contact:

GCC - is it "C" or "C++" ?

Post by GCB-Toronto »

I am compiling a large "C" project under Codelite using GCC. I can get the project to compile, but the compiler is inconsistently mangling global names. What appears to be happening is that some modules are being compiled in a "C" manner while others are being compiled as though under "C++".

I know this to be the case by inspection of the compiler assembly output using the "-S" option. Also the modules that are compiled as "C" do not recognize

extern "C"

but files compiled as "C++" do recognize this construct.

Because of the inconsistent naming I am getting many linker "undefined reference" errors.

The compile switches are the same for each module, as controlled by CodeLite as it generates the .MK file. All modules in this project were originally compiled under MSC as a purely "C" project and are named with the .C extension.

My questions are:

1. Is GCC supposed to be a pure "C" compiler? Under what conditions can it behave as a "C++" compiler?

2. Is there a switch or a #define that can be used to alter the "language" or name mangling that is used?

Gerry
GCB-Toronto
CodeLite Enthusiast
Posts: 16
Joined: Tue Dec 20, 2011 10:20 pm
Genuine User: Yes
IDE Question: C++
Contact:

Re: GCC - is it "C" or "C++" ?

Post by GCB-Toronto »

Oops the title should read GCC... not GGC...

Gerry
compound
CodeLite Curious
Posts: 3
Joined: Thu Dec 22, 2011 1:59 pm
Genuine User: Yes
IDE Question: c++
Contact:

Re: GCC - is it "C" or "C++" ?

Post by compound »

if you are using c++ with some c you should probably be using g++
User avatar
eranif
CodeLite Plugin
Posts: 6375
Joined: Wed Feb 06, 2008 9:29 pm
Genuine User: Yes
IDE Question: C++
Contact:

Re: GCC - is it "C" or "C++" ?

Post by eranif »

If your C files are compilable with C++ compiler, you can instruct codelite to use g++ instead of gcc for C files.

This can be done from:
1) Find out which compiler settings are you using from: project settings -> common settings -> general ->Compiler
2) go to : "Settings -> build Settings -> Compilers -> <compiler name> -> Tools" and set "C++ compiler name" to "g++", "C compiler name" to "gcc" and "shared object linker" => "g++ -shared fPIC"
3) Under "Settings -> build Settings -> Compilers -> <compiler name> -> File Types" add new file type "C" and copy the settings for the "c" file type.

Rebuild your project
Eran
Make sure you have read the HOW TO POST thread
GCB-Toronto
CodeLite Enthusiast
Posts: 16
Joined: Tue Dec 20, 2011 10:20 pm
Genuine User: Yes
IDE Question: C++
Contact:

Re: GCC - is it "C" or "C++" ?

Post by GCB-Toronto »

My files are not compatible with "C++". I want to compile using "C".

It turns out that some of my files have a .C rather than a .c extension. I believe given a apparently unknown file extension that the compiler is "falling back" to C++.

I am able to fix the problem by changing the project file so that all .c is lower case. I don't have to rename the actual .c files, just the name of each file as given to the CodeLite project file.

I don't think it is something that CodeLite is doing, because the command line generated appears to be identical whether the file has a .c or a .C extension. it seems to be a "quirk" of GCC. I don't know if this is a documented thing or not.

Gerry
jackieku
CodeLite Curious
Posts: 1
Joined: Wed Jun 20, 2012 11:54 am
Genuine User: Yes
IDE Question: C++
Contact:

Re: GCC - is it "C" or "C++" ?

Post by jackieku »

GCB-Toronto wrote:it seems to be a "quirk" of GCC. I don't know if this is a documented thing or not.
Gerry
It is documented:
http://gcc.gnu.org/onlinedocs/gcc-4.7.1 ... ll-Options
file.c
C source code that must be preprocessed.

file.cc
file.cp
file.cxx
file.cpp
file.CPP
file.c++
file.C
C++ source code that must be preprocessed. Note that in `.cxx', the last two letters must both be literally `x'. Likewise, `.C' refers to a literal capital C.
User avatar
Jarod42
CodeLite Expert
Posts: 240
Joined: Wed Sep 30, 2009 5:54 pm
Genuine User: Yes
IDE Question: C++
Location: France
Contact:

Re: GCC - is it "C" or "C++" ?

Post by Jarod42 »

Extract of gcc man:

Code: Select all

-x language
    Specify explicitly the language for the following input files (rather than letting the compiler choose a default based on the file name suffix). This option applies to all following input files until the next -x option. Possible values for language are:

    c  c-header  c-cpp-output
    c++  c++-header  c++-cpp-output
    objective-c  objective-c-header  objective-c-cpp-output
    objective-c++ objective-c++-header objective-c++-cpp-output
    assembler  assembler-with-cpp
    ada
    f77  f77-cpp-input f95  f95-cpp-input
    java

-x none
    Turn off any specification of a language, so that subsequent files are handled according to their file name suffixes (as they are if -x has not been used at all).
So in your case:

Code: Select all

gcc -x c
or change the extension.
spaces
CodeLite Veteran
Posts: 67
Joined: Mon Aug 22, 2011 10:15 pm
Genuine User: Yes
IDE Question: c++
Contact:

Re: GCC - is it "C" or "C++" ?

Post by spaces »

Well, this is not a problem with GCC, this is an undefined behavior left to the compilers. Anyway use this in your header files if you want to use functions from C and C++, too.

Code: Select all

#ifdef __cplusplus
extern "C" {
#endif

extern void blabla( void );

#ifdef __cplusplus
}
#endif
foxmuldr
CodeLite Expert
Posts: 120
Joined: Sun May 10, 2009 6:56 am
Contact:

Re: GCC - is it "C" or "C++" ?

Post by foxmuldr »

spaces wrote:

Code: Select all

#ifdef __cplusplus
extern "C" {
#endif

extern void blabla( void );

#ifdef __cplusplus
}
#endif
The above is what I do for all my C programming when it has to touch other places. When it's just C code in a single stand-alone app, I leave the above off, but just use the .cpp file extension even though it's only C code. I typically program about 75% C code, 25% C++ code, but that 25% uses only the class and that's about it. Everything else I keep lower level.

C++ allows for some relaxed syntax restrictions, such as not requiring the keyword "struct" when structures are used, being able to define member functions in a struct (for encapsulation), the bool variable type is native, no requirements on main() arguments or an explicit "return 0", and other things which make writing C code nicer. :-)

C++ does remove a couple things I do like about C though, such as void* assignment to other types ("int* x = malloc(n * sizeof(int));" is valid in C, fails in C++), and the freedom to not always have forward declarations for functions.
Best regards,
Rick C. Hodgin
www.Visual-FreePro.org
www.LibSF.org
Post Reply