Eclipse GDT:对库组件的未定义引用
Eclipse GDT: undefined reference to library components
我正在尝试 运行 使用 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;
}
正在使用以下选项从头开始创建新项目...
...并用这些构建它...
...选择'Build All'时,控制台出现错误信息:
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 链接器 -> 命令行模式 在 ${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`
有关 link 顺序及其重要性的更多信息,请参阅 。
我正在尝试 运行 使用 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;
}
正在使用以下选项从头开始创建新项目...
...并用这些构建它...
...选择'Build All'时,控制台出现错误信息:
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 链接器 -> 命令行模式 在 ${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`
有关 link 顺序及其重要性的更多信息,请参阅 。