简体   繁体   中英

install g++ and compile issues

Can I use gcc to compile C++ program? Is there any difference with using g++ instead?

On an exisitng system, I type "man g++", the system returns "no manual entry for g++". But it has gcc installed. Is that possible to install g++ from existing gcc library directly; or I have to download g++ instead?

On most systems you will need to install g++ (along with libstdc++ etc) to compile C++ code.

gcc is a frontend that can be instructed to compile C++, but that usage is not recommended. Use g++ which also adds the C++ library etc 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.

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

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:-)

If you are on Linux, then installing g++ is fairly straight-forward. 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. If you are on OSX, then it comes with the Xcode developer tools. 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). For that, just use the Cygwin package manager again.

g++ is an extension of gcc. you need to install g++ specific parts to be able to compile c++ programs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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