开发者

install g++ and compile issues

开发者 https://www.devze.com 2023-03-20 07:18 出处:网络
Can I use gcc to compile C++ program? Is there any difference with using g++ instead? On an exisitng system开发者_StackOverflow, I type \"man g++\", the system returns \"no manual entry for g++\". Bu

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

On an exisitng system开发者_StackOverflow, 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?


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 (i.e., 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.


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消