编译错误R_X86_64_32S:Makefile中-fPIC应添加至何处?
Fixing relocation R_X86_64_32S Error with Assembly Code in Makefile
Let's break down your problem and fix it step by step:
Why You're Seeing This Error
On x86_64 systems, when building position-independent code (required for shared libraries or modern position-independent executables/PIE), your assembly object files need to be compiled with PIC-aware flags. The error occurs because your current assembly compilation step isn't generating PIC-compatible code, so the linker can't resolve the relocations correctly.
Where to Add -fPIC in Your Makefile
Since you're compiling assembly source code (calc.s) with the as assembler, you need to add -fPIC to the ASFLAGS variable, not CFLAGS (which applies to C compiler commands). Here's the corrected Makefile:
PROGRAM = calc # Added -fPIC to ASFLAGS for position-independent assembly code ASFLAGS = -gstabs -fPIC 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 *~
Key Notes
- For pure assembly projects: Only modifying
ASFLAGSis necessary, as shown above. - For mixed C + assembly projects: You'd also add
-fPICtoCFLAGSto ensure your C code is compiled with PIC support too. - If you still run into issues, some older assembler versions might use
--pic-relocsinstead of-fPIC—try that if-fPICdoesn't work for your environment.
内容的提问来源于stack exchange,提问作者Frenki
相关产品推荐
相关产品推荐

