0

I'm trying to have my first embryo of C++ compiled with clang++-4.0

my code:

//main.cpp
#include <iostream>

// this line is to deal with the link error:
// "undefined reference to symbol '__cxa_thread_atexit@@CXXABI..."
// cf. http://stackoverflow.com/q/29322666/356440
extern "C" int __cxa_thread_atexit(void (*func)(), void *obj,
                                   void *dso_symbol) {
  int __cxa_thread_atexit_impl(void (*)(), void *, void *);
  return __cxa_thread_atexit_impl(func, obj, dso_symbol);
}

int main ()
{
    return 0;
}

and I compile with clang++-4.0, and link with ld this way:

clang++-4.0 -c main.cpp -o main.o
ld -o test main.o -lc++ -lc++abi -lc

It's as if I'm missing to link with another library, but I don't know which one or ones.

Do you see what's wrong or missing ? I think I'm missing some beginner stuff for doing c++ stuffs with clang++ on linux.

Compilation errors:

ld: warning: cannot find entry symbol _start; defaulting to 0000000000400380 main.o: In function __cxx_global_var_init': main.cpp:(.text.startup+0x13): undefined reference to std::ios_base::Init::Init()' main.cpp:(.text.startup+0x19): undefined reference to std::ios_base::Init::~Init()' main.cpp:(.text.startup+0x2d): undefined reference to__dso_handle' //usr/lib/x86_64-linux-gnu/libc++.so: undefined reference to _Unwind_GetRegionStart' //usr/lib/x86_64-linux-gnu/libc++.so: undefined reference to_Unwind_RaiseException' //usr/lib/x86_64-linux-gnu/libc++.so: undefined reference to _Unwind_SetIP' //usr/lib/x86_64-linux-gnu/libc++.so: undefined reference to_Unwind_GetLanguageSpecificData' //usr/lib/x86_64-linux-gnu/libc++.so: undefined reference to _Unwind_Resume' //usr/lib/x86_64-linux-gnu/libc++.so: undefined reference to_Unwind_GetIP' //usr/lib/x86_64-linux-gnu/libc++.so: undefined reference to _Unwind_SetGR' //usr/lib/x86_64-linux-gnu/libc++.so: undefined reference to _Unwind_DeleteException'

Stephane Rolland
  • 38,876
  • 35
  • 121
  • 169
  • Possible dublicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – πάντα ῥεῖ May 14 '17 at 22:04
  • @πάνταῥεῖ I don't think it's a dupplicate. Here the undefined reference is not linked to my code. Here I try to compile a main function that simply returns 0... nothing else... – Stephane Rolland May 14 '17 at 22:07
  • That's why I didn't dupe hammer your question. – πάντα ῥεῖ May 14 '17 at 22:08
  • What happens when you add `-lcpp` _at the end_ of the command line? Is this a version of `clang` installed via a package manager, or did you build it from source? – Qix - MONICA WAS MISTREATED May 15 '17 at 08:56
  • installed via package manager, with apt.llvm.org added to my apt sourcelist. When I add at linking `-lcpp`, `ld` complains it doesn't know about it. – Stephane Rolland May 15 '17 at 09:30

1 Answers1

1

You should not link C++ applications with ld. Link with the C++ compiler executable because it links in all the necessary libraries:

clang++-4.0 -c main.cpp -o main.o
clang++-4.0 -o test main.o
Maxim Egorushkin
  • 131,725
  • 17
  • 180
  • 271