ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
On my machine, gcc doesn't like c++ programs correctly, at least with default arguments. Also, g++ will compile and link a c++ program which has a .c extension, while gcc won't even compile it. (looks like it doesn't know where the c++ headers are)
gcc and g++ do share a lot of code. In fact, I want to say that g++ just invokes gcc with some set of additional arguments.
09:58 aluser@alf:~/test/c$ cat hello.cpp
#include <iostream>
using namespace std;
int main() {
cout << "hello world\n";
return 0;
}
09:58 aluser@alf:~/test/c$ gcc -Lstdc++ -o hello hello.cpp
/tmp/cc4E4x2P.o(.text+0x1b): In function `main':
: undefined reference to `std::cout'
/tmp/cc4E4x2P.o(.text+0x20): In function `main':
: undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
/tmp/cc4E4x2P.o(.text+0x49): In function `__static_initialization_and_destruction_0(int, int)':
: undefined reference to `std::ios_base::Init::Init[in-charge]()'
/tmp/cc4E4x2P.o(.text+0x7a): In function `__tcf_0':
: undefined reference to `std::ios_base::Init::~Init [in-charge]()'
/tmp/cc4E4x2P.o(.eh_frame+0x11): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status
09:58 aluser@alf:~/test/c$ g++ -o hello hello.cpp
09:59 aluser@alf:~/test/c$
This is on debian sarge. not quite sure what's going on, but the two aren't quite equivalent.
Even if we get the -L and -Is figured out, I'm fairly sure that g++ will compile c++ files with a .c extension, where gcc will give you a bunch of syntax errors.
Mostly the same code, but there are enough differences that you want to get in the habit of using the right one : )
Mostly the same code, but there are enough differences that you want to get in the habit of using the right one : )
Technically speaking it is 100% the same source code. The difference is when you use a front end wrapper like g++ it assumes you are compiling c++ code regardless of the file extension you use. When you just invoke "gcc" it decides what language the source is in based on the extension of the file.
".c" are assumed to be C files.
".cxx", ".cc", ".cpp", ".C", ect. are assumed to be C++.
Check out the Compiling C++ section of the GCC manual for more info.
Quote:
g++ is a program that calls GCC with the default language set to C++, and automatically specifies linking against the C++ library. On many systems, g++ is also installed with the name c++.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.