c - 与 cygwin 链接

标签 c gcc makefile cygwin

我有一个小程序,由汇编函数和调用它的 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 退出状态 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

非常感谢您的帮助

最佳答案

由用户“tvin”解决 - 尝试将原型(prototype)修改为 extern int strToLeet (char*) asm ("strToLeet"); – ivn

关于c - 与 cygwin 链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29591719/

相关文章:

c - 计算数组中相同元素对数的有效算法

c - GCC 4.7 字符串文字的源字符编码和执行字符编码?

C++:何时(以及如何)调用 C++ 全局静态构造函数?

Makefile为 "activate"Python虚拟环境

c++ - 制作 : No targets provided near line 28 (CMake)

c - 为什么用 strcmp 函数比较 2 个相等的字符串(在屏幕上打印)会给出错误的答案?

c - popen 实现中的段错误

c++ - linux上g++9.3.0 -O2的一个奇怪bug

linux - Make 不会重建使用 hg pull 更改的文件

c++ - LabWindows CVI 如何检测 C 语言中的缓冲区溢出