• ubuntu16.04 Cmake学习二


    本节主要总结编译程序的时候使用了第三方库的情况,以调用开源opencv-2.4.9为例子,具体安装详见http://www.cnblogs.com/xsfmg/p/5900420.html

    工程文件目录创建

    /home/bmi-zc/project:
    |—CMakeLists.txt 顶层CMakeLists
    |
    |—bin
    |
    |—include
    |    test.h
    |    ceshi.h     
    |
    |—lib
    |
    |—src
      |  CMakeLists.txt
      |  
      |—main
      |  CMakeLists.txt
      |  main.cpp
      |
      |—test
        CMakeLists.txt
        test1.cpp
        test2.cpp  
        ceshi.cpp

    程序清单

    test.h

        #ifndef INCLUDE_TEST_H
        #define INCLUDE_TEST_H
        #include <stdio.h>
        void t1();
        void t2();
        #endif /*INCLUDE_TEST_H*/
    

    ceshi.h

       #ifndef INCLUDE_CESHI_H
       #define INCLUDE_CESHI_H
       #include <cv.h>
       #include <highgui.h>
       int t3();
       #endif /*INCLUDE_CESHI_H*/
    

    test1.cpp

        #include "/home/bmi-zc/project/include/test.h"
        void t1()
        {
        printf("this is t1()
    ");	
        }
    

    test2.cpp

        #include "/home/bmi-zc/project/include/test.h"
        void t2()
        {
        printf("this is t2()
    ");
        }
    

    ceshi.cpp

        #include "/home/bmi-zc/project/include/ceshi.h"
        using namespace cv;
        int t3()
        {
        Mat image;
        image = imread("/home/bmi-zc/project/test1.jpg", 1);
        if (!image.data)
        {
        printf("No image data
    ");
        return -1;
        }
        namedWindow("Display Image", CV_WINDOW_AUTOSIZE);
        imshow("Display Image", image);
        waitKey(0);
        return 0;
        }
    

    main.cpp

        #include "/home/bmi-zc/project/include/test.h"  
        int main()  
        {  
        t1();  
        t2();
        t3();  
        return 0;  
        }
    
    • 这一节里,因为opencv主要采用C++编写,所以我们的编译器变成了g++,在建立算法程序后缀需要用.cpp。
    • 这一节将头文件都统一整理在了test.h和ceshi.h,其中test.h里是c语言库标准头文件,ceshi.h里是我们要用到的opencv的头文件。

    CMakeLIsts.txt清单

    /home/bmi-zc/project/CMakeLists.txt

        cmake_minimum_required(VERSION 3.5)
        PROJECT(TEST)
        ADD_SUBDIRECTORY(src)
    

    /home/bmi-zc/project/src/CMakeLists.txt

        ADD_SUBDIRECTORY(main)
        ADD_SUBDIRECTORY(test)
    

    /home/bmi-zc/project/src/test/CMakeLists.txt

        SET(CMAKE_CXX_COMPTLER g++)  //编译器采用g++
    	SET(SRC_LIST test1.cpp test2.cpp ceshi.cpp)
    	INCLUDE_DIRECTORIES(/home/bmi-zc/opencv-2.4.9/include/opencv)   //opencv头文件目录
    	INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/include)
    	SET(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)
    	ADD_LIBRARY(libtest STATIC ${SRC_LIST})
    

    /home/bmi-zc/project/src/main/CMakeLists.txt

        SET(SRC_LIST main.cpp)
        INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/include)
    	FIND_PACKAGE(OpenCV REQUIRED)
    	SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
    	ADD_EXECUTABLE(main ${SRC_LIST})
    	link_directories(${PROJECT_SOURCE_DIR}/lib)
    	TARGET_LINK_LIBRARIES(main ${OpenCV_LIBS})
    	TARGET_LINK_LIBRARIES(main libtest)
    
    • 重点介绍find_package命令

      find_package可以被用来在系统中自动查找配置构建工程所需的程序库。CMake自带的模块文件里有大半是对各种常见开源库的find_package支持。

    编译&运行

    • 回到工程根目录,/home/bmi-zc/project
      cmake .
      make

    • 进入bin文件夹,执行main可执行文件
      cd bin
      ./main

    分享推送

    比较优秀的博文:

    1. CMake使用进阶,作者linghutf,写于2016年6月。
      http://linghutf.github.io/2016/06/16/cmake/
  • 相关阅读:
    ps | grep排除grep这个进程
    树莓派3B安装LEDE
    从路由器镜像中提取uImage头信息
    提取路由器固件中的squashfs
    javascript监听按键
    linux 英汉词典程序shell+postgresql版
    树莓派(centos7)安装mysql
    在线视频下载利器——youtube-dl
    使用curl自动签到百度贴吧
    极路由hc5661安装tcpdump
  • 原文地址:https://www.cnblogs.com/xsfmg/p/5908064.html
Copyright © 2020-2023  润新知