在C ++中初始化vector >>(Initialize a vector > > in C++)
我有双向量传染媒介传染媒介。 我正在尝试初始化它。 以下代码段错误。 包括注释代码也没有帮助(不编译)。
vector<vector<vector<double> > > Q(MAX_GRID); for(int row = 0; row < MAX_GRID; row++) { //vector<vector<double> > inQ(MAX_GRID); //Q[row].push_back(inQ); for(int col = 0; col < MAX_GRID; col++) for(int action = 0; action <= 3; action++) Q[row][col].push_back(0); }
I have a vector of vector of vector of doubles. I'm trying to initialize it. The below code seg faults. Including the commented code also does not help (does not compile).
vector<vector<vector<double> > > Q(MAX_GRID); for(int row = 0; row < MAX_GRID; row++) { //vector<vector<double> > inQ(MAX_GRID); //Q[row].push_back(inQ); for(int col = 0; col < MAX_GRID; col++) for(int action = 0; action <= 3; action++) Q[row][col].push_back(0); }
原文:https://stackoverflow.com/questions/15872795
最满意答案
Q1:如何链接来自不同子目录的目标文件?
假设你的程序
prog
是一个C程序,它将从对象file0.o
,file1.o
链接,这些对象将被编译到子目录obj
。 以下是您在makefile中通常需要执行的链接。$(OBJS) = $(patsubst %.o,obj/%.o,file0.o file1.o) prog: $(OBJS) gcc -o $@ $(CFLAGS) $(OBJS) $(LDFLAGS) $(LDLIBS)
这使得
$(OBJS)
=obj/file0.o, obj/file1.o
并且只需将这样的对象文件传递给link命令。patsubst
文档注意如果你想编译一个目标文件到目标文件中,这个目录不存在就不足以创建
obj
子目录。 你必须自己创造它或研究如何make
做到这一点。Q2:如何包含不同的搜索路径?
这是一个含糊不清的问题 - 模棱两可令你感到困惑 - 我们必须将其分解为Q2.a,Q2.b,Q2.c :
Q2.a:如何指定不同的搜索路径,预处理器将查找源代码中
#include
-ed的头文件?默认情况下,预处理器将使用内置的标准搜索路径列表查找头文件。 您可以通过在详细模式下运行预处理器来查看它们,例如
cpp -v
(CTRL-C终止)。 输出将包含如下内容:#include "..." search starts here: #include <...> search starts here: /usr/lib/gcc/x86_64-linux-gnu/4.8/include /usr/local/include /usr/lib/gcc/x86_64-linux-gnu/4.8/include-fixed /usr/include/x86_64-linux-gnu /usr/include End of search list.
假设您在子目录
inc
和inc/foo/bar
有一些您自己的头文件,并且希望预处理程序也搜索这些目录。 然后您需要传递预处理器选项:-I inc -I inc/foo/bar
到你的编译命令。 预处理器选项通常分配给make变量
CPPFLAGS
,例如CPPFLAGS = -I inc -I inc/foo/bar
(以及您需要的任何其他预处理器选项),并在编译命令配方中通过此变量传递,例如
gcc -c -o $@ $(CPPFLAGS) $(CFLAGS) $<
注意:认为
CPPFLAGS
是C ++编译器标志的常规make变量是一个常见的错误。 C ++编译器标志的常规make变量是CXXFLAGS
。运行以下命令可以看到
-I
选项的效果:mkdir -p inc/foo/bar # Just to create the path cpp -v -I inc -I inc/foo/bar
(CTRL-C终止)。 现在输出将包含类似于:
#include "..." search starts here: #include <...> search starts here: inc inc/foo/bar /usr/lib/gcc/x86_64-linux-gnu/4.8/include /usr/local/include /usr/lib/gcc/x86_64-linux-gnu/4.8/include-fixed /usr/include/x86_64-linux-gnu /usr/include End of search list.
Q2.b:如何指定链接器将查找库的不同搜索路径?
假设您有一个库,
libfoobar.a
,您需要与prog
链接,并且它驻留在一个目录lib
,该lib
位于makefile的上两级。 然后你需要传递链接器选项:-L ../../lib
和
-lfoobar
到你的链接命令。 其中的第一个将告诉链接器
../../lib
是查找库的地方之一。 通常,您通过LDFLAGS
在链接器命令配方中传递此选项。 第二个告诉连接器搜索一个名为libfoobar.a
(一个静态库)或libfoobar.so
(一个动态库)的库。 通常,您通过LDLIBS
在链接器命令配方中传递此选项就像预处理程序的默认搜索路径列表一样,链接程序也有默认的搜索路径列表。 您可以通过运行来查看它们:
gcc -v -Wl,--verbose 2>&1 | grep 'LIBRARY_PATH'
输出结果如下所示:
LIBRARY_PATH = / usr / lib / gcc / x86_64-linux-gnu / 4.8 /:/ usr / lib / gcc / x86_64-linux-gnu / 4.8 /../../../ x86_64-linux-gnu / usr / lib / gcc / x86_64-linux-gnu / 4.8 /../../../../ lib /:/ lib / x86_64-linux-gnu /:/ lib /../ lib /:/ usr /lib/x86_64-linux-gnu/:/usr/lib/../lib/:/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../:/lib/: / usr / lib中/
当您需要链接其中一个默认库搜索路径中的标准库之一(例如
libm
(数学库))时,您不需要传递任何-L
选项。-lm
独自会做。Q2.c:如何指定不同的搜索路径,
make
将查找目标的先决条件?注意这是一个关于
make
的问题,而不是关于预处理器,编译器或链接器的问题。我们假定所有的目标文件都将被编译到子目录
obj
。 为了让它们在那里编译,只需使用模式规则就会很好,很简单:obj/%.o:%.c gcc -c -o $@ $(CPPFLAGS) $(CFLAGS) $<
这告诉
make
,例如obj/file0.o
是由file0.c
通过配方创建的:gcc -c -o obj/file0.o $(CPPFLAGS) $(CFLAGS) file0.c
对于任何文件
obj/*.o
和匹配文件*.c
相似的这很好,只要
file0.c
驻留在与makefile相同的目录中,但是假设你的*.c
文件在其他地方? 假设你的源文件被组织在子目录foo/file0.c
和bar/file1.c
。 然后make
将无法满足该模式规则,并会说“没有规则来制作目标obj / file0.o”等。为了解决这个问题,使用
VPATH
,一个具有特殊含义的make
变量。 如果您将目录名称列表(以':'为VPATH
分配给VPATH
,那么只要在当前目录中找不到目录名称,make
就会在每个列出的目录中搜索先决条件。 所以:VPATH = foo:bar
会导致
make
在当前目录中查找,然后在尝试查找.c
文件以符合该模式规则时查找foo
和bar
。 它将成功满足规则并编译必要的源文件。注意您在发布的代码中错误地使用了
VPATH
:VPATH = -L/home/dkumar/libtsnnls-2.3.3/tsnnls
您已经为其链接器搜索路径分配了链接器选项
-L
,该链接器选项在那里没有业务。底线:
预处理程序的搜索路径(用于定位头文件)由预处理程序的
-I<dirname>
选项指定。 将这些选项传递给CPPFLAGS
的编译配方用于定位库的链接程序搜索路径通过链接程序的
-L<dirname>
选项指定。 将这些选项传递给LDFLAGS
的链接配方
make
规则的preprequisities的搜索路径在make
变量VPATH
为一个':-punctuated目录名称列表。Q1: How to link object files from a different subdirectory?
Let's say your program
prog
is a C program that will be linked from objectfile0.o
,file1.o
which are to be compiled into subdirectoryobj
. Here is the sort of thing you typically need in your makefile to perform that linkage.$(OBJS) = $(patsubst %.o,obj/%.o,file0.o file1.o) prog: $(OBJS) gcc -o $@ $(CFLAGS) $(OBJS) $(LDFLAGS) $(LDLIBS)
This makes
$(OBJS)
=obj/file0.o, obj/file1.o
and you simply pass the object files like that to the link command. Documentation ofpatsubst
N.B. This is not sufficient to create the
obj
subdirectory if it doesn't exist when you want to compile an object file into it. You'll have to create it yourself or study how to makemake
do it.Q2: How to include different search paths?
This is an ambiguous question - the ambiguity is confusing you - and we must break it down into Q2.a, Q2.b, Q2.c:
Q2.a: How to specify different search paths where the preprocessor will look for header files that are
#include
-ed in the source code?By default, the preprocessor will look for header files using a built-in a list of standard search paths. You can see them by running the preprocessor in verbose mode, e.g.
cpp -v
(CTRL-C to terminate). The output will contain something like:#include "..." search starts here: #include <...> search starts here: /usr/lib/gcc/x86_64-linux-gnu/4.8/include /usr/local/include /usr/lib/gcc/x86_64-linux-gnu/4.8/include-fixed /usr/include/x86_64-linux-gnu /usr/include End of search list.
Let's suppose you have some header files of your own in subdirectories
inc
andinc/foo/bar
and want the preprocessor to search these directories as well. Then you need to pass the preprocessor options:-I inc -I inc/foo/bar
to your compile command. Preprocessor options are conventionally assigned to the make variable
CPPFLAGS
, e.g.CPPFLAGS = -I inc -I inc/foo/bar
(along with any other preprocessor options you require), and passed via this variable in the compile command recipe, e.g.
gcc -c -o $@ $(CPPFLAGS) $(CFLAGS) $<
N.B. It is a common mistake to think that
CPPFLAGS
is the conventional make variable for C++ compiler flags. The conventional make variable for C++ compiler flags isCXXFLAGS
.You can see the effect of the
-I
option by running:mkdir -p inc/foo/bar # Just to create the path cpp -v -I inc -I inc/foo/bar
(CTRL-C to terminate). Now the output will contain the like of:
#include "..." search starts here: #include <...> search starts here: inc inc/foo/bar /usr/lib/gcc/x86_64-linux-gnu/4.8/include /usr/local/include /usr/lib/gcc/x86_64-linux-gnu/4.8/include-fixed /usr/include/x86_64-linux-gnu /usr/include End of search list.
Q2.b: How to specify different search paths where the the linker will look for libraries?
Let's suppose you are have a library,
libfoobar.a
that you need to link withprog
and that it resides in a directorylib
that is 2 levels up from your makefile. Then you need to pass the linker options:-L ../../lib
and
-lfoobar
to your link command. The first of these will tell the linker that
../../lib
is one of the places to look for libraries. Conventionally, you pass this option in the linker command recipe viaLDFLAGS
. The second tells the linker to search for some library calledlibfoobar.a
(a static library) orlibfoobar.so
(a dynamic library). Conventionally, you pass this option in the linker command recipe viaLDLIBS
Just as there is a default list of search paths for the preprocessor, there is a default list of search paths for the linker. You can see them by running:
gcc -v -Wl,--verbose 2>&1 | grep 'LIBRARY_PATH'
The output will be something like:
LIBRARY_PATH=/usr/lib/gcc/x86_64-linux-gnu/4.8/:/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/: /usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../lib/:/lib/x86_64-linux-gnu/:/lib/../lib/: /usr/lib/x86_64-linux-gnu/:/usr/lib/../lib/:/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../:/lib/:/usr/lib/
When you need to link one of the standard libraries, e.g.
libm
(the math library), that resides in one of the the default library search paths, you don't need to pass any-L
option.-lm
alone will do.Q2.c: How to specify different search paths where
make
will look for the prerequisites of targets?N.B. This is a question about
make
, not a question about the preprocessor, compiler, or linker.We have assumed that all of your object files will be compiled into the subdirectory
obj
. To get them compiled there, it would be nice and simple just to use the pattern rule:obj/%.o:%.c gcc -c -o $@ $(CPPFLAGS) $(CFLAGS) $<
which tell
make
that, e.g.obj/file0.o
is made fromfile0.c
by the recipe:gcc -c -o obj/file0.o $(CPPFLAGS) $(CFLAGS) file0.c
and similarly for any file
obj/*.o
and matching file*.c
This is fine as long
file0.c
resides in the same directory as the makefile, but suppose you have your*.c
files somewhere else? Say your source files are organised in subdirectories,foo/file0.c
andbar/file1.c
. Thenmake
will be unable to satisfy that pattern rule and will say there is "no rule to make target obj/file0.o", etc.To solve this problem use
VPATH
, amake
variable that has a special meaning. If you assign a list of directory names, punctuated by ':', toVPATH
, thenmake
will search for a prerequisite in each of the listed directories whenever it can't find it in the current directory. So:VPATH = foo:bar
will cause
make
to look in the current directory, and thenfoo
andbar
when it tries to find.c
files to match that pattern rule. It will succeed in satisfying the rule and will compile the necessary source files.N.B. You have used
VPATH
wrongly in your posted code:VPATH = -L/home/dkumar/libtsnnls-2.3.3/tsnnls
You have asigned a linker search path to it, with the linker option
-L
, which has no business there.Bottom line:
The preprecessor search paths, for locating header files, are specified with the preprocessor's
-I<dirname>
option. Pass these options to the compile recipe inCPPFLAGS
The linker search paths, for locating libraries, are specified with the linker's
-L<dirname>
option. Pass these options to the linkage recipe inLDFLAGS
The search paths for the preprequisities of
make
rules are specified in themake
variableVPATH
, as a ':-punctuated list of directory names.
相关问答
更多-
使用automake将头文件放在/ usr / include的子目录中?(Placing header files in a subdirectory of /usr/include with automake?)[2022-03-26]
和你提到的pkginclude_HEADERS ,你也可以用你喜欢的任何名字将头文件安装到/usr/include的任意子目录中,如下所示: otherincludedir = $(includedir)/arbitrary_name otherinclude_HEADERS = a.h b.h As well as pkginclude_HEADERS, which you mention, you can also install header files into an arbitrary subd ... -
由于您使用GNUmake,因此使用模式规则来编译对象文件: $(OBJDIR)/%.o: %.c $(CC) $(CFLAGS) $(CPPFLAGS) -c -o $@ $< Since you're using GNUmake, use a pattern rule for compiling object files: $(OBJDIR)/%.o: %.c $(CC) $(CFLAGS) $(CPPFLAGS) -c -o $@ $<
-
Makefile子目录递归(Makefile subdirectory recursive)[2023-05-09]
我会这样做: SUBDIRS = lib1 lib2 lib3 main: main.cpp $(SUBDIRS) $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $< $(wildcard $(addsuffix /obj/*.o,$(SUBDIRS))) $(LDLIBS) .PHONY: $(SUBDIRS) $(SUBDIRS): $(MAKE) -C $@ 关于这一点的好处:它可以与make -jN用于并行构建,如果你只想构建一个子目录,你可 ... -
PHP包含根文件夹中不同子目录的文件(PHP include files from different subdirectories within the root folder)[2021-09-25]
@ sandeep的回答是部分正确的。 因为 $_SERVER['DOCUMENT_ROOT']; 将再次返回到root的完全限定路径。 将返回图像,因为现在我没有根据您的问题给它本地路径,但我正在运行的服务器的URL。 @sandeep's answer is partially right. because $_SERVER['DOCUMENT_ROOT']; will again ... -
用它来测试: foreach (glob("addons/*.php", GLOB_NOCHECK) as $filename) { PRINT $filename . "\n"; } 如果目录不存在相对于当前的目录,那么它将显示addons/*.php作为输出。 Use this for testing: foreach (glob("addons/*.php", GLOB_NOCHECK) as $filename) { PRINT $filename . "\n"; } Shoul ...
-
Q1:如何链接来自不同子目录的目标文件? 假设你的程序prog是一个C程序,它将从对象file0.o , file1.o链接,这些对象将被编译到子目录obj 。 以下是您在makefile中通常需要执行的链接。 $(OBJS) = $(patsubst %.o,obj/%.o,file0.o file1.o) prog: $(OBJS) gcc -o $@ $(CFLAGS) $(OBJS) $(LDFLAGS) $(LDLIBS) 这使得$(OBJS) = obj/file0.o, obj/f ...
-
Make的一个根本缺点是它对通配符的粗略处理。 在这种情况下,您可以使用辅助扩展来编写可以执行所需操作的模式规则: all: mkdir $(PDF_FILES) .SECONDEXPANSION: %.pdf: $$(subst /output/,/, $$(subst .pdf,.tex, $$@)) @echo buiding $@ from $< One of the fundamental shortcomings of Make is its crude handling of wi ...
-
如何在webserver子目录上重写绝对资产路径(How to rewrite absolute assets paths on webserver subdirectory)[2022-07-17]
如果所有内容都在subdirectory ,您可以使用简单的重写 RewriteEngine on # don't rewrite, if it is already rewritten RewriteCond %{REQUEST_URI} !^/subdirectory # only rewrite requests for files below /assets RewriteCond %{REQUEST_URI} ^/assets/ RewriteRule ^.*$ /subdirectory/$0 ... -
我要离开这个寻找失落的灵魂,他们找到了这个悬而未决的问题。 您需要将include更改为** / *以包含指定目录中的所有内容。 这是你唯一需要改变的地方。 I'm leaving this for lost souls who found this unanswered question. You need to change the include to **/* to include everything in the specified directory. That is the only thi ...
-
一个解决方案是将所有CSS文件(IE浏览器除外)合并到一个文件中。 然后根据需要包含IE特定文件。 无论如何你都要这样做,因为20个http连接(每次最多2个)需要永远。 After scouring the web for what seemed like ages and not finding anything, I finally decided to come up with my own way. I anticipate using this again in future projects, ...