简体   繁体   English

使用 gcc 编译 C++ 程序

[英]Compiling a C++ program with gcc

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

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? gcc 编译器不能编译 C++ 程序吗? On a related note, what is the difference between gcc and g++.在相关说明中,gcc 和 g++ 之间有什么区别。 Thanks,谢谢,

gcc can actually compile c++ code just fine. gcc实际上可以很好地编译c ++代码。 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. 这使它链接到标准的c ++库,然后它将工作得很好。

However, you should just make your life easier and use g++. 但是,你应该让你的生活更轻松,并使用g ++。


EDIT: 编辑:

Rup says it best in his comment to another answer: 鲁普说, 对另一个答案的评论最好:

[...] gcc will select the correct back-end compiler based on file extension (ie 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; [...] gcc将根据文件扩展名选择正确的后端编译器(即将.c编译为C,将.cc编译为C ++)并默认将二进制文件与标准C和GCC帮助程序库相链接,而不管输入语言; g++ will also select the correct back-end based on extension except that I think it compiles all C source as C++ instead (ie it compiles both .c and .cc as C++) and it includes libstdc++ in its link step regardless of input languages. g ++也将基于扩展选择正确的后端,除了我认为它将所有C源代码编译为C ++(即它将.c和.cc编译为C ++),并且无论输入语言如何,它在链接步骤中都包含libstdc ++。

If you give the code a .c extension the compiler thinks it is C code, not C++. 如果你给代码一个.c扩展名,编译器认为它是C代码,而不是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. C ++编译器驱动程序称为g ++,如果使用gcc驱动程序,则会出现链接器问题,因为默认情况下标准C ++库不会链接。 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. 甚至不要考虑使用大写的.C扩展名,除非你永远不想移植你的代码,并准备好被你工作的人讨厌。

使用g++而不是gcc

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 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". 更多选项在gcc手册页的“选项控制输出类型”下详细说明。 See eg http://linux.die.net/man/1/gcc (search on the page for the text -x language ). 参见例如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。

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 (用于c)或g ++ (用于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 .c syntax: for .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 file which you want to produce after compilation (Note: i have not put any extension here). 这里exe_filename(示例中myfile)是您要在编译后生成的.exe文件的名称(注意:我没有在此处添加任何扩展名)。 And yourfilename.c (myfile.c in example) is the your source file which has the .c extension. yourfilename.c(示例中的myfile.c)源文件 ,其扩展名为.c。

Now go to folder containing your .c file, here you will find a file with .exe extension. 现在转到包含.c文件的文件夹,在这里您将找到扩展名为.exe的文件。 Just open it. 打开它吧。 Hurray.. 欢呼..

For .cpp syntax: 对于.cpp语法:

g++ -o exe_filename yourfilename.cpp g ++ -o exe_filename yourfilename.cpp

After it the process is same as for .c . 之后,该过程与.c相同。

If I recall correctly, gcc determines the filetype from the suffix. 如果我没记错的话,gcc会根据后缀确定文件类型。 So, make it foo.cc and it should work. 所以,让它成为foo.cc,它应该工作。

And, to answer your other question, that is the difference between "gcc" and "g++". 而且,为了回答你的另一个问题, 就是“gcc”和“g ++”之间的区别。 gcc is a frontend that chooses the correct compiler. gcc是一个选择正确编译器的前端。

对于 .cpp 文件:

          g++ myprog.cpp -o myprog

An update with my gcc version 10.3.0 on Ubuntu.我的gcc 版本 10.3.0在 Ubuntu 上的更新。 Even if I add -lstdc++ or use the correct extension, cc1plus must be installed on the system, otherwise this error shows up:即使我添加-lstdc++或使用正确的扩展名,也必须在系统上安装cc1plus ,否则会出现此错误:

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, eg sudo apt install g++ .获得它的一种方法是安装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.现在,如果我使用扩展名.cc我可以直接调用gcc ,但是,仍然会出现警告和错误。 To use gcc cleanly, with a .c extension I have to call it like this :要使用gcc干净,具有.c扩展我不得不把它像这样

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

Shorter if I use the .cc extension, like the accepted answer :如果我使用.cc扩展名, .cc短,就像接受的答案一样

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.或者像其他人所说的那样,只需使用g++ info.c代替,不需要额外的参数来指示 C++,并且它适用于.c.cc.cpp扩展名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM