与cygwin链接
问题描述:
我有一个由程序集函数和C函数调用的小程序。 现在程序编译和完美的作品在UNIX系统上,但在Cygwin中使用Makefile时,我得到以下错误:与cygwin链接
的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退出状态 makefile: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
答
由用户TVIN'解决 - 尝试修改你的原型成为EXTERN INT strToLeet(char *)asm(“strToLeet”); - tivn
'strToLeet'似乎是一个函数。在C中,所需要的只是一个原型,而不是'extern'语句。 – user3629249
asm文件是否包含必要的语句以使strToLeet()函数在应用程序的其余部分中可见? – user3629249
strToLeet是一个用汇编写的函数, – yair