【发布时间】:2017-01-25 17:49:47
【问题描述】:
注意:这是 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;
}
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
我不知道我做错了什么,而且我对这些事情很陌生。我已经在 makefile 中搜索了示例,但它们给了我现在正在使用的代码。 我现在可以只为上述程序使用一行,但我的想法是我想制作自己的 .c 文件,将它们制作成 .o 文件,然后将它们链接在一起。因此生成文件。
【问题讨论】:
-
我真的怀疑 exact 相同的命令是否可以从终端运行。您应该将库标志
-lallegro -lallegro_main移动到链接行的末尾,在.o文件之后。 -
确实有效。每当我运行程序时,我都会看到一个打开的屏幕
-
我的意思是,链接命令不是输出程序。无论如何,您的库必须位于链接行中的目标文件之后。
-
好吧,另外没有使用makefile。您可以看出这一点,因为链接命令不是您在 makefile 中编写的命令。你给makefile起什么名字?它必须是
Makefile或makefile,否则如果选择非标准名称,则必须使用make -f mymakefile Blasteroids。 -
像这样:gcc -Wall -I/usr/include/allegro5 -L/usr/lib Blasteroids.o -lallegro -lallegro_main -o Blasteroids?还是在 -o 之后?无论如何,我的文件称为 makefile(但是我使用的是 .txt 扩展名)。这可能是问题吗?对不起,如果这看起来很简单,但我真的很想理解这一点。