Makefile在Ubuntu / Linux上用C ++编译OpenCV代码 [英] Makefile to Compile OpenCV Code in C++ on Ubuntu/Linux
问题描述
我正在使用学习OpenCV 书籍学习OpenCV。
编译代码时遇到的一个问题是我必须编写一个长命令来编译和获取可执行文件。
这是我使用的命令
g ++`pkg-config -cflags opencv` file_name.cpp -o output_file`pkg-config -libs opencv`
我没有专家,但我想我可以消除写作那个长命令使用 make 。
在此之前,我应该解释我的工作流程。我在我的主目录(〜/ opencv /
)中创建了一个名为opencv的目录。我正在阅读本书一节,并将示例或练习编码到该目录中的新cpp源代码文件中。
现在我想要做的就是,
$假设我在我的 opencv
中编写了一个名为 facesetect.cpp
的新文件,目录,如果我调用make这样 make facesetect
然后我要让我为我执行以下命令
g ++ `pkg-config --cflags opencv` facedetect.cpp -o facesetect`pkg-config --libs opencv`
,所以每当我创建一个名为abc.cpp的新文件,我将执行 make abc
,以便我可以运行
$ ./abc
在我的命令行测试我的abc.cpp
请给出make文件,这样我就可以节省每次输入long命令的麻烦。
PS:我有Google的帮助,并且发现这使用 CMake ,但我不明白这是什么。请同时解释如何对同一个任务使用 CMake 。
p>您可以在工作目录中创建一个名为 Makefile
的文件,如下所示:
CFLAGS =`pkg-config --cflags opencv`
LIBS =`pkg-config --libs opencv`
%:%.cpp
g ++ $(CFLAGS )$(LIBS)-o $ @ $<
那么您可以将此文件用于所有单文件程序。只需用要编译的文件的basename调用 make
即可。对 facedetect.cpp
这将是
make facesetect
这里有一些详细信息:
makefile是这样的:
target:dependecy1 dependenc2 ...
生成目标的命令
所以对于你的例子,你可以这样写:
facedetect:facedetect.cpp
g ++ $(CFLAGS)$(LIBS)-o facesetect facesetect.cpp
对于每个新示例,您现在可以创建一个新的目标。但您也可以使其更通用:
%:%.cpp
g ++ $(CFLAGS)$ )-o $ @ $<
这里%
匹配任何非空子字符串。自动变量 $ @
和 $
有关详情,请参阅 make文档。
I am learning OpenCV using Learning OpenCV book.
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.
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
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
at my command line to test my abc.cpp
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.
You can create a file called Makefile
in you working directory like this:
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
make facedetect
Here some more details:
The general format of a makefile is like this:
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.
这篇关于Makefile在Ubuntu / Linux上用C ++编译OpenCV代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!