c++ - 在 C++ 中使用 CImg 出现 undefined reference 错误

标签 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

在google上搜索后,我也尝试过:

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

链接器将从左到右完成依赖关系。命令行左边的任何东西都不能满足它右边的东西的依赖(一般来说,当然也有异常(exception))。

关于c++ - 在 C++ 中使用 CImg 出现 undefined reference 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48290887/

相关文章:

c++ - 将 int 类型的结构放入 Big Endian 中的 ostream 以转换为 char *

c++ - C++ 中的异常类不起作用

c++ - 验证用户的输入以确保它是二进制的 (C++)

c++ - 如何使用 CImg 库打开 PNG 而不丢失 alpha channel ?

c++ - 定期从 pthread 异步返回值

c++ - Q_INVOKABLE 方法返回自定义 C++ 类型

c++ - ITK 从缓冲区导入图像数据

c++ - 以用户身份而非 sudo 身份运行时 libusb 1.0 程序中的段错误

c++ - CImg库在编译时返回对 `__imp_SetDIBitsToDevice'的 undefined reference

c++ - 如何在 CImg 中将像素数据作为矩阵/vector 获取?