Page 1 of 1

[SOLVED] clang attach own library

Posted: Thu Nov 10, 2022 5:20 am
by CorruptedByCPU
Hey! I'm trying to bind a library to a program, but clang insistently does not see it or doesn't recognize it.

Can someone give me a hint?

clang-14: warning: -lfont: 'linker' input unused [-Wunused-command-line-argument]
clang-14: warning: argument unused during compilation: '-L.' [-Wunused-command-line-argument]
ld: main.o: in function `entry':
main.c:(.text+0x6): undefined reference to `font_char_length'


font.h

Code: Select all

extern uint8_t font_char_length( uint8_t character );
font.c

Code: Select all

#include	"stdint.h"
#include	"stddef.h"

uint8_t font_char_length( uint8_t character ) {
	return character + 1;
}
main.c

Code: Select all

#include	"stdint.h"
#include	"stddef.h"

#include	"font.h"

void entry() {
	uint8_t length = font_char_length( 0x30 );
}
make.sh

Code: Select all

CFLAGS="-Ofast -march=x86-64 -nostdlib -fomit-frame-pointer -fno-builtin -fno-stack-protector -m64 -mno-red-zone -fno-asynchronous-unwind-tables"
LDFLAGS="-nostdlib -zmax-page-size=0x1000 -static -no-dynamic-linker"

clang -c -fpic font.c ${CFLAGS}
clang -shared -o libfont.so font.o ${CFLAGS}

clang -L. -c main.c -o main.o -lfont ${CFLAGS}
ld main.o -o main -T linker.software ${LDFLAGS}

Re: clang attach own library

Posted: Thu Nov 10, 2022 9:35 am
by nullplan
You are giving linker command line flags to the compiler. That will not work. Try removing the -L and -l options from the second-to-last line and adding them to the last line of make.sh.

Re: clang attach own library

Posted: Thu Nov 10, 2022 11:19 am
by CorruptedByCPU
nullplan wrote:You are giving linker command line flags to the compiler. That will not work. Try removing the -L and -l options from the second-to-last line and adding them to the last line of make.sh.
It worked! Thank you :)

My mistake was that I saw an example from GCC and the linker was built-in there.

Re: [SOLVED] clang attach own library

Posted: Thu Nov 10, 2022 11:32 am
by Octocontrabass