Page 1 of 1

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

Posted: Tue Jan 03, 2012 5:39 pm
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

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

Posted: Tue Jan 03, 2012 5:40 pm
by GCB-Toronto
Oops the title should read GCC... not GGC...

Gerry

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

Posted: Tue Jan 03, 2012 7:12 pm
by compound
if you are using c++ with some c you should probably be using g++

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

Posted: Tue Jan 03, 2012 7:14 pm
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

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

Posted: Tue Jan 03, 2012 9:14 pm
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

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

Posted: Wed Jun 20, 2012 12:00 pm
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.

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

Posted: Wed Jun 20, 2012 12:26 pm
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.

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

Posted: Thu Jun 21, 2012 11:42 pm
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

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

Posted: Tue Jul 03, 2012 10:56 pm
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.