Skip to content
Advertisement

makefile : How to link object files

I have makefile and I need to link two objects into “main” object

They are -> oglinet.o and libshape.o

their path in system -> home/pi/openvg/

Problem :I need to write full path and objects name(home/pi/openvg/libshapes.o) is possible to “make” them as Makefile variable for example home/pi/openvg/libshape.o into $(OBJ1) in makefile rule ?

Tried to make them as variable for example Obj1= /home/pi/openvg/oglinit.o if I compile that the compilator freeks out.

Working Makefile

#NOT IDEAL MAKEFILE BUT WORKING!!!!!

INCLUDEFLAGS = -I/opt/vc/include -I/opt/vc/include/interface/vmcs_host/linux -I/opt/vc/include/interface/vcos/pthreads -I/home/pi/openvg
LIBFLAGS = -L/opt/vc/lib -lbrcmEGL -lbrcmGLESv2 -lbcm_host -lpthread  -ljpeg

main:main.cpp 
        g++ -Wall $(INCLUDEFLAGS) -o main  main.cpp $(LIBFLAGS) home/pi/openvg/libshapes.o /home/pi/openvg/oglinit.o

Expectations: what libshape.o and oglinit.o objects and thei path in system are “stored” in some kind of variable/variables and if I need I can easily make changes in makefile /

After help of Chriss Dodd (not ideal?) makefile looks like this

INCLUDEFLAGS = -I/opt/vc/include -I/opt/vc/include/interface/vmcs_host/linux -I/opt/vc/include/interface/vcos/pthreads -I/home/pi/openvg
LIBFLAGS = -L/opt/vc/lib -lbrcmEGL -lbrcmGLESv2 -lbcm_host -lpthread  -ljpeg

OPENVG=/home/pi/openvg

App: main.cpp $(OPENVG)/libshapes.o $(OPENVG)/oglinit.o
    g++ -Wall  -o $@  $^ $(LIBFLAGS) $(INCLUDEFLAGS)

Advertisement

Answer

You usually want to make all the object files you’re linking dependencies of the target. Then you can just use $^. You also usually want all the libraries after all the object files on the linker command line. With both of those you end up with something like:

OPENVG=/home/pi/openvg

main: main.o $(OPENVG)/libshape.o $(OPENVG)/objinit.o
        g++ -Wall -o $@ $^ $(LIBFLAGS)
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement