简体   繁体   English

gcc编译无效的C代码

[英]gcc compiling invalid C code

Some of my C programs are not working as expected. 我的一些C程序没有按预期工作。 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. 例如,在C中不能通过引用传递,但是当我编写一个使用它并使用gcc编译它的C程序时,它工作正常。

Is gcc a C++ compiler? gcc是C ++编译器吗? How do I make it behave like a C compiler? 如何使其行为像C编译器?

gcc, g++, and the other frontends use filenames to determine language. gcc,g ++和其他前端使用文件名来确定语言。 For example, the only major difference between gcc and g++ is one that bites new C++ programmers: different link settings (for the C++ stdlib). 例如,gcc和g ++之间唯一的主要区别就是咬了新的C ++程序员:不同的链接设置(对于C ++ stdlib)。

Use the -x option (and maybe -std) to specify explicitly, if your files get mis-detected. 如果您的文件被错误检测,请使用-x选项(可能是-std)明确指定。 Or follow the common naming conventions that gcc uses for filenames . 或者遵循gcc用于文件名的常用命名约定 For C this means *.c. 对于C,这意味着* .c。

Double-check that you didn't use a capital/uppercase *.C to name your file; 仔细检查您是否使用大写/大写* .C来命名文件; that's detected as C++. 那被检测为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? 你有没有给出你正在编译.cpp扩展名的文件? If you have, the gcc driver will compile it as a C++ file. 如果有,gcc驱动程序会将其编译为C ++文件。

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. 程序gcc是一个驱动程序,它可以根据安装的内容和文件的扩展名分派给C,C ++,Ada,Fortran,Java和其他编译器。

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++. 如果明智地选择了这些,那么你不应该做任何事情来将C文件编译为编译为C ++的C和C ++文件。 To force compiling as C, use -xc as an option before the compiled file. 要强制编译为C,请在编译文件之前使用-xc作为选项。

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++. 我的猜测是你用大写的C而不是小写的C命名你的文件,大写的C被认为是C ++。

Try defining the command-line option -pedantic , and specify the C standard you wish to comply to, eg --std=c99 for C99, --std=c89 for C89; 尝试定义命令行选项-pedantic ,并指定您希望遵守的C标准,例如,对于C99, - --std=c89 --std=c99 ,对于C89, - --std=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”. 编辑:请注意, -ansi可以代表C89或C ++ 98,并且可能无法强制编译器进入“C模式”。

gcc is the driver. gcc是驱动程序。 It will invoke the actual different front-end according to the file extension or forcibly -x. 它将根据文件扩展名或强制-x调用实际的不同前端。

But for g++ , it'll force to treat source files as C++ by default, even your file is (*.c) (lowercase). 但是对于g++ ,默认情况下它会强制将源文件视为C ++,即使你的文件是(* .c)(小写)。

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.] [请注意/usr/libexec/gcc/i386-redhat-linux/4.1.2/cc1plus行,这是实际的前端编译器。]

======================== ========================

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++. 你将获得与gcc + test.cpp相同的结果,这意味着编译为C ++。

cc is the C front end cc1plus is the C++ front end. cc是C前端cc1plus是C ++前端。 That's it. 而已。

g++ should be the front-end for C++ and cc for C, but both point to gcc. g++应该是C ++和cc for C的前端,但都指向gcc。

If you want to compile standard compliant C code, use gcc -ansi 如果要编译符合标准的C代码,请使用gcc -ansi

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM