使用 gcc 编译 C++ 程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3178342/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Compiling a C++ program with gcc
提问by xyz
Question: How to compile a C++ program with gcc compiler?
问题:如何用gcc编译器编译C++程序?
info.c:
信息.c:
#include<iostream>
using std::cout;
using std::endl;
int main()
{
#ifdef __cplusplus
cout << "C++ compiler in use and version is " << __cplusplus << endl;
#endif
cout <<"Version is " << __STDC_VERSION__ << endl;
cout << "Hi" << __FILE__ << __LINE__ << endl;
}
and when I try to compile info.c
当我尝试编译时 info.c
$ gcc info.C
$ gcc info.C
Undefined first referenced
symbol in file
cout /var/tmp/ccPxLN2a.o
endl(ostream &) /var/tmp/ccPxLN2a.o
ostream::operator<<(ostream &(*)(ostream &))/var/tmp/ccPxLN2a.o
ostream::operator<<(int) /var/tmp/ccPxLN2a.o
ostream::operator<<(long) /var/tmp/ccPxLN2a.o
ostream::operator<<(char const *) /var/tmp/ccPxLN2a.o
ld: fatal: Symbol referencing errors. No output written to a.out
collect2: ld returned 1 exit status
Isn't gcc compiler capable of compiling C++ programs? On a related note, what is the difference between gcc and g++. Thanks,
gcc 编译器不能编译 C++ 程序吗?在相关说明中,gcc 和 g++ 之间有什么区别。谢谢,
回答by Evan Teran
gcc can actually compile c++ code just fine. The errors you received are linkererrors, not compiler errors.
gcc 实际上可以很好地编译 C++ 代码。您收到的错误是链接器错误,而不是编译器错误。
Odds are that if you change the compilation line to be this:
如果您将编译行更改为:
gcc info.C -lstdc++
which makes it link to the standard c++ library, then it will work just fine.
这使它链接到标准的 C++ 库,然后它就可以正常工作了。
However, you should just make your life easier and use g++.
但是,您应该让生活更轻松并使用 g++。
EDIT:
编辑:
Rupsays it best in his commentto another answer:
[...] gcc will select the correct back-end compiler based on file extension (i.e. will compile a .c as C and a .cc as C++) and links binaries against just the standard C and GCC helper libraries by default regardless of input languages; g++ will also select the correct back-end based on extension except that I think it compiles all C source as C++ instead (i.e. it compiles both .c and .cc as C++) and it includes libstdc++ in its link step regardless of input languages.
[...] gcc 将根据文件扩展名选择正确的后端编译器(即,将 .c 编译为 C,将 .cc 编译为 C++)并默认仅针对标准 C 和 GCC 帮助程序库链接二进制文件,而不管输入语言;g++ 还将根据扩展选择正确的后端,但我认为它将所有 C 源代码编译为 C++(即,它将 .c 和 .cc 编译为 C++)并且它在其链接步骤中包含 libstdc++,而不管输入语言如何。
回答by xyz
If you give the code a .c extension the compiler thinks it is C code, not C++. And the C++ compiler driver is called g++, if you use the gcc driver you will have linker problems, as the standard C++ libraries will not be linked by default. So you want:
如果给代码一个 .c 扩展名,编译器会认为它是 C 代码,而不是 C++。而 C++ 编译器驱动程序称为 g++,如果您使用 gcc 驱动程序,则会出现链接器问题,因为默认情况下不会链接标准 C++ 库。所以你要:
g++ myprog.cpp
And do not even consider using an uppercase .C extension, unless you never want to port your code, and are prepared to be hated by those you work with.
甚至不要考虑使用大写的 .C 扩展名,除非您永远不想移植您的代码,并且准备好被与您一起工作的人讨厌。
回答by Alexandre C.
use g++
instead of gcc
.
使用g++
代替gcc
.
回答by Praveen S
The difference between gcc and g++ are:
gcc 和 g++ 的区别是:
gcc | g++
compiles c source | compiles c++ source
use g++ instead of gcc to compile you c++ source.
使用 g++ 而不是 gcc 来编译你的 c++ 源代码。
回答by bleater
By default, gcc selects the language based on the file extension, but you can force gcc to select a different language backend with the -x option thus:
默认情况下,gcc 根据文件扩展名选择语言,但您可以强制 gcc 使用 -x 选项选择不同的语言后端,因此:
gcc -x c++
More options are detailed in the gcc man page under "Options controlling the kind of output". See e.g. http://linux.die.net/man/1/gcc(search on the page for the text -x language
).
更多选项在 gcc 手册页中的“控制输出类型的选项”下有详细说明。参见例如http://linux.die.net/man/1/gcc(在页面上搜索文本-x language
)。
This facility is very useful in cases where gcc can't guess the language using a file extension, for example if you're generating code and feeding it to gcc via stdin.
在 gcc 无法使用文件扩展名猜测语言的情况下,此功能非常有用,例如,如果您正在生成代码并通过 stdin 将其提供给 gcc。
回答by Rajkumar Bansal
It worked well for me. Just one line code in cmd.
它对我来说效果很好。cmd中只有一行代码。
First, confirm that you have installed the gcc(for c) or g++(for c++) compiler.
首先,确认您已经安装了gcc(for c) 或g++(for c++) 编译器。
In cmd for gcc type:
在 cmd 中为 gcc 类型:
gcc --version
gcc --version
in cmd for g++ type:
在 cmd 中为 g++ 类型:
g++ --version
g++ --version
If it is installed then proceed.
如果已安装,则继续。
Now, compile your .c or .cpp using cmd
现在,使用 cmd 编译您的 .c 或 .cpp
for .csyntax:
对于.c语法:
gcc -o exe_filename yourfilename.c
gcc -o exe_filename yourfilename.c
Example:
例子:
gcc -o myfile myfile.c
gcc -o myfile myfile.c
Here exe_filename (myfile in example)is the name of your .exe filewhich you want to produce after compilation (Note: i have not put any extension here). And yourfilename.c (myfile.c in example)is the your source filewhich has the .c extension.
这里exe_filename (myfile in example)是你想要在编译后生成的.exe 文件的名称(注意:我没有在这里放置任何扩展名)。而yourfilename.c(示例中的 myfile.c)是您的源文件,其扩展名为 .c。
Now go to folder containing your .c file, here you will find a file with .exe extension. Just open it. Hurray..
现在转到包含您的 .c 文件的文件夹,在这里您将找到一个扩展名为 .exe 的文件。只需打开它。欢呼..
For .cppsyntax:
对于.cpp语法:
g++ -o exe_filename yourfilename.cpp
g++ -o exe_filename yourfilename.cpp
After it the process is same as for .c .
之后的过程与 .c 相同。
回答by Borealid
If I recall correctly, gcc determines the filetype from the suffix. So, make it foo.cc and it should work.
如果我没记错的话,gcc 会根据后缀确定文件类型。所以,让它成为 foo.cc 并且它应该可以工作。
And, to answer your other question, thatis the difference between "gcc" and "g++". gcc is a frontend that chooses the correct compiler.
而且,要回答您的另一个问题,那就是“gcc”和“g++”之间的区别。gcc 是一个选择正确编译器的前端。