Results 1 to 7 of 7
Whenever I try to compile a c++ program using gcc using command
gcc program.cpp
, it give me errors even if it is with return statement only and no other ...
- 07-05-2008 #1Just Joined!
- Join Date
- Jul 2008
- Location
- Delhi, India
- Posts
- 72
Tell me how to use GCC
Whenever I try to compile a c++ program using gcc using command
gcc program.cpp
, it give me errors even if it is with return statement only and no other statement is there. Though if I ask it to create the object file only, it does so without any problem.Please tell what is the correct syntax. I am able to compile c programs.
A new member of the forum so I expect that you all will understand that it will take me some time to learn the rules of the forum.
- 07-05-2008 #2
Hello and welcome,
it would have been nice if you had given us the exact errors so we don't have to guess what the problem could be.
GCC is only a front end which dispatches the source files to the correct compiler. Make sure the c++ compiler is installed (type g++ --version).
When it is, the syntax should work:
Code:>$ cat program.cpp int main() { return 33; } >$ ./a.out >$ echo $? 33
- 07-06-2008 #3Just Joined!
- Join Date
- Jul 2008
- Location
- Delhi, India
- Posts
- 72
Thanks for the reply and it worked with g++ command.
#include<iostream>
int main()
{
std::cout<<"HI";
return 0;
}
When I used gcc I got the output as shown in the file. TextFile
The forum doesn't allow to post the code in the post itself.
- 07-06-2008 #4
Hello,
are there no copy&paste facilities on you desktop?
To whom else it may be of interest, here is the content of the file thinkfree has uploaded:
Now to the actual problem. Contrary to what you have stated above, the program indeed does more than just returning a number.Code:gcc new.cpp /tmp/ccqJw58y.o: In function `__static_initialization_and_destruction_0(int, int)': new.cpp:(.text+0x23): undefined reference to `std::ios_base::Init::Init()' /tmp/ccqJw58y.o: In function `__tcf_0': new.cpp:(.text+0x6c): undefined reference to `std::ios_base::Init::~Init()' /tmp/ccqJw58y.o: In function `main': new.cpp:(.text+0x8e): undefined reference to `std::cout' new.cpp:(.text+0x93): 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/ccqJw58y.o:(.eh_frame+0x11): undefined reference to `__gxx_personality_v0' collect2: ld returned 1 exit status
It uses the std:cout class from the c++ runtime. The definitions are known by including the headers, which you did.
But the actual code has to be linked to your executable. This is done by adding the -lstdc++ argument to the command line.
Whenever you get a "undefined reference to" this is an indicator you have forgotten a required library. (The standard C++ library in this case.)
- 07-07-2008 #5Just Joined!
- Join Date
- Jul 2008
- Location
- Delhi, India
- Posts
- 72
It would have been better had you been a little polite. I tried to paste the code but the forum told that I can't put more than 6 images in my post. Still thanks for providing help.
- 07-07-2008 #6
I'm not sure how he was rude. And for future reference, just copy the text of the code and paste it here. Not sure what you were doing to get an image error.
In other news, GNU-Fan proposes one solution (-lstdc++). Another option is to, instead of using the "gcc" command, use the "g++" command. Both "gcc" and "g++" invoke the same program, but with different settings, such as libraries to load by default, and default language.DISTRO=Arch
Registered Linux User #388732
- 07-07-2008 #7Just Joined!
- Join Date
- Jul 2008
- Location
- Delhi, India
- Posts
- 72
I had copied and tried to paste the code but it didn't work.


Reply With Quote