tomolt / libschrift

A lightweight TrueType font rendering library
ISC License
471 stars 32 forks source link

linkers needs the libraries *after* the files #2

Closed glysbaysb closed 4 years ago

glysbaysb commented 4 years ago

The order libraries are passed to the linker matters, more specfially all external libraries should be at the end.

The current master can not be built

cc -Os -lm sftdemo.o -o sftdemo -L/usr/lib/X11 -L. -lX11 -lXrender -lschrift
/usr/bin/ld: ./libschrift.a(schrift.o): in function `draw_line.isra.0':
schrift.c:(.text+0x334): undefined reference to `floor'
/usr/bin/ld: schrift.c:(.text+0x3ea): undefined reference to `floor'
/usr/bin/ld: ./libschrift.a(schrift.o): in function `proc_outline':
schrift.c:(.text+0x969): undefined reference to `floor'
/usr/bin/ld: schrift.c:(.text+0x98e): undefined reference to `floor'
/usr/bin/ld: schrift.c:(.text+0x9aa): undefined reference to `ceil'
/usr/bin/ld: schrift.c:(.text+0x9d6): undefined reference to `ceil'
collect2: error: ld returned 1 exit status
make: *** [Makefile:17: sftdemo] Error 1

After applying a simple patch it does build.

$ make
cc -Os -std=c99 -pedantic -Wall -Wextra -I./  -c -o schrift.o schrift.c
ar rc libschrift.a schrift.o
ranlib libschrift.a
cc -c -Os -std=c99 -pedantic -Wall -Wextra sftdemo.c -o sftdemo.o -I./ -I/usr/include/X11
cc -Os -lm sftdemo.o -o sftdemo -L/usr/lib/X11 -L. -lX11 -lXrender -lschrift -lm
cc -c -Os -std=c99 -pedantic -Wall -Wextra stress.c -o stress.o -I./
cc -Os -lm stress.o -o stress -L. -lschrift -lm

For completness sake my toolchain is:

$ gcc --version
gcc (Ubuntu 9.2.1-9ubuntu2) 9.2.1 20191008
$ ld --version
GNU ld (GNU Binutils for Ubuntu) 2.33
glysbaysb commented 4 years ago

The patch being:

$ cat 0001-link-order-matters.patch 
From 1cce2cfd176414770f3c2d6cf6920562c77c508e Mon Sep 17 00:00:00 2001
From: Geert Ijewski <gm.ijewski@web.de>
Date: Tue, 21 Apr 2020 19:04:25 +0200
Subject: [PATCH] link order matters

---
 Makefile | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/Makefile b/Makefile
index 0964497..72f2f50 100644
--- a/Makefile
+++ b/Makefile
@@ -14,12 +14,12 @@ libschrift.a: schrift.o
 schrift.o: schrift.h

 sftdemo: sftdemo.o libschrift.a
-       $(LD) $(LDFLAGS) $< -o $@ -L$(X11LIB) -L. -lX11 -lXrender -lschrift
+       $(LD) $(LDFLAGS) $< -o $@ -L$(X11LIB) -L. -lX11 -lXrender -lschrift -lm
 sftdemo.o: sftdemo.c schrift.h arg.h
        $(CC) -c $(CFLAGS) $< -o $@ $(CPPFLAGS) -I$(X11INC)

 stress: stress.o libschrift.a
-       $(LD) $(LDFLAGS) $< -o $@ -L. -lschrift
+       $(LD) $(LDFLAGS) $< -o $@ -L. -lschrift -lm
 stress.o: stress.c schrift.h arg.h
        $(CC) -c $(CFLAGS) $< -o $@ $(CPPFLAGS)

-- 
2.20.1
tomolt commented 4 years ago

Interesting. I'm basically using the same toolchain but I never hit this problem. I don't have time to do this right now, but I'll apply the patch to master ASAP. Thank you very much!