在C++中使用 CImg 的未定义参考错误



我正在使用CImg进行图像处理,我想尝试一个非常简单的示例,将图像的某些像素转换为黑色:

main.cpp

#include <iostream>
#include<X11/Xlib.h>
#include "CImg.h"
using namespace cimg_library;
int main()
{
    // read a jpg image
    CImg<float> imgin("frog.jpg");
    // do some computation on the pixels (R,G,B values)
    for(int i=0; i<100; i++)
        for(int j=0; j<10; j++)
            imgin(i,i+j,0) = imgin(i,i+j,1) = imgin(i, i+j, 2) = 0;
    // write image back
    imgin.save("frog2.jpg");
    return(0);
}

我以不同的方式编译了我的程序:

g++ -L/usr/lib -lm -lpthread -lX11 -O3 -o try main.cpp

g++ -L/usr/libX11 -lm -lpthread -lX11 -O3 -o try main.cpp

g++ -L/usr/X11R6/lib -lm -lpthread -lX11 -O3 -o try main.cpp

但我总是得到同样的错误:

    /tmp/ccQmlT63.o: In function `cimg_library::CImgDisplay::paint(bool) [clone .part.18]':
main.cpp:(.text+0x4f2): undefined reference to `XPutImage'
main.cpp:(.text+0x625): undefined reference to `XSendEvent'
/tmp/ccQmlT63.o: In function `cimg_library::CImgDisplay::move(int, int) [clone .part.142]':
main.cpp:(.text+0xe6a): undefined reference to `XMoveWindow'
main.cpp:(.text+0x109c): undefined reference to `XSendEvent'
/tmp/ccQmlT63.o: In function `cimg_library::CImgDisplay::_events_thread(void*)':
main.cpp:(.text._ZN12cimg_library11CImgDisplay14_events_threadEPv[_ZN12cimg_library11CImgDisplay14_events_threadEPv]+0xe8): undefined reference to `XCheckTypedEvent'
main.cpp:(.text._ZN12cimg_library11CImgDisplay14_events_threadEPv[_ZN12cimg_library11CImgDisplay14_events_threadEPv]+0x100): undefined reference to `XCheckMaskEvent'
main.cpp:(.text._ZN12cimg_library11CImgDisplay14_events_threadEPv[_ZN12cimg_library11CImgDisplay14_events_threadEPv]+0x124): undefined reference to `pthread_testcancel'
main.cpp:(.text._ZN12cimg_library11CImgDisplay14_events_threadEPv[_ZN12cimg_library11CImgDisplay14_events_threadEPv]+0x4d2): undefined reference to `XUnmapWindow'
main.cpp:(.text._ZN12cimg_library11CImgDisplay14_events_threadEPv[_ZN12cimg_library11CImgDisplay14_events_threadEPv]+0x574): undefined reference to `XCheckWindowEvent'
main.cpp:(.text._ZN12cimg_library11CImgDisplay14_events_threadEPv[_ZN12cimg_library11CImgDisplay14_events_threadEPv]+0x5ca): undefined reference to `XResizeWindow'
main.cpp:(.text._ZN12cimg_library11CImgDisplay14_events_threadEPv[_ZN12cimg_library11CImgDisplay14_events_threadEPv]+0x65e): undefined reference to `XLookupString'
main.cpp:(.text._ZN12cimg_library11CImgDisplay14_events_threadEPv[_ZN12cimg_library11CImgDisplay14_events_threadEPv]+0x8a4): undefined reference to `XCheckWindowEvent'
main.cpp:(.text._ZN12cimg_library11CImgDisplay14_events_threadEPv[_ZN12cimg_library11CImgDisplay14_events_threadEPv]+0x9bc): undefined reference to `XCheckWindowEvent'
main.cpp:(.text._ZN12cimg_library11CImgDisplay14_events_threadEPv[_ZN12cimg_library11CImgDisplay14_events_threadEPv]+0xa75): undefined reference to `XCheckWindowEvent'
main.cpp:(.text._ZN12cimg_library11CImgDisplay14_events_threadEPv[_ZN12cimg_library11CImgDisplay14_events_threadEPv]+0xb14): undefined reference to `XCheckWindowEvent'
main.cpp:(.text._ZN12cimg_library11CImgDisplay14_events_threadEPv[_ZN12cimg_library11CImgDisplay14_events_threadEPv]+0xb79): undefined reference to `XPutImage'
main.cpp:(.text._ZN12cimg_library11CImgDisplay14_events_threadEPv[_ZN12cimg_library11CImgDisplay14_events_threadEPv]+0xb9b): undefined reference to `XGetWindowAttributes'
main.cpp:(.text._ZN12cimg_library11CImgDisplay14_events_threadEPv[_ZN12cimg_library11CImgDisplay14_events_threadEPv]+0xbac): undefined reference to `XSync'
main.cpp:(.text._ZN12cimg_library11CImgDisplay14_events_threadEPv[_ZN12cimg_library11CImgDisplay14_events_threadEPv]+0xbc9): undefined reference to `XSetInputFocus'
main.cpp:(.text._ZN12cimg_library11CImgDisplay14_events_threadEPv[_ZN12cimg_library11CImgDisplay14_events_threadEPv]+0xc70): undefined reference to `XCheckWindowEvent'
main.cpp:(.text._ZN12cimg_library11CImgDisplay14_events_threadEPv[_ZN12cimg_library11CImgDisplay14_events_threadEPv]+0xecc): undefined reference to `XCheckWindowEvent'
main.cpp:(.text._ZN12cimg_library11CImgDisplay14_events_threadEPv[_ZN12cimg_library11CImgDisplay14_events_threadEPv]+0xf17): undefined reference to `XQueryKeymap'
main.cpp:(.text._ZN12cimg_library11CImgDisplay14_events_threadEPv[_ZN12cimg_library11CImgDisplay14_events_threadEPv]+0xf63): undefined reference to `XLookupString'
/tmp/ccQmlT63.o: In function `cimg_library::CImgDisplay::_set_colormap(unsigned long&, unsigned int)':
main.cpp:(.text._ZN12cimg_library11CImgDisplay13_set_colormapERmj[_ZN12cimg_library11CImgDisplay13_set_colormapERmj]+0x280): undefined reference to `XStoreColors'
/tmp/ccQmlT63.o: In function `cimg_library::CImgDisplay::_map_window()':
main.cpp:(.text._ZN12cimg_library11CImgDisplay11_map_windowEv[_ZN12cimg_library11CImgDisplay11_map_windowEv]+0x5c): undefined reference to `XMapRaised'
main.cpp:(.text._ZN12cimg_library11CImgDisplay11_map_windowEv[_ZN12cimg_library11CImgDisplay11_map_windowEv]+0x73): undefined reference to `XWindowEvent'
main.cpp:(.text._ZN12cimg_library11CImgDisplay11_map_windowEv[_ZN12cimg_library11CImgDisplay11_map_windowEv]+0xae): undefined reference to `XGetWindowAttributes'
main.cpp:(.text._ZN12cimg_library11CImgDisplay11_map_windowEv[_ZN12cimg_library11CImgDisplay11_map_windowEv]+0xbf): undefined reference to `XSync'
/tmp/ccQmlT63.o: In function `cimg_library::CImgDisplay::_paint(bool)':
main.cpp:(.text._ZN12cimg_library11CImgDisplay6_paintEb[_ZN12cimg_library11CImgDisplay6_paintEb]+0x87): undefined reference to `XPutImage'
main.cpp:(.text._ZN12cimg_library11CImgDisplay6_paintEb[_ZN12cimg_library11CImgDisplay6_paintEb]+0xfd): undefined reference to `XSendEvent'
/tmp/ccQmlT63.o: In function `cimg_library::CImgDisplay::assign()':
main.cpp:(.text._ZN12cimg_library11CImgDisplay6assignEv[_ZN12cimg_library11CImgDisplay6assignEv]+0x5ed): undefined reference to `XDestroyWindow'
main.cpp:(.text._ZN12cimg_library11CImgDisplay6assignEv[_ZN12cimg_library11CImgDisplay6assignEv]+0x63f): undefined reference to `XSync'
main.cpp:(.text._ZN12cimg_library11CImgDisplay6assignEv[_ZN12cimg_library11CImgDisplay6assignEv]+0x989): undefined reference to `XUngrabKeyboard'
main.cpp:(.text._ZN12cimg_library11CImgDisplay6assignEv[_ZN12cimg_library11CImgDisplay6assignEv]+0xd0b): undefined reference to `XFreeColormap'
main.cpp:(.text._ZN12cimg_library11CImgDisplay6assignEv[_ZN12cimg_library11CImgDisplay6assignEv]+0xd18): undefined reference to `XDestroyWindow'
/tmp/ccQmlT63.o: In function `cimg_library::CImgDisplay::screen_height()':
main.cpp:(.text._ZN12cimg_library11CImgDisplay13screen_heightEv[_ZN12cimg_library11CImgDisplay13screen_heightEv]+0xfb): undefined reference to `XOpenDisplay'
main.cpp:(.text._ZN12cimg_library11CImgDisplay13screen_heightEv[_ZN12cimg_library11CImgDisplay13screen_heightEv]+0x11d): undefined reference to `XCloseDisplay'
/tmp/ccQmlT63.o: In function `cimg_library::CImgDisplay::screen_width()':
main.cpp:(.text._ZN12cimg_library11CImgDisplay12screen_widthEv[_ZN12cimg_library11CImgDisplay12screen_widthEv]+0xfb): undefined reference to `XOpenDisplay'
main.cpp:(.text._ZN12cimg_library11CImgDisplay12screen_widthEv[_ZN12cimg_library11CImgDisplay12screen_widthEv]+0x11d): undefined reference to `XCloseDisplay'
/tmp/ccQmlT63.o: In function `cimg_library::CImgDisplay::_init_fullscreen()':
main.cpp:(.text._ZN12cimg_library11CImgDisplay16_init_fullscreenEv[_ZN12cimg_library11CImgDisplay16_init_fullscreenEv]+0xcf): undefined reference to `XCreateWindow'
main.cpp:(.text._ZN12cimg_library11CImgDisplay16_init_fullscreenEv[_ZN12cimg_library11CImgDisplay16_init_fullscreenEv]+0x148): undefined reference to `XCreateImage'
main.cpp:(.text._ZN12cimg_library11CImgDisplay16_init_fullscreenEv[_ZN12cimg_library11CImgDisplay16_init_fullscreenEv]+0x16b): undefined reference to `XSelectInput'
main.cpp:(.text._ZN12cimg_library11CImgDisplay16_init_fullscreenEv[_ZN12cimg_library11CImgDisplay16_init_fullscreenEv]+0x17a): undefined reference to `XMapRaised'
main.cpp:(.text._ZN12cimg_library11CImgDisplay16_init_fullscreenEv[_ZN12cimg_library11CImgDisplay16_init_fullscreenEv]+0x193): undefined reference to `XWindowEvent'
main.cpp:(.text._ZN12cimg_library11CImgDisplay16_init_fullscreenEv[_ZN12cimg_library11CImgDisplay16_init_fullscreenEv]+0x1d3): undefined reference to `XPutImage'
main.cpp:(.text._ZN12cimg_library11CImgDisplay16_init_fullscreenEv[_ZN12cimg_library11CImgDisplay16_init_fullscreenEv]+0x1eb): undefined reference to `XGetWindowAttributes'
main.cpp:(.text._ZN12cimg_library11CImgDisplay16_init_fullscreenEv[_ZN12cimg_library11CImgDisplay16_init_fullscreenEv]+0x206): undefined reference to `XSync'
/tmp/ccQmlT63.o: In function `cimg_library::CImgDisplay::_assign(unsigned int, unsigned int, char const*, unsigned int, bool, bool)':
main.cpp:(.text._ZN12cimg_library11CImgDisplay7_assignEjjPKcjbb[_ZN12cimg_library11CImgDisplay7_assignEjjPKcjbb]+0x2e0): undefined reference to `XCreateWindow'
main.cpp:(.text._ZN12cimg_library11CImgDisplay7_assignEjjPKcjbb[_ZN12cimg_library11CImgDisplay7_assignEjjPKcjbb]+0x303): undefined reference to `XSelectInput'
main.cpp:(.text._ZN12cimg_library11CImgDisplay7_assignEjjPKcjbb[_ZN12cimg_library11CImgDisplay7_assignEjjPKcjbb]+0x325): undefined reference to `XStoreName'
main.cpp:(.text._ZN12cimg_library11CImgDisplay7_assignEjjPKcjbb[_ZN12cimg_library11CImgDisplay7_assignEjjPKcjbb]+0x339): undefined reference to `XAllocClassHint'
main.cpp:(.text._ZN12cimg_library11CImgDisplay7_assignEjjPKcjbb[_ZN12cimg_library11CImgDisplay7_assignEjjPKcjbb]+0x366): undefined reference to `XSetClassHint'
main.cpp:(.text._ZN12cimg_library11CImgDisplay7_assignEjjPKcjbb[_ZN12cimg_library11CImgDisplay7_assignEjjPKcjbb]+0x36e): undefined reference to `XFree'
main.cpp:(.text._ZN12cimg_library11CImgDisplay7_assignEjjPKcjbb[_ZN12cimg_library11CImgDisplay7_assignEjjPKcjbb]+0x3ec): undefined reference to `XCreateImage'
main.cpp:(.text._ZN12cimg_library11CImgDisplay7_assignEjjPKcjbb[_ZN12cimg_library11CImgDisplay7_assignEjjPKcjbb]+0x409): undefined reference to `XInternAtom'
main.cpp:(.text._ZN12cimg_library11CImgDisplay7_assignEjjPKcjbb[_ZN12cimg_library11CImgDisplay7_assignEjjPKcjbb]+0x422): undefined reference to `XInternAtom'
main.cpp:(.text._ZN12cimg_library11CImgDisplay7_assignEjjPKcjbb[_ZN12cimg_library11CImgDisplay7_assignEjjPKcjbb]+0x445): undefined reference to `XSetWMProtocols'
main.cpp:(.text._ZN12cimg_library11CImgDisplay7_assignEjjPKcjbb[_ZN12cimg_library11CImgDisplay7_assignEjjPKcjbb]+0x688): undefined reference to `XCreateSimpleWindow'
main.cpp:(.text._ZN12cimg_library11CImgDisplay7_assignEjjPKcjbb[_ZN12cimg_library11CImgDisplay7_assignEjjPKcjbb]+0x6c6): undefined reference to `XGrabKeyboard'
main.cpp:(.text._ZN12cimg_library11CImgDisplay7_assignEjjPKcjbb[_ZN12cimg_library11CImgDisplay7_assignEjjPKcjbb]+0x713): undefined reference to `XOpenDisplay'
main.cpp:(.text._ZN12cimg_library11CImgDisplay7_assignEjjPKcjbb[_ZN12cimg_library11CImgDisplay7_assignEjjPKcjbb]+0x78d): undefined reference to `XVisualIDFromVisual'
main.cpp:(.text._ZN12cimg_library11CImgDisplay7_assignEjjPKcjbb[_ZN12cimg_library11CImgDisplay7_assignEjjPKcjbb]+0x7ac): undefined reference to `XGetVisualInfo'
main.cpp:(.text._ZN12cimg_library11CImgDisplay7_assignEjjPKcjbb[_ZN12cimg_library11CImgDisplay7_assignEjjPKcjbb]+0x7e5): undefined reference to `XFree'
main.cpp:(.text._ZN12cimg_library11CImgDisplay7_assignEjjPKcjbb[_ZN12cimg_library11CImgDisplay7_assignEjjPKcjbb]+0x81e): undefined reference to `pthread_create'
main.cpp:(.text._ZN12cimg_library11CImgDisplay7_assignEjjPKcjbb[_ZN12cimg_library11CImgDisplay7_assignEjjPKcjbb]+0x85b): undefined reference to `XMapRaised'
main.cpp:(.text._ZN12cimg_library11CImgDisplay7_assignEjjPKcjbb[_ZN12cimg_library11CImgDisplay7_assignEjjPKcjbb]+0x877): undefined reference to `XWindowEvent'
main.cpp:(.text._ZN12cimg_library11CImgDisplay7_assignEjjPKcjbb[_ZN12cimg_library11CImgDisplay7_assignEjjPKcjbb]+0x8be): undefined reference to `XGetWindowAttributes'
main.cpp:(.text._ZN12cimg_library11CImgDisplay7_assignEjjPKcjbb[_ZN12cimg_library11CImgDisplay7_assignEjjPKcjbb]+0x8d1): undefined reference to `XSync'
main.cpp:(.text._ZN12cimg_library11CImgDisplay7_assignEjjPKcjbb[_ZN12cimg_library11CImgDisplay7_assignEjjPKcjbb]+0x95e): undefined reference to `XCreateColormap'
main.cpp:(.text._ZN12cimg_library11CImgDisplay7_assignEjjPKcjbb[_ZN12cimg_library11CImgDisplay7_assignEjjPKcjbb]+0xf9f): undefined reference to `XStoreColors'
main.cpp:(.text._ZN12cimg_library11CImgDisplay7_assignEjjPKcjbb[_ZN12cimg_library11CImgDisplay7_assignEjjPKcjbb]+0xfc5): undefined reference to `XSetWindowColormap'
/tmp/ccQmlT63.o: In function `cimg_library::CImgDisplay::resize(int, int, bool)':
main.cpp:(.text._ZN12cimg_library11CImgDisplay6resizeEiib[_ZN12cimg_library11CImgDisplay6resizeEiib]+0x34d): undefined reference to `XResizeWindow'
main.cpp:(.text._ZN12cimg_library11CImgDisplay6resizeEiib[_ZN12cimg_library11CImgDisplay6resizeEiib]+0x361): undefined reference to `XGetWindowAttributes'
main.cpp:(.text._ZN12cimg_library11CImgDisplay6resizeEiib[_ZN12cimg_library11CImgDisplay6resizeEiib]+0x57d): undefined reference to `XCreateImage'
main.cpp:(.text._ZN12cimg_library11CImgDisplay6resizeEiib[_ZN12cimg_library11CImgDisplay6resizeEiib]+0x76b): undefined reference to `XSendEvent'
/tmp/ccQmlT63.o: In function `cimg_library::CImgDisplay& cimg_library::CImgDisplay::assign<unsigned char>(cimg_library::CImg<unsigned char> const&, char const*, unsigned int, bool, bool)':
main.cpp:(.text._ZN12cimg_library11CImgDisplay6assignIhEERS0_RKNS_4CImgIT_EEPKcjbb[_ZN12cimg_library11CImgDisplay6assignIhEERS0_RKNS_4CImgIT_EEPKcjbb]+0x299): undefined reference to `XSendEvent'
collect2: error: ld returned 1 exit status

在谷歌上搜索后,我也尝试了:

sudo apt-get install libx11-dev(但看起来我已经有了(

sudo ln -s /usr/lib/xorg /usr/X11R6(因为/usr下没有 X11R6 文件夹(

但这些都没有解决问题。我错过了什么吗?

您需要将库放在命令的末尾:

g++ -O3 -o try main.cpp -lm -lpthread -lX11

链接器将从左到右实现依赖项。命令行左侧的任何内容都无法满足其右侧某些内容的依赖关系(通常,当然也有例外(。

最新更新