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}
Related
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.
Downloaded the code from this source: http://n.ethz.ch/~vartokb/gl-shaders.html
I run the make file and I get undefined references to xlib and glut libraries. e.g.,
undefined reference to `XOpenDisplay'
undefined reference to `XDefaultScreen'
undefined reference to `glXChooseVisual'
.
.
.
undefined reference to `__glewGenBuffers'
undefined reference to `__glewBindBuffer'
undefined reference to `__glewBufferData'
The compile line looks like this and the makefile is below:
g++ -L/usr/include/GL/ -L/usr/include/X11/ -lm -lrt -lglut -lGLEW -lGL -lGLU -lX11 main.o events.o graphics.o -o gl-shaders
The Makefile:
NAME = gl-shaders
CXX = g++
CXXFLAGS = -O3
LDFLAGS = -L/usr/include/GL/ -L/usr/include/X11/ -lm -lrt -lglut -lGLEW -lGL -lGLU -lX11
SRCS = main.cpp events.cpp graphics.cpp
OBJS = $(SRCS:%.cpp=%.o)
HDRS = config.hpp main.hpp
all: $(NAME)
$(NAME): $(OBJS)
$(CXX) $(LDFLAGS) $(OBJS) -o $(NAME)
.cpp.o:
$(CXX) $(CFLAGS) $(LFLAGS) -c $< -o $#
$(OBJS): $(HDRS)
clean:
rm -f $(OBJS)
rm -f $(NAME)
I have installed Xlib by running:
sudo aptitude install xorg-dev
sudo apt install xorg-dev
sudo apt-get install libx11-dev
And I installed freeglut a while ago. When I list the contents of the directories linked in the makefile I see:
:l /usr/include/GL/
freeglut_ext.h freeglut_std.h glew.h gl.h glu.h glut.h glxext.h glxint.h glxmd.h glxtokens.h wglew.h
freeglut.h glcorearb.h glext.h gl_mangle.h glu_mangle.h glxew.h glx.h glx_mangle.h glxproto.h internal/
and for Xlib:
l /usr/include/X11/
ap_keysym.h Constraint.h DECkeysym.h HPkeysym.h IntrinsicP.h RectObj.h ShellI.h TranslateI.h Xatom.h Xdmcp.h XKBlib.h Xmd.h Xpoll.h Xthreads.h Xwindows.h
bitmaps/ ConvertI.h dri/ ICE/ keysymdef.h RectObjP.h ShellP.h VarargsI.h Xauth.h XF86keysym.h XlibConf.h Xmu/ Xproto.h Xtos.h Xwinsock.h
CallbackI.h Core.h EventI.h ImUtil.h keysym.h ResConfigP.h SM/ Vendor.h Xaw/ Xft/ Xlib.h Xosdefs.h Xprotostr.h Xtrans/
Composite.h CoreP.h extensions/ InitialI.h Object.h ResourceI.h StringDefs.h VendorP.h Xcms.h Xfuncproto.h Xlibint.h Xos.h Xregion.h Xutil.h
CompositeP.h CreateI.h fonts/ Intrinsic.h ObjectP.h SelectionI.h Sunkeysym.h Xalloca.h Xcursor/ Xfuncs.h Xlib-xcb.h Xos_r.h Xresource.h Xw32defs.h
ConstrainP.h cursorfont.h HookObjI.h IntrinsicI.h PassivGraI.h Shell.h ThreadsI.h Xarch.h Xdefs.h X.h Xlocale.h xpm.h xshmfence.h XWDFile.h
Why do I get undefined references when I have all the required files for compilation?
My problem was parameter ordering, as user cyberguijarro points out over here: Linker problems in Ubuntu 11.10
#boto I know that. Actually, the problem was in the parameter order.
Ubuntu 11.10 ships GCC 4.6.1, which apparently introduces stricter
parameter ordering constraints. g++ test.cpp -lX11 works fine. –
cyberguijarro Jan 24 '12 at 9:37
So by changing the compile line from this:
g++ -I/usr/include/GL/ -lglut -lGL -lGLEW -lGLU -I/usr/include/X11/ -lm -lrt -lX11 main.o events.o graphics.o -o gl-shaders
to this:
g++ main.o events.o graphics.o -I/usr/include/GL/ -lglut -lGL -lGLEW -lGLU -I/usr/include/X11/ -lm -lrt -lX11 -o gl-shaders
allowed it to compile.
I am developing a little program with pocketsphinx (speech to text - library).
On Windows i was using Code::Blocks as development environment and i had success to build a program.
Now i try to port my program to Linux and i am having little problems to link against pocketsphinx.
This is the Makefile:
CC = g++
CFLAGS = -Wall -std=c++11
LDFLAGS = -I/usr/local/include/sphinxbase -I/usr/local/include/pocketsphinx
OBJ = obj/Application.o obj/Main.o obj/Recorder.o
all: $(OBJ)
$(CC) -L/usr/local/lib -o bin/Eve $(OBJ) -s -lsphinxbase -lpocketsphinx
obj/Main.o: src/Main.cpp
$(CC) $(CFLAGS) $(LDFLAGS) -c src/Main.cpp -o obj/Main.o
obj/Application.o: src/Application.cpp src/Application.hpp
$(CC) $(CFLAGS) $(LDFLAGS) -c src/Application.cpp -o obj/Application.o
obj/Recorder.o: src/Recorder.cpp src/Recorder.hpp
$(CC) $(CFLAGS) $(LDFLAGS) -c src/Recorder.cpp -o obj/Recorder.o
It is the same which i was using on Windows, i just adjusted the file path.
I am receiving the following error:
$ make
g++ -Wall -std=c++11 -I/usr/local/include/sphinxbase -I/usr/local/include/pocketsphinx -c src/Application.cpp -o obj/Application.o
g++ -Wall -std=c++11 -I/usr/local/include/sphinxbase -I/usr/local/include/pocketsphinx -c src/Main.cpp -o obj/Main.o
g++ -Wall -std=c++11 -I/usr/local/include/sphinxbase -I/usr/local/include/pocketsphinx -c src/Recorder.cpp -o obj/Recorder.o
g++ -L/usr/local/lib -o bin/Eve obj/Application.o obj/Main.o obj/Recorder.o -s -lsphinxbase -lpocketsphinx
obj/Recorder.o: In function `Recorder::Recorder()':
Recorder.cpp:(.text+0x1c0): undefined reference to `ad_open_sps'
Recorder.cpp:(.text+0x20d): undefined reference to `ad_read'
Recorder.cpp:(.text+0x215): undefined reference to `cont_ad_init'
obj/Recorder.o: In function `Recorder::~Recorder()':
Recorder.cpp:(.text+0x2f3): undefined reference to `cont_ad_close'
Recorder.cpp:(.text+0x303): undefined reference to `ad_close'
obj/Recorder.o: In function `Recorder::recognizeFromMicrophone()':
Recorder.cpp:(.text+0x37a): undefined reference to `ad_start_rec'
Recorder.cpp:(.text+0x395): undefined reference to `cont_ad_calib'
Recorder.cpp:(.text+0x3f0): undefined reference to `cont_ad_read'
Recorder.cpp:(.text+0x4e6): undefined reference to `cont_ad_read'
Recorder.cpp:(.text+0x5b5): undefined reference to `ad_stop_rec'
Recorder.cpp:(.text+0x5d8): undefined reference to `ad_read'
Recorder.cpp:(.text+0x5f4): undefined reference to `cont_ad_reset'
collect2: error: ld returned 1 exit status
make: *** [all] Fehler 1
I don't think that it is a name mangling problem, since i built the lib on my own using the provided Makefile.
What can i do to link against the lib without errors?
EDIT: I figured out how to make it work. I simply modified the rule of the target "all" to this:
$(CC) -static -L/usr/local/lib -o bin/Eve $(OBJ) -s -lpocketsphinx -lsphinxbase -lsphinxad -lpthread
Functions like ad_read are defined in libsphinxad library, you need to add it to your linker command line:
g++ -static -L/usr/local/lib -o bin/Eve obj/Application.o obj/Main.o obj/Recorder.o \
-lpocketsphinx -lsphinxbase -libsphinxad
Please note that the order of libraries is important.
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
I am trying to compile the below test program:
#include <GL/glfw.h>
int main(int argc, char** argv) {
if(!glfwInit()) {
return -1;
}
if(!glfwOpenWindow(640, 480, 8, 8, 8, 0, 24, 0, GLFW_WINDOW)) {
return -1;
}
while(glfwGetWindowParam(GLFW_OPENED)) {
glfwSwapBuffers();
}
return 0;
}
but I always get undefined reference errors in regards to the GLFW functions.
Below is my makefile:
CXX = clang++
CXXFLAGS = -Wall -std=c++0x
LDFLAGS = -lglfw
OBJ_DIR = bin
LIB_DIR = -L/usr/lib
INC_DIR = -I/usr/include
SOURCE = main.cpp
OBJECTS = ${SOURCE:%.cpp=$(OBJ_DIR)/%.o}
EXECUTABLE = hello
all: init $(OBJECTS) $(EXECUTABLE)
$(EXECUTABLE):
$(CXX) $(CXXFLAGS) $(LDFLAGS) $(LIB_DIR) -o $# $(OBJECTS)
$(OBJ_DIR)/%.o: %.cpp
$(CXX) $(INC_DIR) -c $< -o $#
init:
#mkdir -p "$(OBJ_DIR)"
clean:
#rm -rf $(OBJ_DIR) $(EXECUTABLE)
I definitely have glfw.h and libglfw.a/.so as when I run locate glfw I get:
:~$ locate glfw
/usr/include/GL/glfw.h
/usr/lib/libglfw.a
/usr/lib/libglfw.so
/usr/lib/libglfw.so.2
/usr/lib/libglfw.so.2.6
The output of nm /usr/lib/libglfw.a | grep glfwInit:
:~$ nm /usr/lib/libglfw.a | grep glfwInit
U _glfwInitialized
U _glfwInitialized
U _glfwInitialized
U _glfwInitialized
0000000000000000 B _glfwInitialized
0000000000000000 T glfwInit
U _glfwInitialized
U _glfwInitialized
U _glfwInitialized
U _glfwInitialized
U _glfwInitialized
U _glfwInitJoysticks
U _glfwInitTimer
00000000000000c0 T _glfwInitJoysticks
0000000000000000 T _glfwInitTimer
and the verbose message from clang:
clang++ -I/usr/include -c main.cpp -o bin/main.o
clang++ -Wall -std=c++0x -Wl --verbose -lglfw -lGL -lGLU -L/usr/lib -o hello bin/main.o
Ubuntu clang version 3.0-6ubuntu3 (tags/RELEASE_30/final) (based on LLVM 3.0)
Target: x86_64-pc-linux-gnu
Thread model: posix
clang: warning: argument unused during compilation: '-std=c++0x'
"/usr/bin/ld" -z relro --hash-style=gnu --as-needed --build-id --eh-frame-hdr -m elf_x86_64 -dynamic-linker /lib64/ld-linux-x86-64.so.2 -o hello /usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crt1.o /usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crti.o /usr/lib/gcc/x86_64-linux-gnu/4.6/crtbegin.o -L/usr/lib -L/usr/lib/gcc/x86_64-linux-gnu/4.6 -L/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu -L/lib/x86_64-linux-gnu -L/lib/../lib64 -L/usr/lib/x86_64-linux-gnu -L/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu -L/usr/lib/gcc/x86_64-linux-gnu/4.6/../../.. -L/lib/x86_64-linux-gnu -L/lib -L/usr/lib/x86_64-linux-gnu -L/usr/lib -lglfw -lGL -lGLU bin/main.o -lstdc++ -lm -lgcc_s -lgcc -lc -lgcc_s -lgcc /usr/lib/gcc/x86_64-linux-gnu/4.6/crtend.o /usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crtn.o
bin/main.o: In function `main':
main.cpp:(.text+0x17): undefined reference to `glfwInit'
main.cpp:(.text+0x76): undefined reference to `glfwOpenWindow'
main.cpp:(.text+0x97): undefined reference to `glfwGetWindowParam'
main.cpp:(.text+0xa7): undefined reference to `glfwSwapBuffers'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [hello] Error 1
It seems to not be finding the library?
The problem is that the glfw libraries are being specified to the linker before the object file that depends on them. ld searches libraries to resolve dependencies only for the dependencies that it knows about at the point in list of files it's processing. So when ld is searching libglfw.a it doesn't know about the glfwInit dependency in main.o yet. ld (by default) doesn't go back an search the library again.
Try:
$(EXECUTABLE):
$(CXX) $(CXXFLAGS) $(LIB_DIR) -o $# $(OBJECTS) $(LDFLAGS)
Also the libraries should probably be specified in a LDLIBS (or LIBS) variable - LDFLAGS is conventionally use for linker options:
CXX = clang++
CXXFLAGS = -Wall -std=c++0x
LDLIBS = -lglfw -lGL -lGLU
OBJ_DIR = bin
LIB_DIR = -L/usr/lib
INC_DIR = -I/usr/include
SOURCE = main.cpp
OBJECTS = ${SOURCE:%.cpp=$(OBJ_DIR)/%.o}
EXECUTABLE = hello
all: init $(OBJECTS) $(EXECUTABLE)
$(EXECUTABLE):
$(CXX) $(LDFLAGS) $(LIB_DIR) -o $# $(OBJECTS) $(LDLIBS)
$(OBJ_DIR)/%.o: %.cpp
$(CXX) $(INC_DIR) -c $< -o $#
init:
#mkdir -p "$(OBJ_DIR)"
clean:
#rm -rf $(OBJ_DIR) $(EXECUTABLE)