I'm learning to program C++ in Linux, and I have the following problem:
I have 4 files: main.cpp, model.cpp, view.cpp and controller.cpp, each (except main) with their own header files. main.cpp includes model.h, view.h and controller.h, and view.h includes other libraries that are only relevant to it (necessary to run the graphic library). Those libraries are in different folders and have other dependencies on their own (that's why I don't want to move them). So, my makefile looks as follows:
model: model.cpp model.h
g++ -c model.cpp
view: view.cpp view.h
g++ -I/opt/vc/include -I/opt/vc/include/interface/vcos/pthreads -I.. -c view.cpp
controller: controller.cpp
g++ -c controller.cpp
main: main.cpp
g++ -c main.cpp
and also a line to link all the files together (I didn't added it because I'm writing this on my Mac and copying it from the screen of my Raspberry Pi).
My problem is that when I try to compile them, all of them work, except for main, it tells me the following:
In file included from main.cpp:6:0:
view.h:4:23: fatal error: VG/openvg.h: No such file or directory
compilation terminated.
make: *** [main] Error 1
From what I can understand, when I compile view with "make view", it can find the files included without problem, because it has the paths in which it must look, but since "make main" doesn't have those paths, it doesn't know where to look for openvg.h. The problem is that if I add the paths to main, it tells me that there's multiple definitions for what's inside the library... Any help?
here are my files, the code seems to run fine if in main.cpp I include view.cpp instead of view.h, but I don't think that's what I'm supposed to be doing:
main.cpp
view.h:
view.cpp
The
#include VG/openvg.h
is in/opt/vc/include
and is included fromview.h
When you
make view
you are including it fromview.cpp
which is compiled with-I/opt/vc/include
When you
make main
you are including it frommain.cpp
which is compiled without flags so it can't find the file.You need to add the flags
You may need the other flags as well, depending on what
view.h
includes.The multiple definitions are caused by
$(OPENVGLIBDIR)/fontinfo.h
which contains
so if you include that file in more than one object file and link them (
main.o
andview.o
in this case) you will get multiple definitions.Change it to
I then got Background etc undefined as libshapes is a C library. To get round this I changed
in
view.h
toand it builds for me (with references to model and controller removed).
In your makefile, the view/model/controller/main targets are making the
.o
files so they should be.o
too. When youmake test
when no.o
exists, it is looking for the.o
target. If there isn't one in your makefile it will use the default which is a straightg++ -c
.Your make file should be like this:
Thanks A LOT for your help man! I've been fighting with this for the last couple of days (that's what I get for getting used to automatic compiling/linking). It's somewhat based on that example. I can make it run if I make each object by itself and then link them all together like this:
if I do "make view" "make main" and then "make test" everything goes fine, but if I try "make test" without any object created prior to that I get fatal error: VG/openvg.h: No such file or directory when it's trying to compile view.cpp