我的一些C程序沒有按預期工作。例如,在C中通過引用傳遞是不可能的,但是當我編寫一個使用它並使用gcc編譯它的C程序時,它工作正常。gcc編譯無效C代碼
gcc是C++編譯器嗎?我如何使它像C編譯器一樣工作?
我的一些C程序沒有按預期工作。例如,在C中通過引用傳遞是不可能的,但是當我編寫一個使用它並使用gcc編譯它的C程序時,它工作正常。gcc編譯無效C代碼
gcc是C++編譯器嗎?我如何使它像C編譯器一樣工作?
嘗試定義命令行選項-pedantic
,並指定您希望遵守的C標準,例如, C99爲--std=c99
,C89爲--std=c89
;這應該使其拒絕任何不屬於指定標準的部分。
編輯:請注意,-ansi
可以代表C89或C++ 98,並可能無法強制編譯器進入「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,g ++和其他前端使用文件名來確定語言。例如,gcc和g ++之間唯一的主要區別是咬合新的C++程序員:不同的鏈接設置(對於C++ stdlib)。
如果您的文件被誤檢測,請使用-x選項(也可能是-std)來明確指定。或遵循gcc用於文件名的常用命名約定。對於C這意味着* .c。
仔細檢查您是否沒有使用大寫/大寫* .C來命名您的文件;這被檢測爲C++。
g++
應該是C++的前端和C的cc
,但都指向gcc。
如果你想編譯符合標準的C代碼,使用gcc -ansi
-1:'-ansi'指定C90或C++ 98,取決於它是C或C++模式。 '-std'選項更加明確。 – greyfade 2010-02-08 19:12:50
程序GCC是可以分派到一個C,一個C++中,阿達,一個Fortran,一個Java並根據可能其他的編譯器驅動程序什麼是安裝和文件的擴展。
如果這些都明智地選擇,你不應該做任何事情來獲得編譯C和C++編譯爲C++文件C文件。要強制編譯爲C,請使用-x c
作爲選項之前的編譯的文件。
我的猜測是你已經用大寫的C而不是小寫的命名了你的文件,而大寫的C被認爲是C++。
gcc
是驅動程序。它會根據文件擴展名或強制-x調用實際的不同前端。
但g++
,它會強制默認治療源文件作爲C++,甚至你的文件(* .c)中(小寫)。
爲什麼不能有一個簡單的試驗,便於學習自己:[請不/usr/libexec/gcc/i386-redhat-linux/4.1.2/cc1plus
線,這是實際的前端編譯]
echo "int main() { } " > test.c
gcc -v -c test.c
======== ================
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++前端。而已。
你不能發表一些類似的東西(重新PBR)沒有發佈一些代碼! – Joe 2010-02-08 16:33:54
我同意喬:你需要發佈你的代碼和文件名。如果你的源文件的擴展名是.cpp,.cc,.C或.C++(以及其他文件),它將把它編譯爲C++。如果它正在用gcc(或g ++)編譯.c文件,則通過引用調用將不會編譯。請注意,如果您嘗試使用gcc鏈接您的C++對象,則它可能無法成功鏈接,因爲默認情況下,gcc不會鏈接C++庫。 – 2010-02-08 16:47:51
@Joe:什麼是PBR? – 2010-02-08 17:05:55