[LLVMdev] Using llvm-gcc with a simple program and the '-c' option

Wink Saville wink at saville.com
Sun Feb 26 23:44:16 PST 2006


Misha Brukman wrote:
> On Sun, Feb 26, 2006 at 10:00:18PM -0800, Wink Saville wrote:
>   
>   
[snip]
> If you're using -c, you're telling LLVM that there are other modules you
> will link into the executable.  Thus, LLVM does not know whether there
> will be static ctors/dtors to run or not, so there's the call to
> __main() from main.
>
> __main() gets linked into your program if you use gccld (which is what
> llvm-gcc uses) or llvm-ld manually.  The __main() itself comes from one
> of the runtime libraries: llvm/runtime/GCCLibraries/crtend/crtend.c .
>   

Misha,

Great now I understand, one more related question. This actually started 
trying to understand how llvm-gcc, llvm-ar, llvm-ld & llc work together. 
With the help I've received I've successfully used them to create a 
native executable of 3 files, t1.c, t1sub1.c t1sub2.c, where t1.c calls 
a subroutine in t1sub1.c and t1sub2.c.

I then do the following in my makefile:

  llvm-gcc -c t1.c -o t1.bc
  llvm-gcc -c t1sub1.c -o t1sub1.bc
  llvm-gcc -c t1sub2.c -o t1sub2.bc
  llvm-ar r t1.a t1sub1.bc t1sub2.bc
  llvm-ar: creating t1.a
  llvm-ld -o t1.app t1.bc t1.a /opt/llvm-1.6/llvm-gcc/lib/libcrtend.a
  llvm-ld: warning: Cannot find library 'crtend'
  llc t1.app.bc -o t1.app.s
  gcc -m32 t1.app.s -o t1

So a few minor problems, the first is really minor the llvm-ld command 
generates the t.app script that runs t1.app.bc, obviously no big deal, 
but isn't what I expected and would seem to be to be unnecessary. 
Another is the warning:

  llvm-ld: warning: Cannot find library 'crtend'

I tried:

  llvm-ld  -o t1.app t1.bc t1.a -L/opt/llvm-1.6/llvm-gcc/lib -lcrtend

But that gives me two cannot find library warnings and __main is 
undefined. Again, the when using libcrtend.a directly everything works 
bit gives the warning which seems very odd as it is "obviously" there. 
Also, why does the second version not work?

Thanks again,

Wink




More information about the llvm-dev mailing list