Connecting Tech Pros Worldwide Forums | Help | Site Map

Why is my gcc behaving strangely?

ambrnewlearner's Avatar
Expert
 
Join Date: Jan 2008
Location: A city in my country ;)
Posts: 855
#1: Nov 21 '08
Hello,

This is a simple C++ code (which is there in C++ primer):

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2.  
  3. int main()
  4. {
  5. std :: cout << "Enter the numbers: " << std :: endl;
  6. int v1, v2;
  7.  
  8. std :: cin >> v1 >> v2;
  9. std :: cout << "The sum of " << v1 << " and " << v2<< " is " << v1 + v2 << std :: endl;
  10.  
  11. return 0;
  12. }
Now when I try compiling with gcc 3.3.5 under OpenBSD 4.4, I get following output at terminal:

Expand|Select|Wrap|Line Numbers
  1. $gcc -o file file.cpp
  2. try.cpp:13:2: warning: no newline at end of file
  3. /tmp//ccF18167.o(.text+0x1c): In function `main':
  4. : 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> >&)'
  5. /tmp//ccF18167.o(.text+0x29): In function `main':
  6. : undefined reference to `std::cout'
  7. /tmp//ccF18167.o(.text+0x2e): In function `main':
  8. : 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*)'
  9. /tmp//ccF18167.o(.text+0x37): In function `main':
  10. : 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> >&))'
  11. /tmp//ccF18167.o(.text+0x4d): In function `main':
  12. : undefined reference to `std::cin'
  13. /tmp//ccF18167.o(.text+0x52): In function `main':
  14. : undefined reference to `std::basic_istream<char, std::char_traits<char> >::operator>>(int&)'
  15. /tmp//ccF18167.o(.text+0x5b): In function `main':
  16. : undefined reference to `std::basic_istream<char, std::char_traits<char> >::operator>>(int&)'
  17. /tmp//ccF18167.o(.text+0x66): In function `main':
  18. : 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> >&)'
  19. /tmp//ccF18167.o(.text+0x99): In function `main':
  20. : undefined reference to `std::cout'
  21. /tmp//ccF18167.o(.text+0x9e): In function `main':
  22. : 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*)'
  23. /tmp//ccF18167.o(.text+0xa7): In function `main':
  24. : undefined reference to `std::basic_ostream<char, std::char_traits<char> >::operator<<(int)'
  25. /tmp//ccF18167.o(.text+0xb0): In function `main':
  26. : 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*)'
  27. /tmp//ccF18167.o(.text+0xb9): In function `main':
  28. : undefined reference to `std::basic_ostream<char, std::char_traits<char> >::operator<<(int)'
  29. /tmp//ccF18167.o(.text+0xc2): In function `main':
  30. : 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*)'
  31. /tmp//ccF18167.o(.text+0xcb): In function `main':
  32. : undefined reference to `std::basic_ostream<char, std::char_traits<char> >::operator<<(int)'
  33. /tmp//ccF18167.o(.text+0xd4): In function `main':
  34. : 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> >&))'
  35. /tmp//ccF18167.o(.text+0x11e): In function `__static_initialization_and_destruction_0(int, int)':
  36. : undefined reference to `std::ios_base::Init::Init()'
  37. /tmp//ccF18167.o(.text+0x14d): In function `__tcf_0':
  38. : undefined reference to `std::ios_base::Init::~Init()'
  39. /tmp//ccF18167.o(.eh_frame+0x11): undefined reference to `__gxx_personality_v0'
  40. collect2: ld returned 1 exit status
  41.  
Now I dont know the reason for this. I even tried using gcc under linux, MinGW under windows but got similar compiler output.
If I try compiling with g++, all is fine. But as man page says, gcc and g++ are same. So what is the problem?

Thanks.....
AmbrNewlearner

Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#2: Nov 21 '08

re: Why is my gcc behaving strangely?


gcc and g++ are not the same. g++ is for compiling c++ files while gcc is for c files. gcc will compile as c++ when it sees a file extension it regards to be c++ but it will not link the object files created during the compilation correctly itself, which is why you are getting those errors.
Reply