Question

I'm trying to make a kind of my_malloc.C but when I try to compile, this happened :

"/usr/bin/ld: block_addr.o: relocation R_X86_64_PC32 against symbol `baseb' can not be used when making a shared object; recompile with -fPIC

/usr/bin/ld: final link failed: Bad value"

Well, I search a lot in google, i read this topic :

"How do I share a variable between source files in c"

but this didn't solved my problem.

So, I share with you some piece of code.

First : the Makefile :

 CC      =       gcc -fPIC

 RM      =       rm -f

 NAME    =       libmy_malloc_$(HOSTTYPE).so

 SRCS    =       block_addr.c \
            block_ope.c \
            malloc.c \
            my_free.c

 OBJS    =       $(SRCS:.c=.o)

 all:            $(NAME)

 $(NAME):        $(OBJS)
            $(CC) -fPIC -shared -o $(NAME) $(OBJS)

 clean:
            $(RM) $(OBJS)

 fclean:         clean
            $(RM) $(NAME)

 re:             fclean all

.PHONY:         all clean fclean re

in my .h i have it :

extern void             *baseb;

and on my malloc.c :

void            *baseb;

void            *malloc(size_t size)
{
 t_memblock    b;
 t_memblock    last;
 size_t        s;

  baseb = NULL;
 ..
}

and I use "baseb" in other function of my program. I tried to declare "extern void *baseb;" on the begining of each files, but the compilation's error is still the same.

Can you help me ?

Was it helpful?

Solution

/usr/bin/ld: block_addr.o: relocation R_X86_64_PC32 against symbol `baseb' can not be used when making a shared object; recompile with -fPIC

This error indicates that you have not used -fPIC when compiling all the objects to go your shared library.

The part of a Makefile that you have shown does link, not compile, -fPIC has no effect there.

PS.Example:

var.c

int var = 10;

Compile/link with:

gcc -fPIC -shared -o libvar.so var.c

main.c

extern int var;
int main() { return var; }

Compile/link with

gcc main.c -L. -lvar -o main
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top