FreeGLUT linking Issues in Linux - c++

I am running Linux Mint 14.1 64-bit
I have installed the following libs:
mesa-common-dev,
freeglut3-dev,
libglew-dev
through the apt-get tool.
Here are my includes, located in my Main.h file:
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <stdio.h>
#include <GL/glew.h>
#include <GL/glut.h>
#include <time.h>
I checked that the libs installed correctly, they seem to be located in /usr/lib/x86_64-linux-gnu
and the headers in /usr/include/GL
I proceed to compile my Main.C file with the following flags:
g++ -Wall -Wextra -Weffc++ -Winit-self -Wmissing-include-dirs -Wswitch-default -switch-enum -Wunused-parameter -Wstrict-overflow=5 -Wfloat-equal -Wshadow -Wc++0x-compat -Wconversion -Wsign-conversion -Wmissing-declarations -Wstrict-null-sentinel -Woverloaded-virtual -Wsign-promo -Werror -pedantic -Wcast-qual -fno-pretty-templates -fmessage-length=80 -fdiagnostics-show-option -g -std=c++0x -pipe -frepo -c Main.C -o Main.o
Main.o is generated without any issues, then I try to create the binary:
g++ -I/usr/include -L/usr/lib/x86_64-linux-gnu -lGL -lglut -lGLU -lGLEW -lX11 -lm -lrt -lpng Main.o -o main
And receive the following errors:
Main.o: In function `init()':
/path/to/Main.C:12: undefined reference to `glClearColor'
Main.o: In function `initGLUT(int, char**)':
/path/to/Main.C: undefined reference to `glutInit'
/path/to/Main.C:21: undefined reference to `glutInitDisplayMode'
/path/to/Main.C:24: undefined reference to `glutInitWindowSize'
/path/to/Main.C:25: undefined reference to `glutCreateWindow'
/path/to/Main.C:28: undefined reference to `glutDisplayFunc'
/path/to/Main.C:31: undefined reference to `glutKeyboardFunc'
/path/to/Main.C:34: undefined reference to `glutMouseFunc'
/path/to/Main.C:37: undefined reference to `glutReshapeFunc'
/path/to/Main.C:40: undefined reference to `glutIdleFunc'
Main.o: In function `printFPS()':
/path/to/Main.C:96: undefined reference to `glutGet'
Main.o: In function `reshape(int, int)':
/path/to/Main.C:123: undefined reference to `glutPostRedisplay'
Main.o: In function `getTime()':
/path/to/Main.C:129: undefined reference to `glutGet'
Main.o: In function `idle()':
/path/to/Main.C:141: undefined reference to `glutPostRedisplay'
Main.o: In function `display()':
/path/to/Main.C:148: undefined reference to `glClearColor'
/path/to/Main.C:149: undefined reference to `glClear'
/path/to/Main.C:150: undefined reference to `glFlush'
/path/to/Main.C:151: undefined reference to `glutSwapBuffers'
Main.o: In function `main':
/path/to/Main.C:164: undefined reference to `glutMainLoop'
The program compiles and links on another Linux system though.
what could I be missing?

You have to pass the libraries last (after the object file)
g++ -I/usr/include -L/usr/lib/x86_64-linux-gnu Main.o \
-lGL -lglut -lGLU -lGLEW -lX11 -lm -lrt -lpng -o main
The reason behind this is, that the linker only links symbols, that are currently undefined. If you pass the libraries before the object files, then there aren't any undefined symbols to be linked and compilation/linking will therefore fail.

The libraries need to come after your object files:
g++ -I/usr/include -L/usr/lib/x86_64-linux-gnu Main.o -lGL -lglut -lGLU -lGLEW -lX11 -lm -lrt -lpng -o main

Related

HDF5 linking error

When linking my hdf5 code I get the following error. Any ideas what I have done wrong.
The command I am using:
g++ -std=c++11 -m64 -DOPENFOAM_PLUS=1712 -Dlinux64 -DWM_ARCH_OPTION=64 -DWM_DP -DWM_LABEL_SIZE=32 -Wall -Wextra -Wold-style-cast -Wnon-virtual-dtor -Wno-unused-parameter -Wno-invalid-offsetof -Wno-attributes -O3 -DNoRepository -ftemplate-depth-100 -I/opt/software/OpenFOAM/OpenFOAM-v1712/src/finiteVolume/lnInclude -I/opt/software/OpenFOAM/OpenFOAM-v1712/src/sampling/lnInclude -I/opt/software/OpenFOAM/OpenFOAM-v1712/src/meshTools/lnInclude -IlnInclude -I. -I/opt/software/OpenFOAM/OpenFOAM-v1712/src/OpenFOAM/lnInclude -I/opt/software/OpenFOAM/OpenFOAM-v1712/src/OSspecific/POSIX/lnInclude -fPIC -Xlinker --add-needed -Xlinker --no-as-needed Make/linux64GccDPInt32Opt/3DIBicoFoam_2.o -L/opt/software/OpenFOAM/OpenFOAM-v1712/platforms/linux64GccDPInt32Opt/lib \
-lfiniteVolume -lsampling -L/local/hulfeldl/hdf5/lib/libhdf5_hl_cpp.a -L/local/hulfeldl/hdf5/lib/libhdf5_cpp.a -L/local/hulfeldl/hdf5/lib/libhdf5_hl.a -L/local/hulfeldl/hdf5/lib/libhdf5.a -lhdf5_hl_cpp -lhdf5_cpp -lhdf5_hl -lhdf5 -lsz -lz -ldl -lm -lOpenFOAM -ldl \
-lm -o /local/hulfeldl/OpenFOAM/hulfeldl-v1712/applications/bin/linux64GccDPInt32Opt/3DIBicoFoam_2
Error:
Make/linux64GccDPInt32Opt/3DIBicoFoam_2.o: In function `main':
3DIBicoFoam_2.C:(.text.startup+0x107b): undefined reference to
`H5::H5Location::openDataSet(std::string const&) const'
3DIBicoFoam_2.C:(.text.startup+0x17de): undefined reference to
`H5::H5Location::openDataSet(std::string const&) const'
3DIBicoFoam_2.C:(.text.startup+0x189e): undefined reference to
`H5::H5Location::openDataSet(std::string const&) const'
3DIBicoFoam_2.C:(.text.startup+0x1919): undefined reference to
`H5::H5Location::openDataSet(std::string const&) const'
3DIBicoFoam_2.C:(.text.startup+0x19d0): undefined reference to
`H5::H5Location::openDataSet(std::string const&) const'
Please remove all -L/...a to start with. Add one single -L/local/hulfeldl/hdf5/lib leave all -lhdf... in the command. And retry.
For runtime please run the following before you run your program.
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/local/hulfeldl/hdf5/lib

How do I change the order in automake's CXXLINK?

I am trying to compile a simple C++ program with the automake tools. By itself, automake creates in its Makefile the line: CXXLINK = $(CXXLD) $(AM_CXXFLAGS) $(CXXFLAGS) $(AM_LDFLAGS) $(LDFLAGS) \ -o $#
This way, however, i get some linker errors, which can be resolved by simply putting the -o example example.cpp part in the beginning of the g++ command instead of the end.
How can I instruct automake to put the -o example example.cpp in front of the linker commands?
Here is the Makefile.am
bin_PROGRAMS = GLTest
GLTest_SOURCES = main.cpp
AM_CXXFLAGS=#gllibs_CFLAGS# -std=c++11 -pthread
AM_LDFLAGS=#gllibs_LIBS# -lGL -lGLEW -lglfw -lX11 -lXi -lXrandr
Here are the linker errors:
g++ -I/usr/include/libdrm -std=c++11 -pthread -g -O2 -lGLEW -lGLU -lGL -lglfw -lGL -lGLEW -lglfw -lX11 -lXi -lXrandr -o GLTest main.o
main.o: In function `main':
main.cpp:8: undefined reference to `glfwInit'
main.cpp:9: undefined reference to `glfwWindowHint'
main.cpp:10: undefined reference to `glfwWindowHint'
main.cpp:11: undefined reference to `glfwWindowHint'
main.cpp:12: undefined reference to `glfwWindowHint'
main.cpp:14: undefined reference to `glfwCreateWindow'
collect2: error: ld returned 1 exit status
These are resolved when I compile it manually like this:
g++ -o GLTest main.cpp -I/usr/include/libdrm -std=c++11 -pthread -g -O2 -lGLEW -lGLU -lGL -lglfw -lGL -lGLEW -lglfw -lX11 -lXi -lXrandr
You're doing it wrong. The problem is you're passing libraries as LDFLAGS, and that is incorrect.
You should have
GLTest_LDADD = $(gllibs_LIBS)
so that you're telling automake correctly that those are libraries.
Using libtool may work, depending on the libtool version used, since it can sort the command line, but it's still the wrong thing to do and fails in other situations.
From the linker command in the output it looks like you are not using libtool.
Try adding AM_PROG_LIBTOOL to the configure.ac.

From Qt3 to Qt4 : issues with internal classes ( Q3GList & Q3PtrCollection )

I'm updating a code from Qt3 to Qt4. I used qt3to4 porting tool, and it fixed some of my issues. But a few errors are remaining:
g++ -m64 -Wl,-O1 -o CONVECTION_DYN_CODE_version_initiale Convection_points.o Convection_points_prof.o glbox.o globjwin.o main.o algebra.o evector.o matrix.o matrix4.o quaternion.o write_binary.o write_binary_normals.o moc_glbox.o moc_globjwin.o -L/usr/lib/x86_64-linux-gnu -L/usr/X11R6/lib64 -lQtOpenGL -lQtGui -lQtCore -lGLU -lglut -lGL -lpthread -lCGAL -fp-model -lboost_thread -lgmp
glbox.o:(.rodata._ZTI7Q3GList[typeinfo for Q3GList]+0x10): undefined reference to `typeinfo for Q3PtrCollection'
&
glbox.o:(.rodata._ZTV7Q3GList[vtable for Q3GList]+0x18): undefined reference to `Q3GList::clear()'
glbox.o:(.rodata._ZTV7Q3GList[vtable for Q3GList]+0x20): undefined reference to `Q3GList::~Q3GList()'
glbox.o:(.rodata._ZTV7Q3GList[vtable for Q3GList]+0x28): undefined reference to `Q3GList::~Q3GList()'
glbox.o:(.rodata._ZTV7Q3GList[vtable for Q3GList]+0x30): undefined reference to `Q3PtrCollection::newItem(void*)'
...
Both Q3GList and Q3PtrCollection are internal classes. I added the corresponding headers but I still got these errors. I don't know how to fix it since it seems to be internal issues.
Any ideas?
You should also link against qt3support module.
Add
QT += qt3support
to your project settings.

OpenCV LDFlags linking issue

I am compiling a program with the following flags and getting errors (running 64 bit os):
g++ -lm -lml -lcvaux -lhighgui -lcv -lcxcore main.o BRIEF.o -o BRIEF_demo
I get a bunch of undefined references:
main.cpp:(.text+0x1f6): undefined reference to `cvInitMatHeader'
main.cpp:(.text+0x218): undefined reference to cvInitMatHeader'
main.o: In function_Z14drawQuadrangleP9_IplImageiiiiiiii8CvScalari.constprop.77':
main.cpp:(.text+0x2d5): undefined reference to cvLine'
main.cpp:(.text+0x333): undefined reference tocvLine'
main.cpp:(.text+0x398): undefined reference to cvLine'
main.cpp:(.text+0x3f2): undefined reference tocvLine'
Anyone have an idea how to circumvent this?
I suppose you are using the newest OpenCV 2.3.x. cvInitMatHeader() and cvLine() are actually defined in libcxcore.so, which I can see you are including.
My guess is that the order of the linking is wrong, so you need to adjust your command to something like:
g++ main.cpp BRIEF.cpp -o BRIEF_demo -lm -lml -lcvaux -lhighgui -lcv -lcxcore

How do I link and built a dynamic link library correctly?

I am in linux. My Makefile file is this
main2: main.cpp
g++ -c $(LIBS) $(CFLAGS) -fPIC main.cpp
g++ -shared main.o -o main.so
Where,
SDL_CFLAGS := $(shell sdl-config --cflags)
SDL_LDFLAGS := $(shell sdl-config --libs)
CC = gcc
COPTS = -g -Wall
CFLAGS = $(SDL_CFLAGS)
LIBS = -lstdc++ -lSDL $(SDL_LDFLAGS) -L/usr/X11R6/lib -lGL -lGLU
which runs
g++ -c -lstdc++ -lSDL -L/usr/lib -lSDL -L/usr/X11R6/lib -lGL -lGLU -I/usr/include/SDL -D_GNU_SOURCE=1 -D_REENTRANT -fPIC main.cpp
g++ -shared main.o -o main.so
Now, this works without error. main.o and main.so files are produced.
However, when I try to link main.os with python ctypes
from ctypes import * import os
libtest = cdll.LoadLibrary(os.getcwd()+ '/main.so') libtest.main_loop()
libtest.main_loop()
I get this error
>>> libtest = cdll.LoadLibrary(os.getcwd() + '/main.so')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.6/ctypes/__init__.py", line 431, in LoadLibrary
return self._dlltype(name)
File "/usr/lib/python2.6/ctypes/__init__.py", line 353, in __init__
self._handle = _dlopen(self._name, mode)
OSError: /home/atomos/DF/open_gl_client/ctypes_client/main.so: undefined symbol: glEnd
I am not sure if I am creating the linked library correctly. How do I create a linked library that I can load?
Do I have to create a .o and .os file for every library I import from main.cpp or is that taken care of automatically?
I do not understand what the compiler or linker is doing, however it works for a simple example with no imports, but for cpp files which import opengl libraries, it gives that error.
---- Update----
ldd against main.so yields
ldd main.so
linux-gate.so.1 => (0xb7755000)
libstdc++.so.6 => /usr/lib/libstdc++.so.6 (0xaf625000)
libm.so.6 => /lib/tls/i686/cmov/libm.so.6 (0xaf5ff000)
libc.so.6 => /lib/tls/i686/cmov/libc.so.6 (0xaf4a4000)
libgcc_s.so.1 => /lib/libgcc_s.so.1 (0xaf485000)
/lib/ld-linux.so.2 (0xb7756000)
---- Update ----
I ran g++ without -shared flag in the second compilation step and received this error
g++ main.o -o main.so
main.o: In function `Texture_map_list::add_texture(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)':
main.cpp:(.text+0xf0fe): undefined reference to `glGenTextures'
main.cpp:(.text+0xf2ba): undefined reference to `glBindTexture'
main.cpp:(.text+0xf2d7): undefined reference to `glTexParameterf'
main.cpp:(.text+0xf2f4): undefined reference to `glTexParameterf'
main.cpp:(.text+0xf310): undefined reference to `glTexParameteri'
main.cpp:(.text+0xf32c): undefined reference to `glTexParameteri'
main.cpp:(.text+0xf365): undefined reference to `gluBuild2DMipmaps'
main.o: In function `init()':
main.cpp:(.text+0xf457): undefined reference to `SDL_Init'
main.cpp:(.text+0xf46d): undefined reference to `SDL_WM_SetCaption'
main.cpp:(.text+0xf472): undefined reference to `SDL_GetVideoInfo'
main.cpp:(.text+0xf480): undefined reference to `SDL_GetError'
main.cpp:(.text+0xf497): undefined reference to `SDL_Quit'
main.cpp:(.text+0xf4e2): undefined reference to `SDL_GL_SetAttribute'
main.cpp:(.text+0xf505): undefined reference to `SDL_SetVideoMode'
main.cpp:(.text+0xf513): undefined reference to `SDL_GetError'
main.cpp:(.text+0xf52a): undefined reference to `SDL_Quit'
main.cpp:(.text+0xf559): undefined reference to `glClearColor'
main.cpp:(.text+0xf565): undefined reference to `glEnable'
main.cpp:(.text+0xf571): undefined reference to `glMatrixMode'
main.cpp:(.text+0xf576): undefined reference to `glLoadIdentity'
main.cpp:(.text+0xf5a2): undefined reference to `gluPerspective'
main.o: In function `process_keypresses()':
main.cpp:(.text+0x10678): undefined reference to `SDL_PollEvent'
main.cpp:(.text+0x109a1): undefined reference to `SDL_PollEvent'
main.o: In function `main_loop':
main.cpp:(.text+0x10d76): undefined reference to `SDL_GetKeyState'
main.cpp:(.text+0x10d9f): undefined reference to `SDL_Quit'
main.o: In function `render()':
main.cpp:(.text+0x10e00): undefined reference to `SDL_GetMouseState'
main.cpp:(.text+0x10e90): undefined reference to `SDL_GetMouseState'
main.cpp:(.text+0x10f94): undefined reference to `glClear'
main.cpp:(.text+0x10fa0): undefined reference to `glMatrixMode'
main.cpp:(.text+0x10fa5): undefined reference to `glLoadIdentity'
main.cpp:(.text+0x11081): undefined reference to `gluLookAt'
main.cpp:(.text+0x11120): undefined reference to `glTranslatef'
main.cpp:(.text+0x1114d): undefined reference to `glRotatef'
main.cpp:(.text+0x1117a): undefined reference to `glRotatef'
main.cpp:(.text+0x1119e): undefined reference to `SDL_GL_SwapBuffers'
main.o: In function `draw_plane(float, float, float)':
main.cpp:(.text+0x111d8): undefined reference to `glColor3f'
main.cpp:(.text+0x111e4): undefined reference to `glBegin'
main.cpp:(.text+0x1120b): undefined reference to `glVertex3f'
....
First, note that this is easier to debug if you don't build a shared library. You are building with -shared so the linker errors don't happen until you dynamically load the library in Python. Turn off -shared while you are debugging, and you will see the errors on the command-line when you try to link:
g++ main.o -o main
main.o: In function `main':
main.cpp:(.text+0x1d): undefined reference to `glBegin'
main.cpp:(.text+0x22): undefined reference to `glEnd'
collect2: ld returned 1 exit status
Now the problem is that you are passing linker arguments to the compiler. I see that you have nicely separated CFLAGS and LIBS in the Makefile. Good. But you are passing both $(LIBS) and $(CFLAGS) on the first line, to build main.o. The LIBS will be ignored on that line, because they are link flags.
On the second line, where you are actually building the final executable, you do not pass $(LIBS), so the program is not linked with libGL or any other libraries.
Therefore, simply fix your makefile thusly:
main2: main.cpp
g++ -c $(CFLAGS) -fPIC main.cpp
g++ -shared main.o $(LIBS) -o main.so
Edit: I have since realised that in GCC, you must always put the libraries after the object files, so I have changed the last line of the makefile accordingly.
Simple:
You are passing the link flags to object compilation step.
Here is what you need:
g++ -c -I/usr/include/SDL -D_GNU_SOURCE=1 -D_REENTRANT main.cpp
g++ -lstdc++ -lSDL -L/usr/lib -lSDL -L/usr/X11R6/lib -lGL -lGLU -fPIC main.o -o main
Plus you probably don't want to create so file, but a normal executable.