Answer the question
In order to leave comments, you need to log in
How to solve problem with undefined reference c++?
Hello.
I'm trying to write a program, and I need to open pictures, compress them to 64x64, and get a matrix of pixels (red).
I downloaded the CImg lib, connected it, a bunch of undefined reference crashes during assembly, although in ide I can go to the hedders where they are called.
/usr/bin/ld: CMakeFiles/NNforGame.dir/loadertraindata.cpp.o: in function `cimg_library::cimg::Mutex_static::trylock(unsigned int)':
/home/mia/projects/NNforGame/CImg/CImg.h:3214: undefined reference to `pthread_mutex_trylock'
/usr/bin/ld: CMakeFiles/NNforGame.dir/loadertraindata.cpp.o: in function `cimg_library::CImgDisplay::screen_width()':
/home/mia/projects/NNforGame/CImg/CImg.h:9522: undefined reference to `XOpenDisplay'
/usr/bin/ld: /home/mia/projects/NNforGame/CImg/CImg.h:9526: undefined reference to `XCloseDisplay'
/usr/bin/ld: CMakeFiles/NNforGame.dir/loadertraindata.cpp.o: in function `cimg_library::CImgDisplay::screen_height()':
/home/mia/projects/NNforGame/CImg/CImg.h:9544: undefined reference to `XOpenDisplay'
/usr/bin/ld: /home/mia/projects/NNforGame/CImg/CImg.h:9548: undefined reference to `XCloseDisplay'
/usr/bin/ld: CMakeFiles/NNforGame.dir/loadertraindata.cpp.o: in function `cimg_library::CImgDisplay::_handle_events(_XEvent const*)':
/home/mia/projects/NNforGame/CImg/CImg.h:9576: undefined reference to `XUnmapWindow'
/usr/bin/ld: /home/mia/projects/NNforGame/CImg/CImg.h:9582: undefined reference to `XCheckWindowEvent'
/usr/bin/ld: /home/mia/projects/NNforGame/CImg/CImg.h:9587: undefined reference to `XResizeWindow'
/usr/bin/ld: /home/mia/projects/NNforGame/CImg/CImg.h:9599: undefined reference to `XCheckWindowEvent'
/usr/bin/ld: /home/mia/projects/NNforGame/CImg/CImg.h:9604: undefined reference to `XGetWindowAttributes'
/usr/bin/ld: /home/mia/projects/NNforGame/CImg/CImg.h:9605: undefined reference to `XSync'
/usr/bin/ld: /home/mia/projects/NNforGame/CImg/CImg.h:9607: undefined reference to `XSetInputFocus'
/usr/bin/ld: /home/mia/projects/NNforGame/CImg/CImg.h:9619: undefined reference to `XCheckWindowEvent'
/usr/bin/ld: /home/mia/projects/NNforGame/CImg/CImg.h:9632: undefined reference to `XCheckWindowEvent'
/usr/bin/ld: /home/mia/projects/NNforGame/CImg/CImg.h:9636: undefined reference to `XLookupString'
/usr/bin/ld: /home/mia/projects/NNforGame/CImg/CImg.h:9641: undefined reference to `XQueryKeymap'
/usr/bin/ld: /home/mia/projects/NNforGame/CImg/CImg.h:9646: undefined reference to `XLookupString'
/usr/bin/ld: /home/mia/projects/NNforGame/CImg/CImg.h:9651: undefined reference to `XCheckWindowEvent'
/usr/bin/ld: /home/mia/projects/NNforGame/CImg/CImg.h:9657: undefined reference to `XCheckWindowEvent'
/usr/bin/ld: /home/mia/projects/NNforGame/CImg/CImg.h:9662: undefined reference to `XCheckWindowEvent'
/usr/bin/ld: CMakeFiles/NNforGame.dir/loadertraindata.cpp.o: in function `cimg_library::CImgDisplay::_events_thread(void*)':
/home/mia/projects/NNforGame/CImg/CImg.h:9679: undefined reference to `XCheckTypedEvent'
/usr/bin/ld: /home/mia/projects/NNforGame/CImg/CImg.h:9680: undefined reference to `XCheckMaskEvent'
/usr/bin/ld: /home/mia/projects/NNforGame/CImg/CImg.h:9689: undefined reference to `pthread_testcancel'
/usr/bin/ld: CMakeFiles/NNforGame.dir/loadertraindata.cpp.o: in function `cimg_library::CImgDisplay::_set_colormap(unsigned long&, unsigned int)':
/home/mia/projects/NNforGame/CImg/CImg.h:9726: undefined reference to `XStoreColors'
/usr/bin/ld: CMakeFiles/NNforGame.dir/loadertraindata.cpp.o: in function `cimg_library::CImgDisplay::_map_window()':
/home/mia/projects/NNforGame/CImg/CImg.h:9735: undefined reference to `XMapRaised'
/usr/bin/ld: /home/mia/projects/NNforGame/CImg/CImg.h:9737: undefined reference to `XWindowEvent'
/usr/bin/ld: /home/mia/projects/NNforGame/CImg/CImg.h:9744: undefined reference to `XGetWindowAttributes'
/usr/bin/ld: /home/mia/projects/NNforGame/CImg/CImg.h:9745: undefined reference to `XSync'
/usr/bin/ld: CMakeFiles/NNforGame.dir/loadertraindata.cpp.o: in function `cimg_library::CImgDisplay::_paint(bool)':
/home/mia/projects/NNforGame/CImg/CImg.h:9766: undefined reference to `XSendEvent'
/usr/bin/ld: /home/mia/projects/NNforGame/CImg/CImg.h:9774: undefined reference to `XPutImage'
/usr/bin/ld: CMakeFiles/NNforGame.dir/loadertraindata.cpp.o: in function `cimg_library::CImgDisplay::_init_fullscreen()':
/home/mia/projects/NNforGame/CImg/CImg.h:9882: undefined reference to `XDefaultScreen'
/usr/bin/ld: /home/mia/projects/NNforGame/CImg/CImg.h:9882: undefined reference to `XBlackPixel'
/usr/bin/ld: /home/mia/projects/NNforGame/CImg/CImg.h:9884: undefined reference to `XCreateWindow'
/usr/bin/ld: /home/mia/projects/NNforGame/CImg/CImg.h:9887: undefined reference to `XSelectInput'
/usr/bin/ld: /home/mia/projects/NNforGame/CImg/CImg.h:9888: undefined reference to `XMapRaised'
/usr/bin/ld: /home/mia/projects/NNforGame/CImg/CImg.h:9889: undefined reference to `XWindowEvent'
/usr/bin/ld: /home/mia/projects/NNforGame/CImg/CImg.h:9894: undefined reference to `XGetWindowAttributes'
/usr/bin/ld: /home/mia/projects/NNforGame/CImg/CImg.h:9895: undefined reference to `XSync'
/usr/bin/ld: CMakeFiles/NNforGame.dir/loadertraindata.cpp.o: in function `cimg_library::CImgDisplay::_desinit_fullscreen()':
/home/mia/projects/NNforGame/CImg/CImg.h:9902: undefined reference to `XUngrabKeyboard'
/usr/bin/ld: /home/mia/projects/NNforGame/CImg/CImg.h:9913: undefined reference to `XDestroyWindow'
/usr/bin/ld: CMakeFiles/NNforGame.dir/loadertraindata.cpp.o: in function `cimg_library::CImgDisplay::_assign(unsigned int, unsigned int, char const*, unsigned int, bool, bool)':
/home/mia/projects/NNforGame/CImg/CImg.h:9941: undefined reference to `XOpenDisplay'
/usr/bin/ld: /home/mia/projects/NNforGame/CImg/CImg.h:9956: undefined reference to `XVisualIDFromVisual'
/usr/bin/ld: /home/mia/projects/NNforGame/CImg/CImg.h:9958: undefined reference to `XGetVisualInfo'
/usr/bin/ld: /home/mia/projects/NNforGame/CImg/CImg.h:9961: undefined reference to `XFree'
/usr/bin/ld: /home/mia/projects/NNforGame/CImg/CImg.h:9965: undefined reference to `pthread_create'
/usr/bin/ld: /home/mia/projects/NNforGame/CImg/CImg.h:9984: undefined reference to `XCreateWindow'
/usr/bin/ld: /home/mia/projects/NNforGame/CImg/CImg.h:9987: undefined reference to `XCreateSimpleWindow'
/usr/bin/ld: /home/mia/projects/NNforGame/CImg/CImg.h:9989: undefined reference to `XSelectInput'
/usr/bin/ld: /home/mia/projects/NNforGame/CImg/CImg.h:9993: undefined reference to `XStoreName'
/usr/bin/ld: /home/mia/projects/NNforGame/CImg/CImg.h:9995: undefined reference to `XCreateColormap'
/usr/bin/ld: /home/mia/projects/NNforGame/CImg/CImg.h:9997: undefined reference to `XSetWindowColormap'
/usr/bin/ld: /home/mia/projects/NNforGame/CImg/CImg.h:10001: undefined reference to `XAllocClassHint'
/usr/bin/ld: /home/mia/projects/NNforGame/CImg/CImg.h:10004: undefined reference to `XSetClassHint'
/usr/bin/ld: /home/mia/projects/NNforGame/CImg/CImg.h:10005: undefined reference to `XFree'
/usr/bin/ld: /home/mia/projects/NNforGame/CImg/CImg.h:10046: undefined reference to `XCreateImage'
/usr/bin/ld: /home/mia/projects/NNforGame/CImg/CImg.h:10050: undefined reference to `XInternAtom'
/usr/bin/ld: /home/mia/projects/NNforGame/CImg/CImg.h:10051: undefined reference to `XInternAtom'
/usr/bin/ld: /home/mia/projects/NNforGame/CImg/CImg.h:10052: undefined reference to `XSetWMProtocols'
/usr/bin/ld: /home/mia/projects/NNforGame/CImg/CImg.h:10054: undefined reference to `XGrabKeyboard'
/usr/bin/ld: CMakeFiles/NNforGame.dir/loadertraindata.cpp.o: in function `cimg_library::CImgDisplay::assign()':
/home/mia/projects/NNforGame/CImg/CImg.h:10087: undefined reference to `XFreeColormap'
/usr/bin/ld: /home/mia/projects/NNforGame/CImg/CImg.h:10088: undefined reference to `XDestroyWindow'
/usr/bin/ld: /home/mia/projects/NNforGame/CImg/CImg.h:10089: undefined reference to `XSync'
/usr/bin/ld: CMakeFiles/NNforGame.dir/loadertraindata.cpp.o: in function `cimg_library::CImgDisplay::resize(int, int, bool)':
/home/mia/projects/NNforGame/CImg/CImg.h:10170: undefined reference to `XResizeWindow'
/usr/bin/ld: /home/mia/projects/NNforGame/CImg/CImg.h:10171: undefined reference to `XGetWindowAttributes'
/usr/bin/ld: CMakeFiles/NNforGame.dir/loadertraindata.cpp.o: in function `cimg_library::CImgDisplay::move(int, int)':
/home/mia/projects/NNforGame/CImg/CImg.h:10233: undefined reference to `XMoveWindow'
/usr/bin/ld: CMakeFiles/NNforGame.dir/loadertraindata.cpp.o: in function `void cimg_library::CImgDisplay::_resize<unsigned char>(unsigned char, unsigned int, unsigned int, bool)':
/home/mia/projects/NNforGame/CImg/CImg.h:9834: undefined reference to `XCreateImage'
/usr/bin/ld: CMakeFiles/NNforGame.dir/loadertraindata.cpp.o: in function `void cimg_library::CImgDisplay::_resize<unsigned short>(unsigned short, unsigned int, unsigned int, bool)':
/home/mia/projects/NNforGame/CImg/CImg.h:9834: undefined reference to `XCreateImage'
/usr/bin/ld: CMakeFiles/NNforGame.dir/loadertraindata.cpp.o: in function `void cimg_library::CImgDisplay::_resize<unsigned int>(unsigned int, unsigned int, unsigned int, bool)':
/home/mia/projects/NNforGame/CImg/CImg.h:9834: undefined reference to `XCreateImage'
Answer the question
In order to leave comments, you need to log in
The corresponding libraries are not connected. You apparently included headers in your code and decided that this is "connecting libraries".
However, if the unresolved references are elements of the pthread library, then the correct compiler flag is -pthread
(rather than -lpthread
). You need to not only include the library, but also put the compiler into the appropriate "mode" to compile the code under multithreading conditions.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question