解决汇编调用c库出现 ld: cannot find -lc

用汇编调用C库的时候,链接总是出错。先把汇编程序贴上来吧。


这是《Wrox Professional Assembly Language》中的程序:

  1 #cpuid2.s Simple program to extract the processor Vendor ID             
  2 .section .data
  3 output:
  4     .asciz  "The processor Vendor ID is '%s'\n"
  5 .section .bss
  6     .lcomm  buffer, 12
  7 .section .text
  8 .globl _start
  9 _start:
 10     movl    $0, %eax
 11     cpuid
 12     movl    $buffer, %edi
 13     movl    %ebx, (%edi)
 14     movl    %edx, 4(%edi)
 15     movl    %ecx, 8(%edi)
 16 
 17     pushl   $buffer
 18     pushl   $output
 19     call    printf
 20 
 21     addl    $8, %esp
 22     pushl   $0
 23     call    exit
 24 

他用

$ as -o cpuid2.o cpuid2.s
$ ld -dynamic-linker /lib/ld-linux.so.2 -o cpuid2 -lc cpuid2.o

 就可以产生可执行文件 

由于我是64位机,所以加了两个参数

$ as --32 -o cpuid2.o cpuid2.s
$ ld -elf_i386 -dynamic-linker /lib32/ld-linux.so.2 -o cpuid2 -lc cpuid2.o

 但一直出现错误 

ld: cannot find -lc

我修改了一下参数

$ as --32 -o cpuid2.o cpuid2.s
$ ld -m elf_i386 -dynamic-linker /lib32/ld-linux.so.2 /lib32/libc.so.6 -o cpuid2 cpuid2.o

 

终于成功了

$ ./cpuid2
The processor Vendor ID is 'GenuineIntel'


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值