Eclipse and Gtkmm - "undefined reference to" - c++

I use Eclipse and I wanted to use gtkmm in it. I have following code:
#include <gtkmm.h>
#include <iostream>
int main(int argc, char *argv[]) {
Gtk::Main kit(argc, argv);
Gtk::Window mainWindow;
Gtk::Button button("Click here");
mainWindow.set_title("Eclipse/GTKmm Demo");
mainWindow.set_border_width(4);
mainWindow.set_default_size(200, 50);
mainWindow.add(button);
button.show();
Gtk::Main::run(mainWindow);
return 0;
}
I added pkg-config --cflags --libs gtkmm-3.0 (with grave accents, of course) to Cross G++ Compiler Miscellanous options into Other flags and the same to the Cross G++ Compiler Miscellanous options into Linker flags. And it doesn't work!
Here's the compile log:
**** Build of configuration Debug for project User Directory Changer ****
make all
Building file: ../main.cpp
Invoking: Cross G++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 `pkg-config --cflags --libs gtkmm-3.0` -MMD -MP -MF"main.d" -MT"main.d" -o "main.o" "../main.cpp"
Finished building: ../main.cpp
Building target: User Directory Changer
Invoking: Cross G++ Linker
g++ `pkg-config --cflags --libs gtkmm-3.0` -o "User Directory Changer" ./main.o
./main.o: In function `main':
/home/m4tx1/Dropbox/Projects/User Directory Changer/Debug/../main.cpp:12: undefined reference to `Gtk::Main::Main(int&, char**&, bool)'
/home/m4tx1/Dropbox/Projects/User Directory Changer/Debug/../main.cpp:14: undefined reference to `Gtk::Window::Window(Gtk::WindowType)'
/home/m4tx1/Dropbox/Projects/User Directory Changer/Debug/../main.cpp:16: undefined reference to `Glib::ustring::ustring(char const*)'
/home/m4tx1/Dropbox/Projects/User Directory Changer/Debug/../main.cpp:16: undefined reference to `Gtk::Button::Button(Glib::ustring const&, bool)'
/home/m4tx1/Dropbox/Projects/User Directory Changer/Debug/../main.cpp:16: undefined reference to `Glib::ustring::~ustring()'
/home/m4tx1/Dropbox/Projects/User Directory Changer/Debug/../main.cpp:18: undefined reference to `Glib::ustring::ustring(char const*)'
[etc...]
collect2: ld returned 1 exit status
make: *** [User Directory Changer] Error 1
**** Build Finished ****
And I don't know why... When I compile it in terminal by: g++ -O0 -g3 -Wall -c -fmessage-length=0 'pkg-config --cflags --libs gtkmm-3.0' -o ./test ./main.cpp it works...

I found a solution:
In Linker options, in Command line pattern I moved ${FLAGS} to the end, e.g.:
Before: ${COMMAND} ${FLAGS} ${OUTPUT_FLAG} ${OUTPUT_PREFIX}${OUTPUT} ${INPUTS}
After: ${COMMAND} ${OUTPUT_FLAG} ${OUTPUT_PREFIX}${OUTPUT} ${INPUTS} ${FLAGS}
And now it works.

You must divide to pkg-config --cflags <etc> an add there where it's now (compiler options) and then add pkg-config --libs <etc> to linker options

Related

resolving undefined references xcb

I can include items from xcb/xcb.h, but not items that are outlined in /usr/include/xcb/randr.h.
My preference is to use C++, but to help debug I also tried C which produced variations of the same error.
I am certain I am doing something incorrectly, but I am not sure where to start looking to resolve this. Thank you very much for reading, any suggestions?
Example
main.cpp
#include <xcb/xcb.h>
#include <xcb/randr.h>
int main()
{
const xcb_setup_t * xsetup;
xcb_connection_t * conn;
xcb_screen_t * screen;
xcb_window_t root_win;
xcb_screen_iterator_t screen_iterator;
xcb_randr_get_screen_resources_cookie_t resources;
// connect to Xserver
conn = xcb_connect(NULL, NULL);
xsetup = xcb_get_setup(conn);
// get the root window
screen_iterator = xcb_setup_roots_iterator(xsetup);
screen = screen_iterator.data;
root_win = screen->root;
// any function from xcb/randr.h fails with undefined reference.
resources = xcb_randr_get_screen_resources(conn, root_win);
}
Compile
# gcc tries
gcc -Wall main.cpp -o main `pkg-config --cflags --libs xcb`
g++ -Wall main.cpp -o main `pkg-config --cflags --libs xcb`
# clang tries
clang++ main.cpp -o main `pkg-config --cflags --libs xcb`
clang main.cpp -o main `pkg-config --cflags --libs xcb`
Result
gcc
/usr/bin/ld: /tmp/ccWR2GQL.o: in function `main':
main.cpp:(.text+0x6c): undefined reference to `xcb_randr_get_screen_resources'
collect2: error: ld returned 1 exit status
clang
/usr/bin/ld: /tmp/main-d114b5.o: in function `main':
main.cpp:(.text+0x67): undefined reference to `xcb_randr_get_screen_resources'
clang-7: error: linker command failed with exit code 1 (use -v to see invocation)
xcb libraries are split up in several different packages; so it goes that you need to pull in both xcb and xcb-randr libraries, explicitly:
... `pkg-config --cflags --libs xcb xcb-randr`
It's possible that your Linux distribution packages the randr library separately. Checking Fedora, it packages both xcb and xcb-rand in the libxcb-devel subpackage; but it's possible that your Linux distribution has a separate libxcb-randr-devel subpackage that you need to install.
Thank you very much n.m. and G.M. .
I was not linking the xcb-randr .
Solution:
clang++ main.cpp -o main `pkg-config --cflags --libs xcb` -lxcb-randr

Eclipse GDT: undefined reference to library components

I am trying to run a really simple GUI application in C++ with Eclipse (Neon): the program starts, shows a red display and closes itself after 10 seconds.
To achieve this I am running the Allegro 5.0.10 game engine, its source code installs some libs inside /usr/local/include/allegro5. My program looks like this:
#include <stdio.h>
#include <allegro5/allegro.h>
#include <allegro5/allegro5.h>
int main(int argc, char **argv){
ALLEGRO_DISPLAY *display = NULL;
if(!al_init()) {
fprintf(stderr, "failed to initialize allegro!\n");
return -1;
}
display = al_create_display(640, 480);
if(!display) {
fprintf(stderr, "failed to create display!\n");
return -1;
}
al_clear_to_color(al_map_rgb(255,0,0));
al_flip_display();
al_rest(10.0);
al_destroy_display(display);
return 0;
}
Creating a new project from scratch with the following options...
...and building it with these ones...
...when selecting 'Build All', an error message appears in the console:
make all
Building file: ../main.cpp
Invoking: GCC C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -fPIC -MMD -MP -MF"main.d" -MT"main.o" -o "main.o" "../main.cpp"
Finished building: ../main.cpp
Building target: pang
Invoking: GCC C++ Linker
g++ `pkg-config --libs allegro-5 allegro_image-5` -o "pang" ./main.o
./main.o: In function `main':
/home/xvlaze/workspace/pang/Debug/../main.cpp:14: undefined reference to `al_install_system'
/home/xvlaze/workspace/pang/Debug/../main.cpp:19: undefined reference to `al_create_display'
/home/xvlaze/workspace/pang/Debug/../main.cpp:25: undefined reference to `al_map_rgb'
/home/xvlaze/workspace/pang/Debug/../main.cpp:25: undefined reference to `al_clear_to_color'
/home/xvlaze/workspace/pang/Debug/../main.cpp:27: undefined reference to `al_flip_display'
/home/xvlaze/workspace/pang/Debug/../main.cpp:29: undefined reference to `al_rest'
/home/xvlaze/workspace/pang/Debug/../main.cpp:31: undefined reference to `al_destroy_display'
collect2: error: ld returned 1 exit status
make: *** [pang] Error 1
EXTRA: I have already reproduced this answer, but it's still not working.
The problem you are now having is that the special flags you have added are coming before the object that depends on them.
What you should do is change the GCC C Linker -> Command line pattern to have ${FLAGS} after the ${INPUTS}.
Doing that will change the compile line from:
g++ `pkg-config --libs allegro-5 allegro_image-5` -o "pang" ./main.o
to:
g++ -o "pang" ./main.o `pkg-config --libs allegro-5 allegro_image-5`
See https://stackoverflow.com/a/409470/2796832 for some more info on link order and why it matters.

How to resolve 'path is wrong' compile error include GTKmm 3.0

I have changed the command line pattern in the GCC C++ compiler to
${COMMAND} 'pkg-config --cflags --libs gtkmm-3.0' ${FLAGS} ${OUTPUT_FLAG}
${OUTPUT_PREFIX}${OUTPUT} ${INPUTS}
As well as the Command line pattern in the GCC C++ Linker. I then added the paths
/usr/include/gtkmm-3.0
/usr/include/gdkmm-3.0
/usr/include/glibmm-2.4
To includes (http://milindapro.blogspot.nl/2012/10/create-gui-with-gtkmm-setup-eclipse-for.html). Compiling however tells me that some path is either wrong or missing:
make all
Building file: ../main.cpp
Invoking: GCC C++ Compiler
g++ 'pkg-config --cflags --libs gtkmm-3.0' -I/usr/include/gtkmm-3.0 - I/usr/include/gdkmm-3.0 -I/usr/include/glibmm-2.4 -O0 -g3 -Wall -c -fmessage-length=0 -MMD - MP -MF"main.d" -MT"main.d" -o "main.o" "../main.cpp"
g++: error: pkg-config --cflags --libs gtkmm-3.0: No such file or directory
make: *** [main.o] Error 1
apparently, ' is not the same as `. Gosh

Undefined reference to `mysql_init'

I am trying to compile my program on my new server, but it's not working for me at the moment.
Error log is:
rasmus#web01:~/c++$ make test
g++ `mysql_config --cflags --libs` main.cpp logger.cpp cpulogger.cpp -o test
/tmp/ccPaMZUy.o: In function `CPULogger':
/home/rasmus/c++/cpulogger.cpp:7: undefined reference to `mysql_init'
/home/rasmus/c++/cpulogger.cpp:8: undefined reference to `mysql_real_connect'
/home/rasmus/c++/cpulogger.cpp:10: undefined reference to `mysql_get_client_info'
/tmp/ccPaMZUy.o: In function `~CPULogger':
/home/rasmus/c++/cpulogger.cpp:16: undefined reference to `mysql_close'
collect2: ld returned 1 exit status
make: *** [all] Error 1
As you can see I am compiling against MySQL - I have checked that mysql.h is present in include paths.
What am I missing?
cpulogger.cpp has #include "cpulogger.h" at the top, then cpulogger.h has this:
#include <iostream>
#include <fstream>
#include <mysql/mysql.h>
The compiler does not complain about missing mysql/mysql.h, so that part must work?
Output of mysql_config:
rasmus#web01:~/c++$ mysql_config --cflags --libs
-I/usr/include/mysql -DBIG_JOINS=1 -fno-strict-aliasing -g
-L/usr/lib -lmysqlclient -lpthread -lz -lm -lrt -ldl
Makefile:
all:
g++ `mysql_config --cflags --libs` main.cpp logger.cpp cpulogger.cpp -o test
test: all
./test
It's a fresh Ubuntu server installation with a mysql-server install on it.
[solved]:
Putting linker libraries at the end of the compiler commands works.
all:
g++ main.cpp logger.cpp cpulogger.cpp -o test `mysql_config --cflags --libs`
See answer below for explanation.
The order of arguments to the linker is significant. Use mysql-config after listing the files that need it. The linker will see that cpulogger.o needs mysql_init and look in libraries listed after it for the symbol. If the libraries were listed earlier in the arguments they won't be searched again.

Compiling a basic OpenCV + Cuda program on linux

I've worked with opencv on linux in the past, but not with cuda. I've struggled with the following compilation error for months. And after trying many solutions i gave up and worked with windows. However, i really want to work on linux. This is the command i'm using to compile the threshold example given on the opencv_gpu website.
nvcc `pkg-config --libs opencv` -L. -L/usr/local/cuda/lib -lcuda -lcudart `pkg-config --cflags opencv` -I. -I/usr/local/cuda/include threshold.cpp -o threshold
here is the error:
/tmp/tmpxft_0000171b_00000000-1_threshold.o: In function `main':
threshold.cpp:(.text+0x124): undefined reference to `cv::gpu::Stream::Null()'
threshold.cpp:(.text+0x156): undefined reference to `cv::gpu::threshold(cv::gpu::GpuMat const&, cv::gpu::GpuMat&, double, double, int, cv::gpu::Stream&)'
threshold.cpp:(.text+0x16d): undefined reference to `cv::gpu::GpuMat::download(cv::Mat&) const'
/tmp/tmpxft_0000171b_00000000-1_threshold.o: In function `cv::gpu::GpuMat::GpuMat(cv::Mat const&)':
threshold.cpp:(.text._ZN2cv3gpu6GpuMatC1ERKNS_3MatE[cv::gpu::GpuMat::GpuMat(cv::Mat const&)]+0x63): undefined reference to `cv::gpu::GpuMat::upload(cv::Mat const&)'
/tmp/tmpxft_0000171b_00000000-1_threshold.o: In function `cv::gpu::GpuMat::~GpuMat()':
threshold.cpp:(.text._ZN2cv3gpu6GpuMatD1Ev[cv::gpu::GpuMat::~GpuMat()]+0xd): undefined reference to `cv::gpu::GpuMat::release()'
collect2: ld returned 1 exit status
make: *** [all] Error 1
In order to help you I had to download and install CUDA 4.0 (with driver 4.0.21) and then download and compiled OpenCV 2.3 for my Macbook Pro, on Mac OS X 10.6.8.
The sample code from OpenCV_GPU was successfully compiled on my machine through:
g++ threshold.cpp -o threshold `pkg-config --cflags --libs opencv` -lopencv_gpu
You were missing the flag -lopencv_gpu , which is not included by pkg-config.
This looks like a linker problem. I don't know, if nvcc follows the same conventions as gcc, but I would try:
nvcc `pkg-config --cflags opencv` -L. -L/usr/local/cuda/lib -I. -I/usr/local/cuda/include -o threshold threshold.cpp `pkg-config --libs opencv` -lcuda -lcudart
More in general: If you write
gcc t.cpp -lB -lA
it means that libB depends on symbols from libA; t.cpp can depend on symbols from libA and libB.
Instead of using pkg-config in the nvcc line I would suggest just manually pointing the compiler at the opencv library and include files. Perhaps you could just run pkg-config --libs opencv on the command line and copy the necessary libs into your nvcc command. It seems nvcc is only choking on the opencv libs (it can't find them for sure!).