编译 C++与海湾合作委员会的计划
如何使用 GCC 编译器编译 C++ 程序?
文件 info.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;
}
当我尝试编译 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
难道 GCC 编译器不能编译 C++ 程序?
相关说明,gcc
和 g++
之间有什么区别?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
gcc
实际上可以很好地编译 C++ 代码。您收到的错误是链接器错误,而不是编译器错误。可能性是,如果您将编译行更改为:
这使其链接到标准 C++ 库,那么它将正常工作。
但是,您应该使用
g++
让您的生活更轻松。Rup 在 他对另一个答案的评论:
gcc
can actually compile C++ code just fine. The errors you received are linker errors, not compiler errors.Odds are that if you change the compilation line to be this:
which makes it link to the standard C++ library, then it will work just fine.
However, you should just make your life easier and use
g++
.Rup says it best in his comment to another answer:
如果您为代码提供 .c 扩展名,编译器会认为它是 C 代码,而不是 C++。 C++ 编译器驱动程序称为 g++,如果您使用 gcc 驱动程序,则会出现链接器问题,因为默认情况下不会链接标准 C++ 库。所以你想要:
并且甚至不要考虑使用大写的 .C 扩展名,除非你不想移植你的代码,并且准备好被你的同事讨厌。
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:
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.
您应该使用
g++
而不是gcc
。You should use
g++
instead ofgcc
.默认情况下,
gcc
根据文件扩展名选择语言,但您可以使用 -x 选项强制gcc
选择不同的语言后端因此:更多选项在 gcc 手册页的“控制输出类型的选项”下有详细说明。请参见gcc(1) - Linux 手册页(在页面上搜索文本
-x language
)。在
gcc
无法使用文件扩展名猜测语言的情况下,此功能非常有用,例如,如果您生成代码并通过 gcc href="https://en.wikipedia.org/wiki/Standard_streams#Standard_input_(stdin)" rel="nofollow noreferrer">标准输入。By default,
gcc
selects the language based on the file extension, but you can forcegcc
to select a different language backend with the -x option thus:More options are detailed on the
gcc
man page under "Options controlling the kind of output". See e.g. gcc(1) - Linux man page (search on the page for the text-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 togcc
via standard input.gcc
和g++
之间的区别是:使用
g++
而不是gcc
来编译 C++ 源代码。The difference between
gcc
andg++
are:Use
g++
instead ofgcc
to compile you C++ source.如果我没记错的话,gcc 根据后缀确定文件类型。因此,将其设为 foo.cc,它应该可以工作。
并且,为了回答您的其他问题,这是“gcc”和“g++”之间的区别。 gcc 是一个选择正确编译器的前端。
If I recall correctly, gcc determines the filetype from the suffix. So, make it foo.cc and it should work.
And, to answer your other question, that is the difference between "gcc" and "g++". gcc is a frontend that chooses the correct compiler.
Ubuntu 上的 GCC 版本 10.3.0 的更新一个>。即使我添加
-lstdc++
或使用正确的扩展名,cc1plus
也必须安装在系统上。否则会出现此错误:获得此信息的一种方法是安装
g++
,即使您不打算直接使用它,例如sudo apt install g++
。现在,如果我使用扩展名
.cc
,我可以直接调用gcc
,但是,警告和错误仍然会出现。要干净地使用gcc
,带有.c
扩展名,我必须将其命名为 this:如果我使用
.cc
扩展名,则会更短,例如接受的答案 :或者像其他人所说的那样,只需使用
g++ info.c
即可。不需要额外的参数来指示 C++,并且它可以与.c
、.cc
和.cpp
扩展一起使用。An update with my GCC version 10.3.0 on Ubuntu. Even if I add
-lstdc++
or use the correct extension,cc1plus
must be installed on the system. Otherwise this error shows up:One way to get this is to install
g++
even if you're not going to use it directly, e.g.sudo apt install g++
.Now, if I use the extension
.cc
, I can just callgcc
directly, however, warnings and errors still show up. To usegcc
cleanly, with a.c
extension I have to call it like this:Shorter if I use the
.cc
extension, like the accepted answer:Or like others have said, just use
g++ info.c
instead. No extra parameters are needed to indicate C++, and it works with.c
,.cc
and.cpp
extensions.对于 .cpp 文件:
For a .cpp File:
这对我来说效果很好。只需在 Windows 命令行上编写一行代码 (CMD)。
首先,确认您已安装
gcc
(对于 C)或 g++(对于 C++)编译器。在命令行上,对于 gcc,键入:
在命令行上,对于 g++,键入:
如果已安装,则继续。
现在,使用命令行编译 .c 或 .cpp。
对于 C 语法:
示例:
这里exe_filename(示例中为 myfile) 是您要在编译后生成的您的 .exe 文件的名称(注意:我没有放置任何此处扩展)。 yourfilename.c(例如 myfile.c) 是您的源文件,其扩展名为 .c。
现在转到包含 .c 文件的文件夹。在这里您将找到一个扩展名为 .exe 的文件。只要打开它。 Hurray...
对于 C++ 语法:
之后的过程与 C 语法相同。
It worked well for me. Just one line code on the Windows command line (CMD).
First, confirm that you have installed
gcc
(for C) or g++ (for C++) compiler.On the command line, for gcc, type:
On the command line, for g++, type:
If it is installed then proceed.
Now, compile your .c or .cpp using the command line.
For C syntax:
Example:
Here exe_filename (myfile in example) is the name of your .exe file which 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 file which has the .c extension.
Now go to the folder containing your .c file. Here you will find a file with the .exe extension. Just open it. Hurray...
For C++ syntax:
After it, the process is the same as for the C syntax.