LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   my first C++ program, errors while compiling (https://www.linuxquestions.org/questions/programming-9/my-first-c-program-errors-while-compiling-235264/)

lu543285 09-26-2004 12:02 AM

my first C++ program, errors while compiling
 
I'm looking for an answer for the following problem (very difficult against all appearances):

How to compile and run my first C++ program?.

Here is the code (very original..):

#include<iostream>

int main()
{
std::cout << "Hello World!" << std::endl;
return 0;
}

I run it by writing: gcc hello.cpp

Here are the errors:

/home/staszek/tmp/ccJZcrMe.o(.text+0x14): In function `main':
: undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)'
/home/staszek/tmp/ccJZcrMe.o(.text+0x21): In function `main':
: undefined reference to `std::cout'
/home/staszek/tmp/ccJZcrMe.o(.text+0x26): 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*)'
/home/staszek/tmp/ccJZcrMe.o(.text+0x2f): In function `main':
: undefined reference to `std::basic_ostream<char, std::char_traits<char> >::operator<<(std::basic_ostream<char, std::char_traits<char> >& (*)(std::basic_ostream<char, std::char_traits<char> >&))'
/home/staszek/tmp/ccJZcrMe.o(.text+0x5c): In function `__static_initialization_and_destruction_0(int, int)':
: undefined reference to `std::ios_base::Init::Init[in-charge]()'
/home/staszek/tmp/ccJZcrMe.o(.text+0x8b): In function `__tcf_0':
: undefined reference to `std::ios_base::Init::~Init [in-charge]()'
/home/staszek/tmp/ccJZcrMe.o(.eh_frame+0x11): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status

I use Mandrake 10.0 and?... Well, I'd just like someone to answer.

sether 09-26-2004 12:06 AM

i think "g++" if it's a c++ program. you might also want to specify the file it will make with the "-o" option.

lu543285 09-26-2004 12:33 AM

I've got it. That wasn't so difficult, was it?.
Now I'd like to link it and make it a runnable program.
Do I have to do it on my own or maybe there is a person who would be willing to give me some hints?.

sether 09-26-2004 12:40 AM

what do you mean? are you saying you want to only have to type "hello" at the commandline in any directory for it to work? like having it as an actual program on your computer?

lu543285 09-26-2004 12:41 AM

Ay, Ay.

lu543285 09-26-2004 12:45 AM

Yes, that's exactly what I want.

lu543285 09-26-2004 01:00 AM

I've just went through g++ man and still cannot find it.
I think it must be some problem with linking.

sether 09-26-2004 01:11 AM

i think "link" or "symlink" will work with that, but you've gotta play around with it. i don't really know where you would link it to though (what directory).

itsme86 09-26-2004 02:54 AM

It should already be linked into an executable, but if you don't have . (current directory) in your path then you have to tell your shell where to find it. For example, try typing: ./hello

lu543285 09-26-2004 03:35 AM

How to make my shell find the right directory with executable file in C++?.
 
I have compiled my hello.cpp program and I'd like to know where to find executable (if it's been created).
I have compiled it with 'g++ -o hello.cpp' command. I've got hello.o in my current directory but there's no
executable there. Where could I find executable?.

sether 09-26-2004 03:38 AM

you have to say the name you want after the -o, then the .cpp file. for example, if i wanted to compile hello.cpp and make an executable out of it called "hello", i would do this:

g++ -o hello hello.cpp

the newly created "hello" executable file will be in the same directory after the compile is through. execute it by typing

hello

and that's it.

lu543285 09-26-2004 03:42 AM

Thanks
 
Thank you all for posting.


All times are GMT -5. The time now is 08:15 PM.