简体   繁体   English

安装 g++ 并编译问题

[英]install g++ and compile issues

Can I use gcc to compile C++ program?我可以使用 gcc 来编译 C++ 程序吗? Is there any difference with using g++ instead?改用 g++ 有什么区别吗?

On an exisitng system, I type "man g++", the system returns "no manual entry for g++".在现有系统上,我键入“man g++”,系统返回“没有手动输入 g++”。 But it has gcc installed.但它安装了 gcc。 Is that possible to install g++ from existing gcc library directly;是否可以直接从现有的 gcc 库安装 g++ ; or I have to download g++ instead?或者我必须下载 g++ 代替?

On most systems you will need to install g++ (along with libstdc++ etc) to compile C++ code.在大多数系统上,您需要安装g++ (以及libstdc++等)来编译 C++ 代码。

gcc is a frontend that can be instructed to compile C++, but that usage is not recommended. gcc是一个可以被指令编译 C++ 的前端,但是不推荐这样使用。 Use g++ which also adds the C++ library etc pp.使用g++还添加了 C++ 库等 pp。

/tmp$ cat hw.cc
#include <iostream>

int main(void) {
  std::cout << "Hello, world\n" << std::endl;
}
$ g++ -o hw1 hw.cc
$ gcc -o hw2 hw.cc -lstdc++
$ ./hw1
Hello, world

$ ./hw2
Hello, world

$ 

Just because you can use gcc does not mean you should.仅仅因为您可以使用gcc并不意味着您应该使用。

You can use gcc to compile C++ programs as long as you set the proper flags and/or use the right extensions on your source-files (ie, use .cpp ), but that's only if the C++ development tools, such as libstdc++ have been installed... and that typically means that g++ is available as well. You can use gcc to compile C++ programs as long as you set the proper flags and/or use the right extensions on your source-files (ie, use .cpp ), but that's only if the C++ development tools, such as libstdc++ have been已安装...这通常意味着g++也可用。 So if you don't have the C++ development tools and libraries installed, then no, you're not going to be able to compile C++ files, especially if you start using elements from the STL, etc.因此,如果您没有安装 C++ 开发工具和库,那么不,您将无法编译 C++ 文件,特别是如果您开始使用 Z2523E0C272CB676C6F59F97828 等文件中的元素时。

That being said, if you restricted your C++ source-code to a strict C-subset of the language, then I don't see why it wouldn't compile... but that would defeat the whole point of using C++ in the first place... it would just be a C file with a .cpp extension:-)话虽这么说,如果您将 C++ 源代码限制为该语言的严格 C 子集,那么我不明白为什么它不会编译……但这会破坏首先使用 C++ 的全部意义地方......它只是一个带有.cpp扩展名的C文件:-)

If you are on Linux, then installing g++ is fairly straight-forward.如果您使用的是 Linux,那么安装 g++ 相当简单。 Simply search the repository for g++ using apt-get , yum , or some other package manager, and it should come up as a nice pre-compiled binary, along with all it's dependencies.只需使用apt-getyum或其他一些 package 管理器搜索 g++ 的存储库,它应该会作为一个很好的预编译二进制文件以及它的所有依赖项出现。 If you are on OSX, then it comes with the Xcode developer tools.如果您使用的是 OSX,那么它附带 Xcode 开发人员工具。 If you are on Windows, then you can use MinGW-w64, and again, that comes as a pre-compiled binary for x86_64, or you could use the version that comes with Cygwin (you didn't mention your platform, so it's possible you could be running Cywin on Windows).如果您使用的是 Windows,那么您可以使用 MinGW-w64,同样,它作为 x86_64 的预编译二进制文件提供,或者您可以使用 Cygwin 附带的版本(您没有提到您的平台,所以有可能您可以在 Windows 上运行 Cywin)。 For that, just use the Cygwin package manager again.为此,只需再次使用 Cygwin package 管理器。

g++ is an extension of gcc. g++ 是 gcc 的扩展。 you need to install g++ specific parts to be able to compile c++ programs.您需要安装 g++ 特定部件才能编译 c++ 程序。

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

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