Category Archives: c/c++

c/c++ related work

Creating an OpenGL texture in a multi-threaded environment

When creating an OpenGL texture in a multi-threaded environment the glGenTextures method will return (fill in the output argument) with zero value if the call is made from thread that hasn’t got the OpenGL context.

Additionally, glGenTextures cannot be called between glBegin and glEnd calls.

Advertisements

Silent make by removing echo

To remove default output of make command start each line of the Makefile that you wish to ‘silence’ with ‘@’:

@rm -f *.o

This can cause problems when the compiler generates an error. In that case you need to catch the error and terminate the make process. You should also generate a message saying which command caused the problem since this command won’t be printed on the screen (The error message will show up though). Use following code to do just that:

default:
    @echo "Compiling helloworld.cpp";
    @gcc helloworld.cpp $(CFLAGS) -o helloworld \
    || (echo "Error in command: gcc helloworld.cpp $(CFLAGS) -o helloworld" && false)

Compiling cython code as c++ (PySim)

Use following makefile as template:

CURDIR=$(shell pwd)../
PYXFLAGS=-I$(CURDIR)/api -I$(CURDIR)/include
CFLAGS+=-I$(CURDIR)/include -I$(CURDIR)/api
CFLAGS+=-Wno-write-strings -Wall -O2 -Os -fno-strict-aliasing -g
default: fri.so

fri.cpp: fri.pyx server.pyx libfri.h libfri.cpp
 cython fri.pyx -I. -I../api --cplus

fri.so: fri.cpp
 g++ -shared -o fri.so fri.cpp libfri.cpp friremote.cpp friudp.cpp -I/usr/include/python2.6 -DHAVE_TIME_STAMP_RECEIVE $(CFLAGS)
 rm -f fri.cpp

clean:
 rm -f frifirstapp frisecondapp test_fri
 rm -f *.o *.so fri.cpp

The “cython fri.pyx -I. -I../api –cplus” line creates fri.cpp.

Remember to use proper $(CFLAGS) as above.

“undefined reference to” error

One of possible causes of this error is incorrect linking order of libraries using gcc/g++.

Linking libraries before object files causes the error:

g++ -o $(LIBS) $(PROGRAM) $(OBJS)

Linking libraries after object files solves the problem:

g++ -o $(PROGRAM) $(OBJS) $(LIBS)

This is a solution for static library linking. Shared libraries should link both ways. Also order of respective libraries is important. If library A uses library B, library B has to linked first (-lB -lA).