从C调用汇编函数的问题

从C调用汇编函数的问题

本文介绍了从C调用汇编函数的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(在64位Windows 7上运行MingW,在Kubuntu上运行GCC)

(Running MingW on 64-bit Windows 7 and the GCC on Kubuntu)

这可能只是MingW问题,但至少在安装一次Kubuntu时也失败了,所以我对此表示怀疑.

This may possibly be just a MingW problem, but it's failed on at least one Kubuntu installation as well, so I'm doubtful.

我有一个简短的C程序,应该调用汇编程序.我使用nasm编译汇编程序,并使用MingW的gcc实现编译c程序.两者通过一个makefile链接在一起-bog-simple.但是,在声称外部函数是未定义引用"的声明中,链接失败了.

I have a short, simple C program, which is supposed to call an assembly function. I compile the assembler using nasm and the c program using MingW's implementation of the gcc. The two are linked together with a makefile - bog-simple. And yet, linkage fails on the claim the claim that the external function is an 'undefined reference'

makefile的相关部分:

Relevant part of the makefile:

assign0: ass0.o main.o
gcc -v -m32 -g -Wall -o assign0 ass0.o main.o


    main.o: main.c
         gcc -g -c -Wall -m32 -o main.o main.c

    ass0.o: ass0.s
     nasm -g -f elf -w+all -o ass0.o ass0.s

程序集文件的开头:

section .data                       ; data section, read-write
    an:    DD 0                 ; this is a temporary var

section .text                       ; our code is always in the .text section
    global do_str               ; makes the function appear in global scope
    extern printf

do_str:                             ; functions are defined as labels
[Just Code]

以及c文件的声明:

extern int do_str(char* a);

这至少在一个Kubuntu安装上起作用,在另一个安装上失败,在MingW上失败.有人有主意吗?

This has worked on at least one Kubuntu installation, failed on another, and failed on MingW. Does anyone have an idea?

推荐答案

C编译器可能会以不同的方式调用实际的函数",例如_do_str而不是do_str.名称处理不经常发生可能取决于系统(当然也取决于编译器).尝试调用asm函数_do_str.使用适当的属性(在gcc中)也可以解决此问题.另外,阅读.

C compilers may call the actual "function" differently, e.g. _do_str instead of do_str. Name mangling not happening always could depends on the system (and of course on the compiler). Try calling the asm function _do_str. Using proper attributes (in gcc) could also fix the problem. Also read this.

这篇关于从C调用汇编函数的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 16:25