![](/img/trans.png)
[英]How to link two object files by placing their respective sections at different locations?
[英]How to link two object files
我有rfact.s和rfactmain.c
rfact.s:
.text
.p2align 4
.global rfact
.type rfact, @function
rfact:
pushl %ebp
movl %esp, %ebp
pushl %ebx
subl $4, %esp
movl 8(%ebp), %ebx
movl $1, %eax
cmpl $1, %ebx
jle .L53
leal -1(%ebx), %eax
movl %eax, (%esp)
call rfact
imull %ebx, %eax
.L53:
addl $4, %esp
popl %ebx
popl %ebp
ret
rfactmain.c:
int main(){
int x = rfact(4);
return 0;
}
我使用的是64位系統。 所以首先我通過做得到兩個目標文件
gcc -m32 -c rfact.s
gcc -m32 -c rfactmain.c
然后我嘗試像這樣將它們鏈接在一起
gcc -m32 -o rf rfact.o rfactmain.o
但我得到一個錯誤
gcc -m32 -o rf rfact.o rfactmain.o
/usr/bin/ld: cannot find crt1.o: No such file or directory
/usr/bin/ld: cannot find crti.o: No such file or directory
/usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-redhat-linux/4.8.2/libgcc_s.so when searching for -lgcc_s
/usr/bin/ld: cannot find -lgcc_s
/usr/bin/ld: skipping incompatible /usr/lib64/libc.so when searching for -lc
/usr/bin/ld: skipping incompatible /usr/lib64/libc.a when searching for -lc
/usr/bin/ld: cannot find -lc
/usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-redhat-linux/4.8.2/libgcc_s.so when searching for -lgcc_s
/usr/bin/ld: cannot find -lgcc_s
/usr/bin/ld: cannot find crtn.o: No such file or directory
collect2: error: ld returned 1 exit status
如果我刪除-m32,則會收到錯誤消息(這是預期的,因為兩個目標文件均為32位)
gcc -o rf rfact.o rfactmain.o
/usr/bin/ld: i386 architecture of input file `rfact.o' is incompatible with i386:x86-64 output
/usr/bin/ld: i386 architecture of input file `rfactmain.o' is incompatible with i386:x86-64 output
collect2: error: ld returned 1 exit status
您沒有安裝32位支持內容。 您可能需要單獨使用gcc-multilib軟件包或32位庫軟件包。 請咨詢發行版的文檔和/或程序包管理器。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.