Makefile to Compile OpenCV Code in C++ on Ubuntu/Linux

后端 未结 3 1033
半阙折子戏
半阙折子戏 2021-01-31 05:08

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 executabl

3条回答
  •  说谎
    说谎 (楼主)
    2021-01-31 05:39

    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.

提交回复
热议问题