【发布时间】:2015-04-12 16:29:29
【问题描述】:
我有一个由汇编函数和调用它的 C 函数组成的小程序。 现在程序可以在 UNIX 系统上完美编译和运行,但是在 cygwin 中使用 makefile 时出现以下错误:
gcc -m32 -g -c -o main.o main.c
gcc -g -m32 -o ass0 main.o myasm.o
main.o:在函数main':
/cygdrive/c/ass0/main.c:15: undefined reference to_strToLeet'
collect2:错误:ld 返回 1 个退出状态
生成文件:3:目标“ass0”的配方失败
make: *** [ass0] 错误 1
main.c 文件代码:
#include <stdio.h>
# define MAX_LEN 100 // Maximal line size
extern int strToLeet (char*);
int main(void) {
char str_buf[MAX_LEN];
int str_len = 0;
printf("Enter a string: ");
fgets(str_buf, MAX_LEN, stdin); // Read user's command line string
str_len = strToLeet (str_buf); // Your assembly code function
printf("\nResult string:%s\nNumber of letters converted to Leet: %d\n",str_buf,str_len);
}
汇编代码的开始:
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 strToLeet ; makes the function appear in global scope
extern printf ; tell linker that printf is defined elsewhere
strToLeet: ; functions are defined as labels
push ebp ; save Base Pointer (bp) original value
mov ebp, esp ; use base pointer to access stack contents
pushad ; push all variables onto stack
mov ecx, dword [ebp+8] ; get function argument
makefile 代码:
all: ass0
ass0: main.o myasm.o
gcc -g -m32 -o ass0 main.o myasm.o
main.o: main.c
gcc -m32 -g -c -o main.o main.c
myasm.o: myasm.s
nasm -g -f elf -l ass0list -o myasm.o myasm.s
帮助将是最appriciated
【问题讨论】:
-
'strToLeet' 似乎是一个函数。在 C 中,所需要的只是一个原型,而不是“外部”语句。
-
asm 文件是否包含使 strToLeet() 函数在应用程序的其余部分可见的必要语句?
-
strToLeet 是一个用汇编语言编写的函数,
-
我想再次提醒您,这个问题主要是关于 Cygwin,因为该程序可以在 UNIX 系统上完美地编译和执行。
-
尝试修改你的原型变成
extern int strToLeet (char*) asm ("strToLeet");