gpt4 book ai didi

c - Allegro5 makefile 错误

转载 作者:行者123 更新时间:2023-11-30 16:51:14 28 4
gpt4 key购买 nike

注意:这是《Head First C》书中的最后一个练习。

我遇到以下问题。我正在尝试使用 allegro5.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;
}

生成文件是:

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

我不知道我做错了什么,而且我对这些事情很陌生。我在 makefile 中搜索了示例,但它们给了我像我现在使用的代码。我现在可以只对上述程序使用一行,但我的想法是我想制作自己的 .c 文件,将它们制作成 .o 文件,然后将它们链接在一起。因此就有了 makefile。

最佳答案

make 程序查找名为 makefileMakefile 且不带扩展名的文件。如果您将 makefile 命名为其他名称,例如 makefile.txt,则 make 无法找到它,它只会使用自己的内置规则,而这些规则不会了解有关可能需要的额外标志 ro 库的任何信息。

因此,要么将 makefile 重命名为 makefileMakefile,要么在运行 make 时在命令行上显式指定 makefile 的名称,例如 make -f makefile.txt Blasteroids.

其次,如果您没有在命令行上指定目标,那么 make 将始终构建第一个目标。因此,如果您重新排序目标,使您通常想要构建的目标(在本例中为 Blasteroids)位于第一个,那么您可以不带任何参数运行 make它将构建该目标。

与编程语言不同,目标定义的顺序并不重要:例如,您不必在链接行之前首先为所有目标文件定义规则。 Make 读取整个文件并构造一个先决关系的内部图,并且该图中的节点和边可以按任意顺序添加。

关于c - Allegro5 makefile 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41858268/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com