1

when I try to compile a simple hello world code I get some architecture error from gcc

$ vim test.cpp

#include <iostream>

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

$ gcc test.cpp -o test.o

Undefined symbols for architecture x86_64:
  "std::cout", referenced from:
      _main in ccyEpFri.o
  "std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)", referenced from:
      _main in ccyEpFri.o
  "std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)", referenced from:
      _main in ccyEpFri.o
  "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> >&))", referenced from:
      _main in ccyEpFri.o
  "std::ios_base::Init::Init()", referenced from:
      __static_initialization_and_destruction_0(int, int) in ccyEpFri.o
  "std::ios_base::Init::~Init()", referenced from:
      __static_initialization_and_destruction_0(int, int) in ccyEpFri.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status

The gcc I have is here: /usr/local/bin/gcc

$ gcc --version

gcc (GCC) 4.9.2 20141029 (prerelease)
Copyright (C) 2014 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

System setup: Mac OSX yosemit 10.10.2 GCC from http://hpc.sourceforge.net/

My original gcc 4.2 (the default on mac) was giving me couldn't understand kern.osversion '14.1.0' That's why I went for http://hpc.sourceforge.net/

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
Morteza Shahriari Nia
  • 1,392
  • 18
  • 24

2 Answers2

2

Use g++ instead of gcc.

g++ is a *nix-based C++ compiler. gcc is a compiler for C, C++, Objective-C, Fortran, Java, Ada, and Go. The libraries used are different. Your code will work with g++ not gcc.

tourniquet_grab
  • 792
  • 9
  • 14
1

You should compile with

g++ -Wall -Wextra -g  test.cpp -o mytestprog

then run ./mytestprog and use gdb for debugging it.

If that command does not work, you might add -v after g++ (to be shown what is happening), and it would be a symptom that your g++ was wrongly configured or installed. Of course, do a which g++ to be sure that the g++ is the one you want.

Read my hints here about compiling your GCC. I believe that the binaries you've got from http://hpc.sourceforge.net/ do not fit your operating system. I would suggest to carefully build, compile and install, GCC from its source code (downloadable via these mirrors). It probably is not very easy (for newbies). Be sure to build outside of the source tree. Read carefully the install instructions.

Community
  • 1
  • 1
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • Same error $ g++ -Wall -Wextra -g test.cpp -o mytestprog ld: entry point (start) undefined. Usually in crt1.o for architecture x86_64 collect2: error: ld returned 1 exit status – Morteza Shahriari Nia Feb 27 '15 at 20:14