c - Archive has no index; run ranlib to add one (when linking with a .a containing a MachO64 object file on Linux)
问题描述
I tried to create a library and test it, but an error occurred.
error code:
./libasm.a: error adding symbols: Archive has no index; run ranlib to add one
collect2: error: ld returned 1 exit status
I compiled it like this.
nasm -f macho64 ft_strlen.s -o ft_strlen.o
ar rcs libasm.a ft_strlen.o
ranlib libasm.a
gcc main.c libasm.a
Below is the source file
;ft_strlen.s
segment .text
global ft_strlen
ft_strlen:
mov rax, 0
jmp count
count:
cmp BYTE [rdi + rax], 0
je exit
inc rax
jmp count
exit:
ret
/*main.c*/
#include <stdio.h>
int ft_strlen(char *str);
int main(void)
{
char *str = "hello world";
printf("%d \n", ft_strlen(str));
}
I am using ubuntu installed on wsl.
What am I doing wrong?
解决方案
Generate object files for Linux-based operating system (or perhaps more correctly, and ELF64 system) with nasm -f elf64 ft_strlen.s -o ft_strlen.o
For more info nasm -hf
to see all valid output formats for nasm -f
Small tip: ranlib
command is not needed because ar s
is already indexing the library.
推荐阅读
- mongodb - MongoDB-处理庞大的集合
- python - 列表未正确附加更新的字典
- macos - 使用 awk 或 sed 时如何忽略大小写
- sql - Oracle:在包和过程中设置会话参数
- ruby - 测试后 Selenium-Webdriver/RSpec 关闭窗口
- python - sqlite:每次调用函数时调用数据库
- ruby-on-rails - 无法使用 Webpacker 转换特定的第三方 node_modules 脚本
- javascript - 如何更改 QuillJS (react-js) 中的默认标题名称?
- django - 未找到带有参数 '('',)' 的 *view* 的反向操作
- wordpress - 从子文件夹重定向 404