gcc compiling invalid C code
Some of my C programs are not working as expected. For example, pass by reference is not possible in C, but when I write a C program which uses that and compile it with gcc it works fine.
Is gcc a C++ compiler? How开发者_JAVA百科 do I make it behave like a C compiler?
gcc, g++, and the other frontends use filenames to determine language. For example, the only major difference between gcc and g++ is one that bites new C++ programmers: different link settings (for the C++ stdlib).
Use the -x option (and maybe -std) to specify explicitly, if your files get mis-detected. Or follow the common naming conventions that gcc uses for filenames. For C this means *.c.
Double-check that you didn't use a capital/uppercase *.C to name your file; that's detected as C++.
If I compile this:
int f( int & r ) {
return r + 1;
}
int main() {
int x = 3;
return f( x );
}
with:
gcc e.c
I get:
e.c:1: error: expected ';', ',' or ')' before '&' token
Have you perhaps given the file you are compiling a .cpp extension? If you have, the gcc driver will compile it as a C++ file.
The program gcc is a driver which can dispatch to a C, a C++, an Ada, a Fortran, a Java and probably other compilers depending on what is installed and the extension of the file.
If those are wisely chosen, you shouldn't have to do anything to get C files compiled as C and C++ files compiled as C++. To force compiling as C, use -x c
as an option before the compiled file.
My guess is that you have named your file with an uppercase C instead of an lowercase one, and the uppercase C is considered as C++.
Try defining the command-line option -pedantic
, and specify the C standard you wish to comply to, e.g. --std=c99
for C99, --std=c89
for C89; this should make it reject anything not part of the specified standard.
Edit: Note that -ansi
can stand for either C89 or C++98, and might not work to force the compiler into “C-mode”.
gcc
is the driver. It will invoke the actual different front-end according to the file extension or forcibly -x.
But for g++
, it'll force to treat source files as C++ by default, even your file is (*.c) (lowercase).
Why not have a simple trial to convience yourself:
echo "int main() { } " > test.c
gcc -v -c test.c
[Please not the /usr/libexec/gcc/i386-redhat-linux/4.1.2/cc1plus
line, which is the actual front-end compiler.]
========================
Using built-in specs.
Target: i386-redhat-linux
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-libgcj-multifile --enable-languages=c,c++,objc,obj-c++,java,fortran,ada --enable-java-awt=gtk --disable-dssi --enable-plugin --with-java-home=/usr/lib/jvm/java-1.4.2-gcj-1.4.2.0/jre --with-cpu=generic --host=i386-redhat-linux
Thread model: posix
gcc version 4.1.2 20080704 (Red Hat 4.1.2-46)
/usr/libexec/gcc/i386-redhat-linux/4.1.2/cc1 -quiet -v test.c -quiet -dumpbase test.c -mtune=generic -auxbase test -version -o /tmp/ccUiF4Qr.s
ignoring nonexistent directory "/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../i386-redhat-linux/include"
#include "..." search starts here:
#include <...> search starts here:
/usr/local/include
/usr/lib/gcc/i386-redhat-linux/4.1.2/include
/usr/include
End of search list.
GNU C version 4.1.2 20080704 (Red Hat 4.1.2-46) (i386-redhat-linux)
compiled by GNU C version 4.1.2 20080704 (Red Hat 4.1.2-46).
GGC heuristics: --param ggc-min-expand=59 --param ggc-min-heapsize=55455
Compiler executable checksum: 435964263b657ac05d988fae7b6714b1
as -V -Qy -o test.o /tmp/ccUiF4Qr.s
GNU assembler version 2.17.50.0.6-12.el5 (i386-redhat-linux) using BFD version 2.17.50.0.6-12.el5 20061020
=============================
mv test.c test.cpp
gcc -v -c test.cpp
=============================
Using built-in specs.
Target: i386-redhat-linux
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-libgcj-multifile --enable-languages=c,c++,objc,obj-c++,java,fortran,ada --enable-java-awt=gtk --disable-dssi --enable-plugin --with-java-home=/usr/lib/jvm/java-1.4.2-gcj-1.4.2.0/jre --with-cpu=generic --host=i386-redhat-linux
Thread model: posix
gcc version 4.1.2 20080704 (Red Hat 4.1.2-46)
/usr/libexec/gcc/i386-redhat-linux/4.1.2/cc1plus -quiet -v -D_GNU_SOURCE test.cpp -quiet -dumpbase test.cpp -mtune=generic -auxbase test -version -o /tmp/ccUgae0u.s
ignoring nonexistent directory "/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../i386-redhat-linux/include"
#include "..." search starts here:
#include <...> search starts here:
/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/4.1.2
/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/4.1.2/i386-redhat-linux
/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/4.1.2/backward
/usr/local/include
/usr/lib/gcc/i386-redhat-linux/4.1.2/include
/usr/include
End of search list.
GNU C++ version 4.1.2 20080704 (Red Hat 4.1.2-46) (i386-redhat-linux)
compiled by GNU C version 4.1.2 20080704 (Red Hat 4.1.2-46).
GGC heuristics: --param ggc-min-expand=59 --param ggc-min-heapsize=55455
Compiler executable checksum: 2c84476b74368e297382b43d14e53b01
as -V -Qy -o test.o /tmp/ccUgae0u.s
GNU assembler version 2.17.50.0.6-12.el5 (i386-redhat-linux) using BFD version 2.17.50.0.6-12.el5 20061020
================================
mv test.cpp test.c
g++ -v -c test.c
You'll get the same result as gcc
+ test.cpp
, which means compiled as C++.
cc
is the C front end cc1plus
is the C++ front end. That's it.
g++
should be the front-end for C++ and cc
for C, but both point to gcc.
If you want to compile standard compliant C code, use gcc -ansi
精彩评论