使用 Makefile 编译使用图像的 FLTK 源代码时出错
Posted
技术标签:
【中文标题】使用 Makefile 编译使用图像的 FLTK 源代码时出错【英文标题】:Errors Using Makefile to compile FLTK source code that uses images 【发布时间】:2016-10-25 18:35:42 【问题描述】:这是我的 Makefile:
# object files
OBJS = main.o
# compiler
# since fltk-config --cxx displays the c++ compiler that was used to compile FLTK (g++)
# we just use it instead of ... for safety purpose
#CC = g++-5 -std=c++11
CXX = $(shell fltk-config --cxx)
# debugging flag
DEBUG = -g
# flags used in compiling and creating object files
# - fltk-config --cxxflags displays C++ complier options to use when compiling FLTK
# source files
# + can also be used when compiling non FLTK files
# - Wall: tells compiler to print all warnings
# - c: is needed to create object file, i.e. .o files
CXXFLAGS = $(shell fltk-config --cxxflags) -Wall -c $(DEBUG)
# flags used in linking
# - fltk-config --ldflags displays the linker options to use when linking a FLTK app
# - ??? fltk-config --ldstaticflags ... when lking a FLTK app to the static FLTK libraries
#LFLAGS = $(shell fltk-config --ldflags) -Wall $(DEBUG)
# may need below options when using images
LINKFLTK_IMG = $(shell fltk-config --use-images --ldstaticflags) -Wall $(DEBUG)
#LINKFLTK_IMG = $(shell fltk-config --use-images --ldflags) -Wall $(DEBUG)
# FLTK libraries
FLTKLIB = -lfltk
executable: $(OBJS)
$(CXX) $(LINKFLTK_IMG) $(OBJS) $(FLTKLIB)
main.o: main.cpp
$(CXX) $(CXXFLAGS) main.cpp
clean:
\rm *.o a.out
这是我要编译的代码:
#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_Box.H>
#include <FL/Fl_Shared_Image.H>
#include <FL/Fl_JPEG_Image.H>
int main()
// const int x = 505;
const int x = 1000;
//const int y = 340;
const int y = 600;
const int border = 10;
// init the image library
fl_register_images();
// create a window and enclosed box
Fl_Window *win = new Fl_Window(x+3*border, y+3*border);
Fl_Box *box = new Fl_Box(border, border, x+border, y+border);
// load jpeg image into memory and attach to the box
Fl_JPEG_Image *jpg = new Fl_JPEG_Image("moon.jpg");
box->image(*jpg);
// done defining the new window
win->end();
// show the window and then delegate control to FLTK
win->show();
return(Fl::run());
这是错误:
/usr/bin/ld: cannot find -lpng
/usr/bin/ld: cannot find -lz
/usr/bin/ld: cannot find -ljpeg
/usr/bin/ld: cannot find -lXft
/usr/bin/ld: cannot find -lfontconfig
/usr/bin/ld: cannot find -lfontconfig
/usr/bin/ld: cannot find -lXinerama
collect2: error: ld returned 1 exit status
make: *** [executable] Error 1
运行make命令后,main.o文件成功生成。所以我猜链接过程有问题。 请发送帮助。
【问题讨论】:
据我记得,png、jpeg 和相关的库(比如 libz)不包含在 fltk 库中,所以你必须手动链接它们 【参考方案1】:取决于您使用的 Linux 版本
SUSE - sudo zypper 安装
libjpeg62-devel 或 libjpeg-devel libpng12-devel 或 libpng-devel zlib-devel xorg-x11-develUbuntu 变体 - sudo apt-get install
libjpeg62-dev libpng12-dev libx11-dev libxcursor-dev libxext-dev libxft-dev libxinerama 开发 libxi 开发 zlib1g-dev对于软件包的名称,系统会有所不同。我不知道他们对 redhat、debian 或 slackware 的称呼。
【讨论】:
以上是关于使用 Makefile 编译使用图像的 FLTK 源代码时出错的主要内容,如果未能解决你的问题,请参考以下文章