首页 > 解决方案 > 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?

标签: clinuxassemblystatic-librariesunix-ar

解决方案


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.


推荐阅读