编译 C++与海湾合作委员会的计划

发布于 2024-09-08 08:38:10 字数 1211 浏览 8 评论 0 原文

如何使用 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++ 程序? 相关说明,gccg++ 之间有什么区别?

How can I compile a C++ program with the GCC compiler?

File 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;
}

And when I try to compile 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 the GCC compiler capable of compiling C++ programs?
On a related note, what is the difference between gcc and g++?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

ゝ杯具 2024-09-15 08:38:10

gcc 实际上可以很好地编译 C++ 代码。您收到的错误是链接器错误,而不是编译器错误。

可能性是,如果您将编译行更改为:

gcc info.C -lstdc++

这使其链接到标准 C++ 库,那么它将正常工作。

但是,您应该使用 g++ 让您的生活更轻松。


Rup他对另一个答案的评论

[...]海湾合作委员会将
选择正确的后端编译器
基于文件扩展名(即
将 .c 编译为 C,将 .cc 编译为 C++)
并链接二进制文件
标准 C 和 GCC 帮助程序库
无论输入语言如何,均默认;
g++ 也会选择正确的
后端基于扩展除了
我认为它编译了所有 C 源代码
作为 C++ 代替(即它编译
.c 和 .cc 作为 C++),它包括
无论如何,libstdc++ 在其链接步骤中
输入语言。

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:

gcc info.C -lstdc++

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:

[...] 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.

两个我 2024-09-15 08:38:10

如果您为代码提供 .c 扩展名,编译器会认为它是 C 代码,而不是 C++。 C++ 编译器驱动程序称为 g++,如果您使用 gcc 驱动程序,则会出现链接器问题,因为默认情况下不会链接标准 C++ 库。所以你想要:

g++ myprog.cpp

并且甚至不要考虑使用大写的 .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:

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.

一袭白衣梦中忆 2024-09-15 08:38:10

您应该使用 g++ 而不是 gcc

You should use g++ instead of gcc.

指尖凝香 2024-09-15 08:38:10

默认情况下,gcc 根据文件扩展名选择语言,但您可以使用 -x 选项强制 gcc 选择不同的语言后端因此:

gcc -x c++

更多选项在 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 force gcc to select a different language backend with the -x option thus:

gcc -x c++

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 to gcc via standard input.

紫瑟鸿黎 2024-09-15 08:38:10

gccg++ 之间的区别是:

     gcc            |        g++
compiles C source   |   compiles C++ source

使用 g++ 而不是 gcc 来编译 C++ 源代码。

The difference between gcc and g++ are:

     gcc            |        g++
compiles C source   |   compiles C++ source

Use g++ instead of gcc to compile you C++ source.

预谋 2024-09-15 08:38:10

如果我没记错的话,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.

深海里的那抹蓝 2024-09-15 08:38:10

Ubuntu 上的 GCC 版本 10.3.0 的更新一个>。即使我添加 -lstdc++ 或使用正确的扩展名,cc1plus 也必须安装在系统上。否则会出现此错误:

gcc:致命错误:无法执行“cc1plus”:execvp:没有这样的文件或目录

获得此信息的一种方法是安装 g++,即使您不打算直接使用它,例如 sudo apt install g++

现在,如果我使用扩展名.cc,我可以直接调用gcc,但是,警告和错误仍然会出现。要干净地使用 gcc,带有 .c 扩展名,我必须将其命名为 this

gcc -x c++ info.c -lstdc++

如果我使用 .cc 扩展名,则会更短,例如接受的答案

gcc info.cc -lstdc++

或者像其他人所说的那样,只需使用 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:

gcc: fatal error: cannot execute ‘cc1plus’: execvp: No such file or directory

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 call gcc directly, however, warnings and errors still show up. To use gcc cleanly, with a .c extension I have to call it like this:

gcc -x c++ info.c -lstdc++

Shorter if I use the .cc extension, like the accepted answer:

gcc info.cc -lstdc++

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.

恋你朝朝暮暮 2024-09-15 08:38:10

对于 .cpp 文件:

g++ myprog.cpp -o myprog

For a .cpp File:

g++ myprog.cpp -o myprog
幸福还没到 2024-09-15 08:38:10

这对我来说效果很好。只需在 Windows 命令行上编写一行代码 (CMD)。

首先,确认您已安装 gcc(对于 C)或 g++(对于 C++)编译器。

在命令行上,对于 gcc,键入:

gcc --version

在命令行上,对于 g++,键入:

g++ --version

如果已安装,则继续。

现在,使用命令行编译 .c 或 .cpp。

对于 C 语法:

gcc -o exe_filename yourfilename.c

示例:

gcc -o myfile myfile.c

这里exe_filename(示例中为 myfile) 是您要在编译后生成的您的 .exe 文件的名称(注意:我没有放置任何此处扩展)。 yourfilename.c(例如 myfile.c)您的源文件,其扩展名为 .c。

现在转到包含 .c 文件的文件夹。在这里您将找到一个扩展名为 .exe 的文件。只要打开它。 Hurray...

对于 C++ 语法:

g++ -o exe_filename yourfilename.cpp

之后的过程与 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:

gcc --version

On the command line, for g++, type:

g++ --version

If it is installed then proceed.

Now, compile your .c or .cpp using the command line.

For C syntax:

gcc -o exe_filename yourfilename.c

Example:

gcc -o myfile myfile.c

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:

g++ -o exe_filename yourfilename.cpp

After it, the process is the same as for the C syntax.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文