c - makefile 中对 'func' 错误的 undefined reference

标签 c makefile header undefined

我试图将引用从一个头文件传递到我的主文件,当这些函数写在不同的 .c 文件中时,我得到一个 undefined reference to 'fund'所有这些功能。 在我的主文件中,我只有一个包含,它是 #include "Declerations.h"#include "Declerations.h" 代码是:

#ifndef Declerations_h
#define  Declerations_h
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "Machine_Memory.h"
#include "Symbols.h"

#define MAX_LINE 1024
#define A 0
#define R 2
#define E 1
#define RIGHT_BIT 16384
#define BITS_IN_WORD 14

int is_a_op(char * wrd);
int find_op(char *wrd);
int op_group(int op);
int is_a_sign(char * wrd);
int is_new_line(char * wrd);
#endif

生成文件:

assembler: Assembler.o DecleratioFuncs.o SymbolFuncs.o
    gcc -g -ansi -Wall -pedantic Assembler.o SymbolFuncs.o DecleratioFuncs.o -o assembler
Assembler.o: Assembler.c Declerations.h
    gcc -c -ansi -Wall -pedantic -o Assembler.o Assembler.c
SymbolFuncs.o: SymbolFuncs.c Declerations.h
    gcc -c -ansi -Wall -pedantic -o SymbolFuncs.o SymbolFuncs.c
DecleratioFuncs.o: DecleratioFuncs.c Declerations.h
    gcc -c -ansi -pedantic -Wall -o DecleratioFuncs.o DecleratioFuncs.c

我没有得到 SymbolFuncs.cDecleratioFuncs.c 函数,并且在 Assembler.c 中得到同样的错误。 另外请注意,SymbolFuncs 函数在 Symbols.h 中声明 发生错误的输出窗口部分:

student@ubuntu:~/Desktop/Finished_Project$ make
gcc -ansi -Wall -pedantic -o Assembler.o Assembler.c
Assembler.c: In function ‘main’:
Assembler.c:28:2: warning: ISO C90 forbids mixed declarations and code [-pedantic]
Assembler.c:243:9: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat]
Assembler.c:268:6: warning: format ‘%d’ expects a matching ‘int’ argument [-Wformat]
Assembler.c:317:9: warning: value computed is not used [-Wunused-value]
Assembler.c:342:9: warning: value computed is not used [-Wunused-value]
Assembler.c:376:19: warning: comparison with string literal results in unspecified behavior [-Waddress]
Assembler.c:392:8: warning: value computed is not used [-Wunused-value]
Assembler.c:424:8: warning: value computed is not used [-Wunused-value]
Assembler.c:447:9: warning: value computed is not used [-Wunused-value]
Assembler.c:464:9: warning: value computed is not used [-Wunused-value]
Assembler.c:500:8: warning: value computed is not used [-Wunused-value]
Assembler.c:548:8: warning: value computed is not used [-Wunused-value]
Assembler.c:581:8: warning: value computed is not used [-Wunused-value]
Assembler.c:602:9: warning: value computed is not used [-Wunused-value]
Assembler.c:648:8: warning: value computed is not used [-Wunused-value]
Assembler.c:58:37: warning: unused variable ‘entry_list’ [-Wunused-variable]
Assembler.c:58:16: warning: unused variable ‘extern_list’ [-Wunused-variable]
Assembler.c:51:18: warning: unused variable ‘num_to_add’ [-Wunused-variable]
Assembler.c:46:6: warning: variable ‘L’ set but not used [-Wunused-but-set-variable]
Assembler.c:42:6: warning: unused variable ‘ise’ [-Wunused-variable]
Assembler.c:33:33: warning: unused variable ‘next’ [-Wunused-variable]
Assembler.c:33:17: warning: unused variable ‘memory’ [-Wunused-variable]
Assembler.c: In function ‘add_to_mem’:
Assembler.c:757:2: warning: ISO C90 forbids mixed declarations and code [-pedantic]
Assembler.c:767:2: warning: ISO C90 forbids mixed declarations and code [-pedantic]
Assembler.c:767:6: warning: unused variable ‘val’ [-Wunused-variable]
/tmp/ccdCytpY.o: In function `main':
Assembler.c:(.text+0x171): undefined reference to `is_new_line'
Assembler.c:(.text+0x18c): undefined reference to `is_a_sign'
Assembler.c:(.text+0x278): undefined reference to `add_to_slist'
Assembler.c:(.text+0x785): undefined reference to `find_op'
Assembler.c:(.text+0x853): undefined reference to `op_group'
Assembler.c:(.text+0x897): undefined reference to `is_a_sign'
Assembler.c:(.text+0x981): undefined reference to `is_a_sign'
Assembler.c:(.text+0xa88): undefined reference to `op_group'
Assembler.c:(.text+0xc2a): undefined reference to `is_a_sign'
Assembler.c:(.text+0xcc9): undefined reference to `is_a_sign'
Assembler.c:(.text+0x103d): undefined reference to `is_a_sign'
Assembler.c:(.text+0x109b): undefined reference to `op_group'
Assembler.c:(.text+0x11e2): undefined reference to `is_a_sign'
Assembler.c:(.text+0x14c3): undefined reference to `is_a_sign'
Assembler.c:(.text+0x16dc): undefined reference to `find_address'
collect2: ld returned 1 exit status
make: *** [Assembler.o] Error 1

最佳答案

您在输出中看到:

gcc -ansi -Wall -pedantic -o Assembler.o Assembler.c

此处缺少编译器标志 -c,因此它尝试链接,当然失败了。

但是,根据您的 makefile 中的规则,这不应该发生。可能还有第二个具有不同规则的 Makefile(大写 M,优先于小写 m 的 makefile)。你应该删除它。

关于c - makefile 中对 'func' 错误的 undefined reference ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51908515/

相关文章:

php - openssl 在 php 和 c 上获得不同的 DH 公钥

gcc - MacOS High Sierra上的链接错误: “Undefined symbols for architecture x86_64” std::__1

bash - 如何在 Makefile 目标中使用 bash 正则表达式

php - 在从 SVN 部署期间维护开发环境和实时环境之间的配置差异

c - 为什么 fgets() 的行为很奇怪?

C 将字符串存储到数组中

c - 从 Int 类型分配给类型 'struct PartyInfo' 时不兼容的类型

php - 重定向到同一页面后如何调用jquery函数?

php - 如何在没有 header 错误的情况下在 PHP 中重定向?

flutter : How to add a Header Row to a ListView