简体   繁体   English

为什么在使用 makefile 时收到未定义的引用错误?

[英]Why while using makefile I receive undefined reference error?

I created the following makefile:我创建了以下 makefile:

assembler: main.o first_pass.o second_pass.o helpers.o data_array.o symbol_table.o
            gcc -Wall -ansi -pedantic main.o first_pass.o second_pass.o helpers.o data_array.o symbol_table.o -o assembler
main.o:         main.c header.h
                gcc -Wall -ansi -pedantic main.c -o main.o
first_pass.o:   first_pass.c header.h
                gcc -Wall -ansi -pedantic first_pass.c -o first_pass.o
second_pass.o:  second_pass.c header.h
                gcc -Wall -ansi -pedantic second_pass.c -o second_pass.o
helpers.o:      helpers.c header.h data.h
                gcc -Wall -ansi -pedantic helpers.c -o helpers.o
data_array.o:   data_array.c header.h data.h
                gcc -Wall -ansi -pedantic data_array.c -o data_array.o
symbol_table.o: symbol_table.c header.h data.h
                gcc -Wall -ansi -pedantic symbol_table.c -o symbol_table.o

In my 'main.c' file I have #include "header.h" .在我的 'main.c' 文件中,我有#include "header.h" Where in 'header.h' I have the declarations of all the functions.在'header.h'中我有所有函数的声明。 But I receive the following error:但我收到以下错误:

gcc -Wall -ansi -pedantic main.c -o main.o
/tmp/cc3dP7hx.o: In function `main':
main.c:(.text+0x257): undefined reference to `first_pass`
main.c:(.text+0x2e4): undefined reference to `second_pass'
main.c:(.text+0x37f): undefined reference to build_obj_file
main.c:(.text+0x3a9): undefined reference to build_ent_file
main.c:(.text+0x427): undefined reference to free_all_data
main.c:(.text+0x436): undefined reference to free_all_symbols
main.c:(.text+0x44d): undefined reference to free_all_words
collect2: error: ld returned 1 exit status
makefile:4: recipe for target 'main.o' failed
make: *** [main.o] Error 1

The problem is that your command doesn't create object files, but attempt to build executable programs:问题是您的命令不会创建 object 文件,而是尝试构建可执行程序:

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

You need the -c option to create object files:您需要-c选项来创建 object 文件:

gcc -Wall -pedantic main.c -c -o main.o

A better idea would be to rely on the make implicit rules so you don't have to explicitly list all commands:一个更好的主意是依赖make 隐式规则,这样您就不必显式列出所有命令:

CC = gcc
LD = gcc
CFLAGS = -Wall -pedantic
LDFLAGS =

assembler: main.o first_pass.o second_pass.o helpers.o data_array.o symbol_table.o

You might want to add rules for the header-file dependencies, or figure out some way to auto-generate them (it's possible).您可能想要为头文件依赖项添加规则,或者想办法自动生成它们(这是可能的)。 Other than that the above Makefile should be enough to build all object files and then link them together into the assembler executable program.除此之外,上面的Makefile应该足以构建所有 object 文件,然后将它们链接到assembler程序可执行程序中。

Note that I have removed the -ansi flag, as it's mostly obsolete these days.请注意,我已经删除了-ansi标志,因为它现在大多已经过时了。

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

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