注:这是头脑第一C书中的最后练习。
我有以下问题。我正在尝试使用寓言5.2库来制作一个游戏。我希望使用多个.c文件,以便将一切组织得井井有条。但是,我在使用makefile编译程序时遇到了问题。我试图编译这个简单的程序:
#include <stdio.h>
#include <allegro5/allegro.h>
const int disp_h = 640;
const int disp_w = 480;
int main(int argc, char **argv) {
ALLEGRO_DISPLAY *display;
if(!al_init()) {
fprintf(stderr, "failed to initialize allegro!\n");
return -1;
}
display = al_create_display(disp_h,disp_w);
if(!display) {
fprintf(stderr, "failed to create display!\n");
return -1;
}
al_rest(0.4);
al_destroy_display(display);
printf("bye bye!!!\n");
return 0;
}makefile是:
Blasteroids.o: allegro.h Blasteroids.c
gcc -Wall -c Blasteroids.c
Blasteroids: Blasteroids.o allegro.h
gcc -Wall -I/usr/include/allegro5 -L/usr/lib -lallegro -lallegro_main Blasteroids.o -o Blasteroids现在,当我使用终端时,它编译得很好,但现在我似乎遇到了问题。终端给出的错误(使用命令make Blasteroids)是:
cc Blasteroids.o -o Blasteroids
Undefined symbols for architecture x86_64:
"_al_create_display", referenced from:
__al_mangled_main in Blasteroids.o
"_al_destroy_display", referenced from:
__al_mangled_main in Blasteroids.o
"_al_install_system", referenced from:
__al_mangled_main in Blasteroids.o
"_al_rest", referenced from:
__al_mangled_main in Blasteroids.o
"_main", referenced from:
implicit entry/start for main executable
(maybe you meant: __al_mangled_main)
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [Blasteroids] Error 1我不知道我做错了什么,我对这些事情很陌生。我在makefiles中搜索了示例,但它们给了我一些代码,就像我现在使用的那样。现在,我只需要为上面的程序使用一行代码,但是我的想法是,我想创建自己的.c文件,将它们放入.o文件中,然后将它们链接到一起。因此产生了makefile。
发布于 2017-01-26 18:53:18
make程序查找名为makefile或Makefile的文件,没有扩展名。如果您将makefile命名为其他东西,比如makefile.txt,那么make就找不到它,它只会使用自己的内置规则,这些规则不了解可能需要的额外标志ro库。
因此,要么将makefile重命名为makefile或Makefile,要么在运行make时在命令行中显式指定makefile的名称,例如make -f makefile.txt Blasteroids。
其次,如果您没有在命令行中指定一个目标,那么make将始终构建第一个目标。因此,如果您重新排序您的目标,以便您通常希望构建的目标(在本例中是Blasteroids)是第一个,那么您只需运行没有参数的make,它就可以构建该目标。
与编程语言不同,目标定义的顺序并不重要:例如,您不必在链接行之前首先为所有对象文件定义规则。Make读取整个文件并构造一个先决条件关系的内部图,该图中的节点和边可以按任何顺序添加。
发布于 2017-01-26 12:49:16
类似于下面的makefile内容应该完成此工作
备注:
$(SRC:.c=?)语句对$(SRC)宏中包含的文件名的扩展名进行字符替换。%.o:%.c食谱指出,对于要编译成对象文件的每个源文件,请使用下面的菜谱而现在的makefile
CC := /bin/gcc
RM := /bin/rm
CFLAGS := -Wall -Wextra -pedantic -std=gnu99 -ggdb -c
LFLAGS := -L/usr/lib -lallegro -lallegro_main
SRC := Blasteroids.c
#OBJ := $(SRC:.c=.0)
OBJ := $(SRC:.c=.o)
NAME := $(SRC:.c=)
.PSEUDO: all clean
all: $(NAME)
%.o:%.c
#$(CC) $(CFLAGS) $^ -o $@ -I/usr/include/allegro5
$(CC) $(CFLAGS) $< -o $@ -I/usr/include/allegro5
$(NAME): $(OBJ)
$(CC) -ggdb $^ -o $@ $(LFLAGS)
.clean:
$(RM) -f $(NAME) $(OBJ)https://stackoverflow.com/questions/41858268
复制相似问题