Solved Error linking Hello World

Pretty standard:

Code:
#include <iostream>

int main()
{
   std::cout << "Hello, World!\n";
}

I'm trying to do it in two steps - compile then link.

$ c++ -c hello.cpp

This produces an object file - hello.o

$ ld.lld -l c++ -L /usr/lib -o hello hello.o

This fails with two errors:
Code:
undefined symbol: _Unwind_Resume
undefined symbol: strlen
It looks like I need to specify one or two more libraries. Which?

-
 
You may see the actual ld-command with `c++ -v`:

Code:
% c++ -v -o hello ./hello.o
...
 "/usr/bin/ld" --eh-frame-hdr -dynamic-linker /libexec/ld-elf.so.1 --hash-style=both --enable-new-dtags -o hello /usr/lib/crt1.o /usr/lib/crti.o /usr/lib/crtbegin.o -L/usr/lib ./hello.o -lc++ -lm -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed /usr/lib/crtend.o /usr/lib/crtn.o
 
the startup code for c++ is different than for c...there are different/additional objects that need to be linked...I don't know off the top of my head what they are named in bsd because only program infrequently in bsd.
 
Why not simplify the second step to "c++ -o hello hello.o". When you use the c++ command as a linker, it knows where to find the correct libraries for the language automatically.

I'm sure that it is possible to get the same effect with ld.lld, as shkal showed, but why that complexity.
 
Why not simplify the second step to "c++ -o hello hello.o". When you use the c++ command as a linker, it knows where to find the correct libraries for the language automatically.

I'm sure that it is possible to get the same effect with ld.lld, as shkal showed, but why that complexity.
I guess that he just would like to understand how these two steps work independently so to get a clue about processes hidden behind the general compiler interface.
 
If you invoke the linkah manually you have to manually specify the appropriate language runtime.

Alternatively, you can use the language frontend for the linker command line:
g++ -o foo foo.o
 
Back
Top