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

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.

Related

g++ fails to link libraries while creating a shared object

I am trying to create a static library for my engine.
The command (in a makefile) is:
g++ -c -fPIC window.cc -lGL -lGLEW -lSDL2 -std=c++14 -I../include/ && g++ -static window.o -lSDL2 -lGL -lGLEW -o ../distribute/so/window.so
So, the first command compiles successful, but the other one outputs this:
/usr/bin/ld: cannot find -lGL
/usr/bin/ld: cannot find -lGLEW
collect2: error: ld returned 1 exit status
But I definitely need those arguments. Can we fix it?
You need to know the exact paths of the library files to be linked with.
The directories that contain them should be added to your link line with -L.

Another undefined reference to boost::system::generic

So I have my project completed - each module works fine on it's own, but when I try to link them together suddenly I can't because I get the following error:
obj/main.o: In function `__static_initialization_and_destruction_0':
/usr/lib/boost_1_47_0/boost/system/error_code.hpp:214: undefined reference to `boost::system::generic_category()'
/usr/lib/boost_1_47_0/boost/system/error_code.hpp:215: undefined reference to `boost::system::generic_category()'
/usr/lib/boost_1_47_0/boost/system/error_code.hpp:216: undefined reference to `boost::system::system_category()'
obj/main.o: In function `error_code':
/usr/lib/boost_1_47_0/boost/system/error_code.hpp:315: undefined reference to `boost::system::system_category()'
obj/main.o: In function `boost::asio::error::get_system_category()':
/usr/lib/boost_1_47_0/boost/asio/error.hpp:216: undefined reference to `boost::system::system_category()'
obj/asio.o: In function `__static_initialization_and_destruction_0':
/usr/lib/boost_1_47_0/boost/system/error_code.hpp:214: undefined reference to `boost::system::generic_category()'
/usr/lib/boost_1_47_0/boost/system/error_code.hpp:215: undefined reference to `boost::system::generic_category()'
/usr/lib/boost_1_47_0/boost/system/error_code.hpp:216: undefined reference to `boost::system::system_category()'
collect2: ld returned 1 exit status
make: *** [all] Error 1
Here is the command I'm using to compile:
[root#engdev GDAgent]# make all
g++ --std=c++0x -c main.cpp -o obj/main.o -g -I/usr/include/mysql -I/usr/include/mysql++/ -I/usr/lib/boost_1_47_0/ -lmysqlpp -lnsl -lz -lm -lboost_thread -lboost_system -L/usr/lib64/ -L/usr/lib/mysql/ -I/usr/lib/boost_1_47_0/stage/lib/
g++ --std=c++0x -Wall -Wextra obj/main.o obj/asio.o obj/dbase.o obj/serial.o -o GDAgent -g -L/obj-L/usr/lib/ -L/usr/lib/mysql/ -I/usr/include/mysql -lmysqlpp -lm -lnsl -lz -I/usr/include/mysql++/ -lmysqlpp -I/usr/lib/boost_1_47_0/ -lboost_thread -I/usr/lib/boost_1_47_0/stage/lib/ -lboost_system-mt
This is my makefile:
all: GDAgent
g++ --std=c++0x -Wall -Wextra obj/main.o obj/asio.o obj/dbase.o obj/serial.o -o GDAgent -g -L/obj-L/usr/lib/ -L/usr/lib/mysql/ -I/usr/include/mysql -lmysqlpp -lm -lnsl -lz -I/usr/include/mysql++/ -lmysqlpp -I/usr/lib/boost_1_47_0/ -lboost_thread -I/usr/lib/boost_1_47_0/stage/lib/ -lboost_system-mt
GDAgent: include/serial.h include/dbase.h
g++ --std=c++0x -c main.cpp -o obj/main.o -g -I/usr/include/mysql -I/usr/include/mysql++/ -I/usr/lib/boost_1_47_0/ -lmysqlpp -lnsl -lz -lm -lboost_thread -lboost_system -L/usr/lib64/ -L/usr/lib/mysql/ -I/usr/lib/boost_1_47_0/stage/lib/
dbase.o: include/dbase.cpp include/dbase.h include/serial.h
g++ --std=c++0x -o obj/dbase.o -c include/dbase.cpp -g -I/usr/include/mysql++ -I/usr/include/mysql -I/usr/lib/boost_1_47_0/ -lmysqlpp -lnsl -lz -lm -lboost_thread -L/usr/lib/ -L/usr/lib/mysql/ -I/usr/lib/boost_1_47_0/stage/lib/ -lboost_system
serial.o: include/serial.cpp include/serial.h include/dbase.h
g++ --std=c++0x -o obj/serial.o -c include/serial.cpp -g -I/usr/include/mysql -I/usr/lib/boost_1_47_0/ -lmysqlpp -lnsl -lz -lm -lboost_thread -lboost_system -L/usr/lib/ -L/usr/lib/mysql/ -I/usr/lib/boost_1_47_0/stage/lib/
asio.o: include/asio.cpp include/serial.h include/dbase.h
g++ --std=c++0x -o obj/asio.o -c include/asio.cpp -g -I/usr/include/mysql -I/usr/lib/boost_1_47_0/ -lmysqlpp -lnsl -lz -lm -lboost_thread -L/usr/lib/ -L/usr/lib/mysql/ -I/usr/lib/boost_1_47_0/stage/lib/ -lboost_system
I've been searching through solutions here, and thus far the solutions I've found either I'm doing them incorrectly or they simply aren't the cause of my problem. (e.g. moving -lboost_system-mt to the end, etc.)
So how do I fix this?
Prefix your library path(s) with -L instead of -I
-L/usr/lib/boost_1_47_0/stage/lib/ -lboost_system-mt
-I is for header include paths.

OpenCL/OpenGL Interop on ARM

i have a small problem with OpenCL/OpenGL interoperability in my code
here's my makefile
LIBS = -lm -lOpenCL -lGAL -lGLEW -lglut -lGLU -lpthread
CFLAGS = -Wall -g
OBJECTS = main.o environment.o input.o animate.o buddhabrot.o buddhacl.o cmodules/timer.o
all: prognonmpi
prognonmpi: $(OBJECTS)
LIBRARY_PATH=/usr/lib/arm-linux-gnueabi/ c++ $(CFLAGS) -o prognonmpi $(OBJECTS) $(LIBS)
%.o: %.cpp $(LIBS)
clean:
rm -f *.o prog cmodules/*.o
the code just comiles fine, but i get a linker error with the following message
LIBRARY_PATH=/usr/lib/arm-linux-gnueabi/ c++ -Wall -g -o prognonmpi main.o environment.o input.o animate.o buddhabrot.o buddhacl.o cmodules/timer.o -lm -lOpenCL -lGAL -lGLEW -lglut -lGLU -lpthread
main.o: In function `displayFunc()':
main.cpp:(.text+0xdda): undefined reference to `clEnqueueAcquireGLObjects'
main.cpp:(.text+0xe12): undefined reference to `clEnqueueReleaseGLObjects'
main.o: In function `initOpenGLBuffers(int, int)':
main.cpp:(.text+0x136a): undefined reference to `clCreateFromGLBuffer'
collect2: ld returned 1 exit status
make: *** [prognonmpi] Error 1
the code compiles very fine on my notebook with the lins, using -lGL instead of -lGAL, seesm -lGAL is ARM specific.
on the ARM machine i get the above mentioned error.
Any suggestions what is missing on my ARM-machine?
using Ubuntu as OS

Installing OpenGL on Ubuntu 13.10

I am a university student trying to setup my home desktop with Ubuntu 13.10 for OpenGL development. I have installed all of these libraries. Our first assignment was to write a simple program and a generic makefile that would work for the rest of the semester. This worked perfectly in the Computer Labs on campus.
My main.c file:
# include <GL/glut.h>
void display();
int main (int argc, char ** argv)
{
glutInit(&argc , argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH );
glutInitWindowSize(640 , 480) ;
glutCreateWindow("Practical 1");
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
void display()
{
glClearColor(0.0 , 0.0 , 1.0 , 1.0);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glFlush();
glutSwapBuffers();
}
And my makefile:
STUDENT_NUMBER = 1234567890
TASK = practical
NUMBER =1
OBJECTS=$(addsuffix .o,$(basename $(shell ls *.C)))
WARNING_FLAGS = -Wall -Wextra -Weffc++ -Winit -self -Wmissing -include -dirs -Wswitch -default -Wswitch -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
FORMATTING_FLAGS = -fno -pretty -templates -fmessage -length=80 -fdiagnostics -show -option
CFLAGS = ${WARNING_FLAGS} ${FORMATTING_FLAGS} g -std=c++0x -pipe -frepo
LDLIBS = -lGL -lglut -lGLEW -lGLU -lX11 -lXi -lm -lrt -lpng
LDFLAGS =
CC = g++
TARGET = main
all: ${OBJECTS}
${CC} ${LDFLAGS} ${LDLIBS} $^ -o ${TARGET}
%.o: %.c
${CC} ${CFLAGS} -c ${LDFLAGS} $< -o $#
makefile.dep: *.[Ch]
for i in *.C; do gcc -MM "$${i}"; done > $#
include makefile.dep
clean:
rm ${OBJECTS} *.rpo *.gch makefile.dep ${TARGET}
tar:
make clean; tar -cvjf ${STUDENT_NUMBER}_${TASK}_${NUMBER}.tar.bz2 *
When trying to compile on my home desktop I get these erors:
g++ -lGL -lglut -lGLEW -lGLU -lX11 -lXi -lm -lrt -lpng main.o -o main
main.o: In function `main':
main.C:(.text+0x1e): undefined reference to `glutInit'
main.C:(.text+0x28): undefined reference to `glutInitDisplayMode'
main.C:(.text+0x37): undefined reference to `glutInitWindowSize'
main.C:(.text+0x41): undefined reference to `glutCreateWindow'
main.C:(.text+0x4b): undefined reference to `glutDisplayFunc'
main.C:(.text+0x50): undefined reference to `glutMainLoop'
main.o: In function `display()':
main.C:(.text+0x76): undefined reference to `glClearColor'
main.C:(.text+0x80): undefined reference to `glClear'
main.C:(.text+0x85): undefined reference to `glFlush'
main.C:(.text+0x8a): undefined reference to `glutSwapBuffers'
collect2: error: ld returned 1 exit status
make: *** [all] Error 1
I think I might be missing some library or something.
As explained in this question, you need to specify the dependant libraries in after the objects that reference them in your linker command:
g++ main.o -lGL -lglut -lGLEW -lGLU -lX11 -lXi -lm -lrt -lpng -o main
I'm not an expert on makefile syntax, but I think changing the 'all' section of your makefile like this might work:
all: ${OBJECTS}
${CC} ${LDFLAGS} $^ ${LDLIBS} -o ${TARGET}

FreeGLUT linking Issues in Linux

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