0

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 ?

Community
  • 1
  • 1
Jay Cee
  • 1,855
  • 5
  • 28
  • 48

1 Answers1

0

/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
chill
  • 16,470
  • 2
  • 40
  • 44
  • Thank to your advice, it seems to work when I tip "gcc -fPIC *.c" but impossible to make it work with the Makefile (I edited the first topic with my entire Makefile). – Jay Cee Feb 13 '14 at 14:14
  • @JayCee, the Makefile looks OK, did you clean or otherwise make sure `block_addr.o` is not left from a previos compilation? – chill Feb 13 '14 at 14:22
  • NEVERMIND I finally succeed to compile, thank to you ! – Jay Cee Feb 13 '14 at 14:23
  • I had to put -shared befor -fPIC to make it functionnal :) – Jay Cee Feb 13 '14 at 14:24