无法编译 x86_64 .s 和 .c 文件:未定义的函数引用

Cannot compile x86_64 .s and .c files : undefined reference to function

这是我的问题,我尝试创建一个汇编函数库并将其与 C 程序一起使用。 我运行进入未定义的引用错误。

我在学校为这个项目提供的虚拟机上使用 xubuntu(所以我想一切都设置好了...)

程序集文件(ft_function.s)

section .text
    global _ft_function

_ft_function:
    mov rax, 42
    ret

C 文件(main.c)

extern int ft_function(char const *str);

int main()
{
    return (ft_function("abc"));
}

目前问题仍然存在,我没有创建库,以减少最简单的情况,所以我生成了目标文件,然后 link 使用 clang。

nasm -f elf64 ft_function.s -o ft_function.o
clang -c main.c

clang ft_function.o main.o

main.o: In function `main':
main.c:(.text+0x1a): undefined reference to `ft_function'
clang: error: linker command failed with exit code 1 (use -v to see invocation)

感谢您的帮助! 干杯!

section .text
    global _ft_function

_ft_function:
    mov rax, 42
    ret

省略前导下划线。其他一些系统使用以下划线开头的符号名称,但 Linux 不使用。只要做:

section .text
    global ft_function

ft_function:
    mov rax, 42
    ret