4

I am getting a ld: symbol(s) not found for architecture x86_64 error. I did do a thorough search through previous threads, but did not find a solution.

As a test I am compiling a program test.cpp:

#include <iostream>

using namespace std;

int main(){
 return 0;
}

with the Makefile:

CXX=g++
CXXFLAGS=-g -Wall -W -Wconversion -Wshadow -Wcast-qual -Wwrite-strings $(shell root-config --cflags --glibs)
CPPFLAGS+=-MMD -MP
LDFLAGS=-g $(shell root-config --ldflags)
LDLIBS=$(shell root-config --libs)

test.o: test.cpp

test: test.o
-include test.d

Running make test in my linux computer produces the following correct output:

CAM ~/Wenu $ make test
g++ -g -Wall -W -Wconversion -Wshadow -Wcast-qual -Wwrite-strings -pthread -m64 -I/cvmfs/lhcb.cern.ch/lib/RootConfig/pro/x86_64-slc5-gcc46-opt/root/include -L/cvmfs/lhcb.cern.ch/lib/RootConfig/pro/x86_64-slc5-gcc46-opt/root/lib -lGui -lCore -lCint -lRIO -lNet -lHist -lGraf -lGraf3d -lGpad -lTree -lRint -lPostscript -lMatrix -lPhysics -lMathCore -lThread -pthread -lm -ldl -rdynamic -MMD -MP  -c -o test.o test.cpp
cc -g -m64  test.o  -L/cvmfs/lhcb.cern.ch/lib/RootConfig/pro/x86_64-slc5-gcc46-opt/root/lib -lCore -lCint -lRIO -lNet -lHist -lGraf -lGraf3d -lGpad -lTree -lRint -lPostscript -lMatrix -lPhysics -lMathCore -lThread -pthread -lm -ldl -rdynamic -o test
CAM ~/Wenu $

Compiling it on my Mac fails in the linking stage with a ld: symbol(s) not found for architecture x86_64 error:

MAC ~/Transmit/Wenu $ make test
g++ -g -Wall -W -Wconversion -Wshadow -Wcast-qual -Wwrite-strings -pthread -m64 -I/opt/local/root-v5-34-00/include -L/opt/local/root-v5-34-00/lib -lGui -lCore -lCint -lRIO -lNet -lHist -lGraf -lGraf3d -lGpad -lTree -lRint -lPostscript -lMatrix -lPhysics -lMathCore -lThread -lpthread -Wl,-rpath,/opt/local/root-v5-34-00/lib -lm -ldl -MMD -MP  -c -o test.o test.cpp
i686-apple-darwin11-llvm-g++-4.2: -lGui: linker input file unused because linking not done
i686-apple-darwin11-llvm-g++-4.2: -lCore: linker input file unused because linking not done
i686-apple-darwin11-llvm-g++-4.2: -lCint: linker input file unused because linking not done
i686-apple-darwin11-llvm-g++-4.2: -lRIO: linker input file unused because linking not done
i686-apple-darwin11-llvm-g++-4.2: -lNet: linker input file unused because linking not done
i686-apple-darwin11-llvm-g++-4.2: -lHist: linker input file unused because linking not done
i686-apple-darwin11-llvm-g++-4.2: -lGraf: linker input file unused because linking not done
i686-apple-darwin11-llvm-g++-4.2: -lGraf3d: linker input file unused because linking not done
i686-apple-darwin11-llvm-g++-4.2: -lGpad: linker input file unused because linking not done
i686-apple-darwin11-llvm-g++-4.2: -lTree: linker input file unused because linking not done
i686-apple-darwin11-llvm-g++-4.2: -lRint: linker input file unused because linking not done
i686-apple-darwin11-llvm-g++-4.2: -lPostscript: linker input file unused because linking not done
i686-apple-darwin11-llvm-g++-4.2: -lMatrix: linker input file unused because linking not done
i686-apple-darwin11-llvm-g++-4.2: -lPhysics: linker input file unused because linking not done
i686-apple-darwin11-llvm-g++-4.2: -lMathCore: linker input file unused because linking not done
i686-apple-darwin11-llvm-g++-4.2: -lThread: linker input file unused because linking not done
i686-apple-darwin11-llvm-g++-4.2: -lpthread: linker input file unused because linking not done
i686-apple-darwin11-llvm-g++-4.2: -rpath: linker input file unused because linking not done
i686-apple-darwin11-llvm-g++-4.2: /opt/local/root-v5-34-00/lib: linker input file unused because linking not done
i686-apple-darwin11-llvm-g++-4.2: -lm: linker input file unused because linking not done
i686-apple-darwin11-llvm-g++-4.2: -ldl: linker input file unused because linking not done
cc -g -m64  test.o  -L/opt/local/root-v5-34-00/lib -lCore -lCint -lRIO -lNet -lHist -lGraf -lGraf3d -lGpad -lTree -lRint -lPostscript -lMatrix -lPhysics -lMathCore -lThread -lpthread -Wl,-rpath,/opt/local/root-v5-34-00/lib -lm -ldl -o test
Undefined symbols for architecture x86_64:
  "std::ios_base::Init::Init()", referenced from:
      __static_initialization_and_destruction_0(int, int) in test.o
  "std::ios_base::Init::~Init()", referenced from:
      ___tcf_0 in test.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [test] Error 1

Any help would be very appreciated.

In case any of this is relevant, I have a 64-bit Mac with OS X 10.7.5 and I have a compiler that came with XCode's command line tools:

MAC ~/Transmit/Wenu $ clang --version
Apple clang version 4.1 (tags/Apple/clang-421.11.66) (based on LLVM 3.1svn)
Target: x86_64-apple-darwin11.4.2
Thread model: posix

ROOT is a collection of C++ libraries that I use in my programs.

mareks
  • 481
  • 2
  • 5
  • 13
  • Try using `c++` instead of `cc`. – James M Jan 01 '13 at 23:49
  • Typically, if the file is called "name.c" rather than "name.cpp", it will compile with cc rather than c++. But of course, if you actually want to use C++ features, you'll need to use a C++ library. I'm pretty sure you are missing the C++ libraries from your installation - that may well be a separate set of files to install, and I don't know what you need to do on a Mac to install that (I'm sure it's quite easy...) – Mats Petersson Jan 02 '13 at 00:03
  • @MatsPetersson but the code compiles if I manually enter the commands make produces and subsitute cc with c++. Also my file has a cpp extension so I don't understand why cc is used. How do I set it to c++ in the Makefile? – mareks Jan 02 '13 at 00:08
  • @JamesMcLaughlin Yes that got rid of the error. How do I set it to use c++ instead of cc in my Makefile? – mareks Jan 02 '13 at 00:09
  • Ah, I just spotted what the problem is. You should use g++ for the LINKING stage too. set `LD=g++` in your makefile, and it should sort itself out. – Mats Petersson Jan 02 '13 at 00:17

1 Answers1

3

Running make test in my linux computer produces the following correct output

This only works by accident: you are linking a C++ program with cc. It will stop working on Linux if you use more of C++, e.g. try putting cout << "Hello" << endl; into your main.

Also my file has a cpp extension so I don't understand why cc is used.

Because the default make rule to produce test from test.o uses $CC, and not $CXX.

You can fix this in one of several ways:

  • force $CC to also be g++, or
  • change the Makefile to use non-default rule when making test from test.o, or
  • make test depend on test.cpp (make will use correct default rule if you do that).
Employed Russian
  • 199,314
  • 34
  • 295
  • 362