Eclipse GDT: undefined reference to library components - c++

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.

Related

g++ parameters order cause link problem on lib freetype

I try to compile JDK source code on Ubuntu 20.04. However, the configure script of JDK failed with some errors:
// conftest.cpp
#include <ft2build.h>
#include FT_FREETYPE_H
int main()
{
FT_Library library;
FT_Error error = FT_Init_FreeType(&library);
if (error) {
printf("Error occurred during init.\n");
}
else {
printf("Initialised OK!\n");
}
return 0;
}
In Terminal, this can link OK:
$ g++ -o conftest -I/usr/include/freetype2 -L/usr/lib/x86_64-linux-gnu conftest.cpp -lfreetype
but this not work:
$ g++ -o conftest -I/usr/include/freetype2 -L/usr/lib/x86_64-linux-gnu -lfreetype conftest.cpp
/usr/bin/ld: /tmp/ccpFC5Ih.o: in function `main':
conftest.cpp:(.text+0x23): undefined reference to `FT_Init_FreeType'
collect2: error: ld returned 1 exit status
Only difference is the order of the parameters and,
from this freetype question1, it seems that it's a upgrade problem. I am pretty sure that already the current latest 20.04,
$ g++ --version
g++ (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0)

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

Read function in magick++ wont compile

#include <iostream>
#include <Magick++.h>
int main()
{
Magick::InitializeMagick(NULL);
Magick::Image im;
im.read("/home/chase/Desktop/m42.jpg");
im.display();
return 0;
}
I get the following error when I try to compile in eclipse...
Building file: ../main.cpp
Invoking: GCC C++ Compiler
g++ -std=c++0x -I/usr/include/x86_64-linux-gnu/ImageMagick-6 -I/usr/include/ImageMagick-6 -O0 -g3 -Wall -c -fmessage-length=0 -fopenmp -DMAGICKCORE_HDRI_ENABLE=0 -DMAGICKCORE_QUANTUM_DEPTH=16 -MMD -MP -MF"main.d" -MT"main.d" -o "main.o" "../main.cpp"
Finished building: ../main.cpp
Building target: Stacking
Invoking: GCC C++ Linker
g++ -L../ -o "Stacking" ./main.o -lMagick++-6.Q16 -lMagickWand-6.Q16 -lMagickCore-6.Q16 -lMagick++-6.Q16 -lMagickWand-6.Q16 -lMagickCore-6.Q16
./main.o: In function `main':
/home/chase/workspace/Stacking/Debug/../main.cpp:8: undefined reference to `Magick::Image::read(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
collect2: error: ld returned 1 exit status
makefile:44: recipe for target 'Stacking' failed
make: *** [Stacking] Error 1
Why does the read function not compile. I used the package config tool in eclipse to set up Magick++. Also as far as I can tell all the other functions work fine. I am using Ubuntu. I installed Magic++ using sudo apt-get install libmagick++-dev.
Update:
I got it to work. I had upgraded to g++-5. When I compiled with g++-4.9 it worked. Wonder why it does not work with g++-5?
Why does the read function not compile
Your read function does compile. What you have is a link problem.
why it does not work with g++-5
Because g++-4.x and g++-5.x use different ABI, and are not link-compatible (and your libMagick* libraries were built with g++-4.x).

Eclipse and Gtkmm - "undefined reference to"

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

Embed python code in C++ (Windows + minGW + Python 2.7.2 + Eclipse)

I'm trying to embed python code in C++ (Windows 7 + minGW + Python 2.7.2 + Eclipse Indigo with CDT and PyDev).
So, this is the simple code:
#include <Python.h> //Python.h
#include <iostream> //iostream
using namespace std;
int main(int argc, char *argv[])
{
Py_Initialize();
PyRun_SimpleString("from time import time,ctime\n"
"print('Today is', ctime(time()))\n");
Py_Finalize();
return 0;
}
And I couldn't understant what am I doing wrong.
I include dirrctories C:\Python27\include and C:\Python27\libs but I can't build my project.
1) When I trying to build my project I got this error:
**** Internal Builder is used for build **** g++
-IC:\Python27\include -IC:\Python27\libs -O0 -g3 -Wall -c
-fmessage-length=0 -o main.o ..\main.cpp g++ -o testpy2.exe main.o
main.o: In function `main':
C:\Users\const\workspace\testpy2\Debug/../main.cpp:7: undefined
reference to `_imp__Py_Initialize'
C:\Users\const\workspace\testpy2\Debug/../main.cpp:9: undefined
reference to `_imp__PyRun_SimpleStringFlags'
C:\Users\const\workspace\testpy2\Debug/../main.cpp:10: undefined
reference to `_imp__Py_Finalize'
collect2: ld returned 1 exit status
Build error occurred, build is stopped Time consumed: 1507 ms.
2) And if I change current toolchain in Eclipse from "minGW" to "CrossGCC" .. I got this error:
**** Build of configuration Release for project testpy ****
make all Building file: ../main.cpp Invoking: Cross G++ Compiler g++
-I"C:\Python27\include" -I"C:\Python27\libs" -O3 -Wall -c
-fmessage-length=0 -MMD -MP -MF"main.d" -MT"main.d" -o "main.o"
"../main.cpp" Finished building: ../main.cpp Building target:
testpy.exe Invoking: Cross G++ Linker g++ -o "testpy.exe" ./main.o
-l"C:/Python27/libs/libpython27.a" -l"C:/Python27/libs/python27.lib"
c:/mingw/bin/../lib/gcc/mingw32/4.5.2/../../../../mingw32/bin/ld.exe:
cannot find -lC:/Python27/libs/libpython27.a
c:/mingw/bin/../lib/gcc/mingw32/4.5.2/../../../../mingw32/bin/ld.exe:
cannot find -lC:/Python27/libs/python27.lib collect2: ld returned 1
exit status make: *** [testpy.exe] Error 1
**** Build Finished ****
Could anybody tell me what's wrong with my code or settings or something else?
Thank you
That is a linker error, not a compiler error. You need to link to the python. As you can see, with the "CrossGCC" toolchain you are almost there:
-lC:/Python27/libs/libpython27.a
You need to change this to
-LC:/Python27/libs -lpython