You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

编译错误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 ASFLAGS is necessary, as shown above.
  • For mixed C + assembly projects: You'd also add -fPIC to CFLAGS to ensure your C code is compiled with PIC support too.
  • If you still run into issues, some older assembler versions might use --pic-relocs instead of -fPIC—try that if -fPIC doesn't work for your environment.

内容的提问来源于stack exchange,提问作者Frenki

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 03:27:51