gpt4 book ai didi

c++ - eclipse GDT : undefined reference to library components

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:45:31 28 4
gpt4 key购买 nike

我正在尝试使用 Eclipse (Neon) 在 C++ 中运行一个非常简单的 GUI 应用程序:程序启动,显示红色并在 10 秒后自行关闭。

为了实现这一点,我正在运行 Allegro 5.0.10 游戏引擎,它的源代码在 /usr/local/include/allegro5 中安装了一些库。我的程序如下所示:

#include <stdio.h>
#include <allegro5/allegro.h>
#include <allegro5/allegro5.h>

int main(int argc, char **argv){

ALLEGRO_DISPLAY *display = NULL;
if(!al_init()) {
fprintf(stderr, "failed to initialize allegro!\n");
return -1;
}

display = al_create_display(640, 480);
if(!display) {
fprintf(stderr, "failed to create display!\n");
return -1;
}

al_clear_to_color(al_map_rgb(255,0,0));

al_flip_display();

al_rest(10.0);

al_destroy_display(display);

return 0;
}

使用以下选项从头开始创建一个新项目...

New project options

...并用这些构建它...

C++ options.

Builder extra options.

...选择“全部构建”时,控制台中会出现一条错误消息:

make all 
Building file: ../main.cpp
Invoking: GCC C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -fPIC -MMD -MP -MF"main.d" -MT"main.o" -o "main.o" "../main.cpp"
Finished building: ../main.cpp

Building target: pang
Invoking: GCC C++ Linker
g++ `pkg-config --libs allegro-5 allegro_image-5` -o "pang" ./main.o
./main.o: In function `main':
/home/xvlaze/workspace/pang/Debug/../main.cpp:14: undefined reference to `al_install_system'
/home/xvlaze/workspace/pang/Debug/../main.cpp:19: undefined reference to `al_create_display'
/home/xvlaze/workspace/pang/Debug/../main.cpp:25: undefined reference to `al_map_rgb'
/home/xvlaze/workspace/pang/Debug/../main.cpp:25: undefined reference to `al_clear_to_color'
/home/xvlaze/workspace/pang/Debug/../main.cpp:27: undefined reference to `al_flip_display'
/home/xvlaze/workspace/pang/Debug/../main.cpp:29: undefined reference to `al_rest'
/home/xvlaze/workspace/pang/Debug/../main.cpp:31: undefined reference to `al_destroy_display'
collect2: error: ld returned 1 exit status
make: *** [pang] Error 1

EXTRA: 我已经转载了this回答,但它仍然无法正常工作。

最佳答案

您现在遇到的问题是您添加的特殊标志出现在依赖它们的对象之前。

您应该做的是更改 GCC C 链接器 -> 命令行模式 以在 之后添加 ${FLAGS} ${INPUTS}.

这样做会改变编译行:

g++ `pkg-config --libs allegro-5 allegro_image-5` -o "pang"  ./main.o   

到:

g++ -o "pang"  ./main.o   `pkg-config --libs allegro-5 allegro_image-5` 

参见 https://stackoverflow.com/a/409470/2796832有关链接顺序及其重要性的更多信息。

关于c++ - eclipse GDT : undefined reference to library components,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39563323/

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