簡體   English   中英

gcc編譯無效的C代碼

[英]gcc compiling invalid C code

我的一些C程序沒有按預期工作。 例如,在C中不能通過引用傳遞,但是當我編寫一個使用它並使用gcc編譯它的C程序時,它工作正常。

gcc是C ++編譯器嗎? 如何使其行為像C編譯器?

gcc,g ++和其他前端使用文件名來確定語言。 例如,gcc和g ++之間唯一的主要區別就是咬了新的C ++程序員:不同的鏈接設置(對於C ++ stdlib)。

如果您的文件被錯誤檢測,請使用-x選項(可能是-std)明確指定。 或者遵循gcc用於文件名的常用命名約定 對於C,這意味着* .c。

仔細檢查您是否使用大寫/大寫* .C來命名文件; 那被檢測為C ++。

如果我編譯這個:

int f( int & r ) {
    return r + 1;
}

int main() {
    int x = 3;
    return f( x );
}

有:

 gcc e.c

我明白了:

 e.c:1: error: expected ';', ',' or ')' before '&' token

你有沒有給出你正在編譯.cpp擴展名的文件? 如果有,gcc驅動程序會將其編譯為C ++文件。

程序gcc是一個驅動程序,它可以根據安裝的內容和文件的擴展名分派給C,C ++,Ada,Fortran,Java和其他編譯器。

如果明智地選擇了這些,那么你不應該做任何事情來將C文件編譯為編譯為C ++的C和C ++文件。 要強制編譯為C,請在編譯文件之前使用-xc作為選項。

我的猜測是你用大寫的C而不是小寫的C命名你的文件,大寫的C被認為是C ++。

嘗試定義命令行選項-pedantic ,並指定您希望遵守的C標准,例如,對於C99, - --std=c89 --std=c99 ,對於C89, - --std=c89 ; 這應該使它拒絕任何不屬於指定標准的東西。

編輯:請注意, -ansi可以代表C89或C ++ 98,並且可能無法強制編譯器進入“C模式”。

gcc是驅動程序。 它將根據文件擴展名或強制-x調用實際的不同前端。

但是對於g++ ,默認情況下它會強制將源文件視為C ++,即使你的文件是(* .c)(小寫)。

為什么不進行簡單的試驗以使自己適應:

echo "int main() { } " > test.c

gcc -v -c test.c

[請注意/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

你將獲得與gcc + test.cpp相同的結果,這意味着編譯為C ++。

cc是C前端cc1plus是C ++前端。 而已。

g++應該是C ++和cc for C的前端,但都指向gcc。

如果要編譯符合標准的C代碼,請使用gcc -ansi

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM