1

I'm trying to write a static library for assembler. It's not working, though. The library builds fine, but when I try to build the program, this happens:

$ ld -o hello -L../myasm -lmyasm hello.o
hello.o: In function `_start':
(.text+0x18): undefined reference to `exit'

I poked around, and that further mystified me.

$ nm ../myasm/libmyasm.a
myasm.o:
00000000 T exit
$ nm hello.o
00000000 T _start
         U exit
00000000 d message

Any idea what's happening?

My code:

hello.s

#; This is a hello world program, in assembler.

.extern exit

.data
message:
    .byte 14
    .ascii "Hello, World!\n"

.text
.global _start

_start:
    #; First, write the message.
    mov $4, %eax            #; write syscall number
    mov $1, %ebx            #; stdout file descriptor
    mov $message+1, %ecx    #; message address
    mov message, %dl        #; we only want one byte, so %dl
    int $0x80

    #; Now, we need to exit.
    call exit

hello/Makefile

hello: hello.o
        ld -o hello -L../myasm -lmyasm hello.o
hello.o: hello.s
        as -o hello.o hello.s
run: hello
        ./hello
clean:
        rm hello.o hello

myasm.s

#; lib.s

.text
.global exit

exit:
    mov $1, %eax #; exit syscall #
    mov $0, %ebx #; success
    int $0x80

myasm/Makefile

libmyasm.a: myasm.o
        ar cr libmyasm.a myasm.o
myasm.o: myasm.s
        as -o myasm.o myasm.s
clean:
        rm myasm.o libmyasm.a
2
  • Try using gcc rather than ld. You didn't mention anything about the tools you're using, which makes it impossible to give a certain answer.
    – Gene
    Commented Aug 13, 2014 at 23:03
  • @Gene I posted my Makefiles.
    – tbodt
    Commented Aug 13, 2014 at 23:06

1 Answer 1

1

In hello's Makefile, move the object file hello.o before -lmyasm as:

hello: hello.o
        ld -o hello hello.o -L../myasm -lmyasm
...

see -l reference here of how the search of symbols is done, 3.13 Options for Linking:

-llibrary
-l library

...

It makes a difference where in the command you write this option; the linker searches and processes libraries and object files in the order they are specified. Thus, foo.o -lz bar.o searches library z after file foo.o but before bar.o. If bar.o refers to functions in z, those functions may not be loaded.

...

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.