简体   繁体   English

C makefile 问题,未定义参考 function

[英]C makefile issue, undefined reference to function

Im trying to do a simple excercise in compilation.我试图在编译中做一个简单的练习。 I have 1 c file 1 assembly file and a makefile.我有 1 个 c 文件 1 个程序集文件和一个 makefile。 when I run the 'make' command I get the following error:当我运行“make”命令时,出现以下错误:

gcc -g -m32 -Wall -o mainAssignment0.o mainAssignment0.c
/tmp/ccXfVxtg.o: In function `main':
/home/caspl202/Desktop/tasks/Assignment0/mainAssignment0.c:12: undefined reference to `do_Str'
collect2: error: ld returned 1 exit status
makefile:10: recipe for target 'mainAssignment0.o' failed
make: * [mainAssignment0.o] Error 1

Meaning that for some reason the c program doesnt recognize the external ASM function.这意味着由于某种原因 c 程序无法识别外部 ASM function。 Whats even weirder is that when I run the same makefile on the same files on a different machine it works like a charm.更奇怪的是,当我在不同机器上的相同文件上运行相同的 makefile 时,它就像一个魅力。 I would really like someone to shed some light on this thing.我真的很希望有人能对这件事有所了解。 C code: C 代码:

#include <stdio.h>
#define MAX_LEN 100         
extern int do_Str(char*);
int main(int argc, char** argv) {
  char str_buf[MAX_LEN];   
  int counter = 0;
  fgets(str_buf, MAX_LEN, stdin);   
  counter = do_Str (str_buf);       
  printf("%s%d\n",str_buf,counter); 
  return 0;
}

ASM code: ASM 代码:

section .data                       
        an: dd 0                    
section .text                       
        global do_Str               
do_Str:                             
        push ebp                    
        mov ebp, esp                 
        pushad                      
        mov ecx, dword [ebp+8]              
    loop:           
        cmp byte [ecx], 32
        jnz noS
        inc dword [an] 
    noS:
        cmp byte [ecx], 65
        jl noC
        cmp byte [ecx], 90
        jg noC
        add byte [ecx], 32
    noC:
        inc ecx                 
        cmp byte [ecx], 0       
        jnz loop
        popad                       
        mov eax,[an]                
        mov esp, ebp            
        pop ebp              
        ret             

Makefile: Makefile:

all: exec
libs: asm-lib
asm-lib: asmAssignment0.s
    nasm -g -f elf -o asmAssignment0.o asmAssignment0.s
exec: mainAssignment0.c libs
    gcc -g -m32 -c -o mainAssignment0.o mainAssignment0.c
    gcc -g -m32 -o Assignment0.out mainAssignment0.o asmAssignment0.o 
.PHONY: clean
clean:
    rm -rf ./*.o Assignment0.out

You don't need to declare the function external.您无需在外部声明 function。

int do_Str(char*);

Also, a function in C is prefixed with an underscore, so you must name it accordingly in your asm file.此外,C 中的 function 带有下划线前缀,因此您必须在 asm 文件中相应地命名它。

    global _do_Str               
_do_Str:                             

The underscore is automatically added by the C compiler, so you don't have to use it in the C module.下划线由 C 编译器自动添加,因此您不必在 C 模块中使用它。

The reason for your error you quote here is that your compile line is wrong.您在此处引用的错误的原因是您的编译行错误。 You can tell because you're trying to create an object file, but getting errors from the linker, so something is clearly not right:您可以判断,因为您正在尝试创建 object 文件,但从 linker 中得到错误,所以显然有些不对:

gcc -g -m32 -Wall -o mainAssignment0.o mainAssignment0.c
  ...
collect2: error: ld returned 1 exit status

The problem is you forgot to add the -c flag to this compile line, so that the compiler generates an object file.问题是您忘记在此编译行中添加-c标志,以便编译器生成 object 文件。

However, in your makefile the -c is present, so clearly this error you quote is not generated from the makefile you show us.但是,在您的 makefile 中存在-c ,因此很明显,您引用的此错误不是从您向我们展示的 makefile 生成的。

exec: mainAssignment0.c libs
        gcc -g -m32 -c -o mainAssignment0.o mainAssignment0.c

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM