Задать вопрос
@Mamol27

Как решить проблему с undefined reference c++?

Здравствуйте.
Пытаюсь написать прогу одну, и мне нужно открывать картинки, сжимать их до 64х64, и получить матрицу пикселей(красного цвета).
Скачал либу CImg, подключил, при сборке вылетает куча undefined reference, хотя в ide могу перейти в хеддеры там, где они вызываются.
spoiler

/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'


Тоесть в IDE я могу прыгнуть в функцию "pthread_mutex_trylock" и "XOpenDisplay", но при сборке проекта они не находятся
Не знаю как это исправить, хеддеры соответствующие вроде же есть.
  • Вопрос задан
  • 1637 просмотров
Подписаться 1 Простой 3 комментария
Решения вопроса 1
Нужно добавить флаг компилятора -lpthread
Ответ написан
Пригласить эксперта
Ответы на вопрос 1
@TheCalligrapher
Не подключены соответствующие библиотеки. Вы, по-видимому, навключали в свой код хедеров и решили, что это и есть "подключение библиотек".

При этом если неразрешенными ссылками являются элементы библиотеки pthread, то правильным флагом компилятора является -pthread (а не -lpthread). Вам нужно не просто подключить библиотеку, а ещё и перевести компилятор в соответствующий "режим" для компиляции кода в условиях многопоточности.
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы