I am learning OpenCV using Learning OpenCV book.
我正在学习OpenCV学习OpenCV的书。
One problem I am facing while compiling the code is that I have to write a long command to compile and get the executable.
在编译代码时,我面临的一个问题是,我必须编写一个长命令来编译和获取可执行文件。
This is the command I am using
这是我正在使用的命令。
g++ `pkg-config –cflags opencv` file_name.cpp -o output_file `pkg-config –libs opencv`
I am no Make expert but I think I can eliminate writing that long command using make. Before that I should explain my work flow. I have created a directory called opencv in my home directory (~/opencv/
). I am reading the book section by section and coding the examples or exercises into new cpp source code files in that directory. So I don't know the names of the files before hand.
我不是专家,但我想我可以消除使用Make的长命令。在此之前,我要解释一下我的工作流程。我在我的主目录中创建了一个名为opencv的目录(~/opencv/)。我正在逐节阅读书籍部分,并在该目录中将示例或练习编码到新的cpp源代码文件中。所以我之前不知道文件的名字。
Now what I want make to do is,
现在我要做的是,
Suppose I have coded a new file named facedetect.cpp
in my opencv
directory, and if I call make like this
假设我已经编写了一个名为facefind的新文件。在我的opencv目录中,如果我调用这样做。
make facedetect
then I want make to execute the following command for me
然后,我想让我执行下面的命令。
g++ `pkg-config --cflags opencv` facedetect.cpp -o facedetect `pkg-config --libs opencv`
so that whenever I make a new file named abc.cpp, I will execute make abc
so that I can run
所以每当我创建一个新的文件,命名为abc。cpp,我会执行make abc,这样我就可以运行了。
$ ./abc
at my command line to test my abc.cpp
在我的命令行测试我的abc。
Please give that make file so that I can save the frustration of typing that long command each time.
请给出这个文件,这样我就可以节省每次输入长命令的挫败感。
PS: I have Googled for help on this and found this on using CMake but I could not understand what that does. Kindly also explain how can I use CMake for the same task.
PS:我用谷歌搜索了一下,发现这是用CMake的,但我不明白这是怎么回事。还请解释我如何使用CMake来完成同样的任务。
3 个解决方案
#1
23
You can create a file called Makefile
in you working directory like this:
您可以在您的工作目录中创建一个名为Makefile的文件:
CFLAGS = `pkg-config --cflags opencv`
LIBS = `pkg-config --libs opencv`
% : %.cpp
g++ $(CFLAGS) $(LIBS) -o $@ $<
then you can use this file for all your single-file programms. Just call make
with the basename of the file you want to compile. For facedetect.cpp
that would be
然后,您可以为所有的单文件程序使用此文件。只需调用要编译的文件的basename。facedetect。cpp这将是
make facedetect
Here some more details:
这里一些更多的细节:
The general format of a makefile is like this:
makefile的一般格式如下:
target : dependecy1 dependenc2 ...
command that generates the target
So for your example you could write:
举个例子,你可以写:
facedetect : facedetect.cpp
g++ $(CFLAGS) $(LIBS) -o facedetect facedetect.cpp
For each new example you can now create a new target. But you can also make it more general:
对于每个新示例,您现在可以创建一个新的目标。但是你也可以让它更一般化:
% : %.cpp
g++ $(CFLAGS) $(LIBS) -o $@ $<
Here %
matches any nonempty substring. The automatic variables $@
and $<
substitute the names of the target file and the source file. For more information you can consult the make documentation.
这里%匹配任何非空的子字符串。自动变量$@和$ <替换目标文件和源文件的名称。有关更多信息,您可以查阅make文档。< p>
#2
5
GNU Make is pretty smart and the Makefile you need for this doesn't need to be as verbose as in the other answers given so far. Here is a simple Makefile which you can use to compile the OpenCV examples:
GNU Make非常聪明,您所需要的Makefile不需要像目前所给出的其他答案一样冗长。下面是一个简单的Makefile,可以用来编译OpenCV示例:
CPPFLAGS = $(shell pkg-config --cflags opencv)
LDLIBS = $(shell pkg-config --libs opencv)
That's it. The Makefile can be this short thanks to Make's implicit rules.
就是这样。Makefile可以是这个简短的感谢,因为它的隐含规则。
Then run make
as usual:
然后像往常一样跑步:
make facedetect
This assumes there is a facedetect.c
or facedetect.cpp
in the same directory.
这假设有一个face检测。c或facedetect。cpp在同一个目录中。
I recommend the following (free!) book if you want to learn Make: http://oreilly.com/catalog/make3/book/index.csp
如果你想学习,我推荐以下(免费)书籍:http://oreilly.com/catalog/make3/book/index.csp。
#3
0
Create a file named makefile
in your working directory that contains the following:
在工作目录中创建一个名为makefile的文件,该文件包含以下内容:
CFLAGS = $SHELL(pkg-config --cflags opencv)
LIBS = $SHELL(pkg-config --libs opencv)
facedetect : facedetect.cpp
g++ $(CFLAGS) $(LIBS) -o $@ $<
Then when you want to compile you just type:
当你想编译时,你只需输入:
$ make
(To answer your PS - note that CMake
is very different from make
- for now you should probaby just use make
.)
(回答你的PS -注意CMake和make是非常不同的,因为现在你应该使用make。)
#1
23
You can create a file called Makefile
in you working directory like this:
您可以在您的工作目录中创建一个名为Makefile的文件:
CFLAGS = `pkg-config --cflags opencv`
LIBS = `pkg-config --libs opencv`
% : %.cpp
g++ $(CFLAGS) $(LIBS) -o $@ $<
then you can use this file for all your single-file programms. Just call make
with the basename of the file you want to compile. For facedetect.cpp
that would be
然后,您可以为所有的单文件程序使用此文件。只需调用要编译的文件的basename。facedetect。cpp这将是
make facedetect
Here some more details:
这里一些更多的细节:
The general format of a makefile is like this:
makefile的一般格式如下:
target : dependecy1 dependenc2 ...
command that generates the target
So for your example you could write:
举个例子,你可以写:
facedetect : facedetect.cpp
g++ $(CFLAGS) $(LIBS) -o facedetect facedetect.cpp
For each new example you can now create a new target. But you can also make it more general:
对于每个新示例,您现在可以创建一个新的目标。但是你也可以让它更一般化:
% : %.cpp
g++ $(CFLAGS) $(LIBS) -o $@ $<
Here %
matches any nonempty substring. The automatic variables $@
and $<
substitute the names of the target file and the source file. For more information you can consult the make documentation.
这里%匹配任何非空的子字符串。自动变量$@和$ <替换目标文件和源文件的名称。有关更多信息,您可以查阅make文档。< p>
#2
5
GNU Make is pretty smart and the Makefile you need for this doesn't need to be as verbose as in the other answers given so far. Here is a simple Makefile which you can use to compile the OpenCV examples:
GNU Make非常聪明,您所需要的Makefile不需要像目前所给出的其他答案一样冗长。下面是一个简单的Makefile,可以用来编译OpenCV示例:
CPPFLAGS = $(shell pkg-config --cflags opencv)
LDLIBS = $(shell pkg-config --libs opencv)
That's it. The Makefile can be this short thanks to Make's implicit rules.
就是这样。Makefile可以是这个简短的感谢,因为它的隐含规则。
Then run make
as usual:
然后像往常一样跑步:
make facedetect
This assumes there is a facedetect.c
or facedetect.cpp
in the same directory.
这假设有一个face检测。c或facedetect。cpp在同一个目录中。
I recommend the following (free!) book if you want to learn Make: http://oreilly.com/catalog/make3/book/index.csp
如果你想学习,我推荐以下(免费)书籍:http://oreilly.com/catalog/make3/book/index.csp。
#3
0
Create a file named makefile
in your working directory that contains the following:
在工作目录中创建一个名为makefile的文件,该文件包含以下内容:
CFLAGS = $SHELL(pkg-config --cflags opencv)
LIBS = $SHELL(pkg-config --libs opencv)
facedetect : facedetect.cpp
g++ $(CFLAGS) $(LIBS) -o $@ $<
Then when you want to compile you just type:
当你想编译时,你只需输入:
$ make
(To answer your PS - note that CMake
is very different from make
- for now you should probaby just use make
.)
(回答你的PS -注意CMake和make是非常不同的,因为现在你应该使用make。)