g ++ -I重复库 [英] g++ -I duplicating libraries
问题描述
我正在学习在Linux中编程C ++,我有以下问题:
I'm learning to program C++ in Linux, and I have the following problem:
我有4个文件:main.cpp,model.cpp,view。 cpp和controller.cpp,每个(除了main)都有自己的头文件。 main.cpp包括model.h,view.h和controller.h,而view.h包括只与它相关的其他库(运行图形库所必需的)。这些库在不同的文件夹,并有自己的其他依赖(这就是为什么我不想移动它们)。所以,我的makefile如下:
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
,还有一行将所有文件链接在一起(我没有添加它,在我的Mac上,并从我的树莓派的屏幕复制它。)
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
我编译视图与make view,它可以找到包含没有问题的文件,因为它有它必须看起来的路径,但由于make main没有那些路径,它不知道在哪里看for openvg.h。问题是,如果我添加的路径到main,它告诉我有多个定义的库里面的内容...任何帮助?
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?
推荐答案
#include VG / openvg.h
位于 / opt / vc / include
view.h
当您 make view
包括它从 -I / opt / vc / include
当你 make main
时,你从 main.cpp
您需要添加标志
main: main.cpp
g++ -c -I/opt/vc/include main.cpp
您可能还需要其他标志,取决于 view.h
包括的标志。
You may need the other flags as well, depending on what view.h
includes.
多个定义是由 $(OPENVGLIBDIR)/fontinfo.h
p>
which contains
Fontinfo SansTypeface, SerifTypeface, MonoTypeface;
因此,如果您将该文件包含在多个对象文件中并链接它们( main.o
和 view.o
),你会得到多个定义。
so if you include that file in more than one object file and link them (main.o
and view.o
in this case) you will get multiple definitions.
将其更改为
extern Fontinfo SansTypeface, SerifTypeface, MonoTypeface;
然后我得到了Background etc undefined as libshapes是一个C库。
I then got Background etc undefined as libshapes is a C library. To get round this I changed
#include "fontinfo.h"
#include "shapes.h"
在 view.h
到
extern "C" {
#include "fontinfo.h"
#include "shapes.h"
}
并且它为我构建(已删除对模型和控制器的引用)。
and it builds for me (with references to model and controller removed).
在你的makefile中,视图/模型/控制器/主目标是使 .o
文件,因此它们应该 .o
。当没有 .o
存在时,进行测试
时,它正在查找 .o
target。如果你的makefile中没有一个,它将使用默认值,它是一个直接的 g ++ -c
。
In your makefile, the view/model/controller/main targets are making the .o
files so they should be .o
too. When you make 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 straight g++ -c
.
您的make文件应如下所示:
Your make file should be like this:
test: model.o view.o controller.o main.o $(OPENVGLIBDIR)/libshapes.o $(OPENVGLIBDIR)/oglinit.o
g++ -o test view.o main.o model.o controller.o $(OPENVGLIBDIR)/libshapes.o $(OPENVGLIBDIR)oglinit.o -L/opt/vc/lib -L/opt/vc/include -lGLES -ljpeg
view.o: view.cpp
g++ -I/opt/vc/include -I/opt/vc/include/interface/vcos/pthreads -I.. -c view.cpp
main.o: main.cpp
g++ -I/opt/vc/include -I/opt/vc/include/interface/vcos/pthreads -I.. -c main.cpp view.cpp
model.o: model.cpp
g++ -c model.cpp
controller.o: controller.cpp
g++ -c controller.cpp
这篇关于g ++ -I重复库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!