0

I saw this problem can be fixed with adding -fPIC but i tried and faild. I don't use makefile a lot, so maybe I didn't add it correctly. (Its assembly code).

This is my makefile:

PROGRAM = calc
ASFLAGS  = -gstabs
CFLAGS = -g -Wall -pedantic

.PHONY: clean

$(PROGRAM): $(PROGRAM).o
gcc $(CFLAGS) -o $(PROGRAM) $(PROGRAM).o
$(PROGRAM).o: $(PROGRAM).s
as $(ASFLAGS) -o $(PROGRAM).o $(PROGRAM).s

clean:
rm -f *.o 
rm -f $(PROGRAM)
rm -f *~

And this is the problem:

/usr/bin/ld: calc.o: relocation R_X86_64_32S against `.data' can not be 
used when making a shared object; recompile with -fPIC           
/usr/bin/ld: final link failed: Nonrepresentable section on output
collect2: error: ld returned 1 exit sta

Where should I add -fPIC in makefile? I get this error with other codes just different '.something'.

Frenki
  • 1
  • 2
  • What did you try? Your example Makefile doesn't contain the option `-fPIC` anywhere. The recipes are also not indented properly with tabs. Furthermore, is there any specific reason for why you manually assemble your program with `as` before compiling? Also, in your example, you are creating an executable, rather than a shared object. – Sebastian Schrader Jan 14 '18 at 15:43
  • I tried to add it in asflags and cflags, and i even tried to compile it without makfile, adding -fPIC after gcc, but its show same error. As I know 'as' makes calc.o and than it compile it together. I used makefile that we used in school, any other basic makefile for asembly (just .s file no c file) would help. – Frenki Jan 14 '18 at 17:46
  • Since you tagged the question with **C**, I assume you want to compile C source code into a `.so` (*Shared Object*). Your `Makefile` however contains no rules to compile `.c` (*C sources*) files. You only have a rule for `.s` (*Assembly*) files into `.o` (*Object files*) and `.o` into *executables*. Please expand your question and make clear, what you're actually trying to do. Maybe post a minimal excerpt of your code. – Sebastian Schrader Jan 14 '18 at 19:51
  • Thank you. It didnt allow me to tag assembly and it is. I tagged c since it can be linked with c files as well, and has some compile simularity. I will edit question. – Frenki Jan 15 '18 at 20:40
  • 1
    gcc (when configured to build PIEs) sets `-fPIC` (well actually `-fPIE`) already, so that isn't actually the problem. Instead, it's non-PIC code in your hand-written `.S` files. (Put `LDFLAGS+='-no-pie' and `CFLAGS+=`-fno-pie` in your Makefile). BTW, you might want `-Wextra` as well as `-pedantic`. Anyway, that linker error message should probably change now that gcc is configured in default-PIE mode by many distros. – Peter Cordes Jan 17 '18 at 03:28
  • @PeterCordes thank you for a solution, it worked for me – MiFi Dec 09 '19 at 22:30

0 Answers0