• ubuntu下cmake自动化编译的一个例子


    一个CMakeLists.txt的例子
    参考:
    https://www.hahack.com/codes/cmake/
    https://blog.csdn.net/afei__/article/details/81201039

    项目目录

    -test

      -include

        -math.hpp

      -src

        -math.cpp

      main.cpp

      CMakeLists.txt

    math.hpp:

    1 double power(double base, int exponent);

    math.cpp:

     1 #include <math.hpp>
     2 
     3 double power(double base, int exponent)
     4 {
     5     int result = base;
     6     int i;
     7 
     8     if (exponent == 0) {
     9         return 1;
    10     }
    11     
    12     for(i = 1; i < exponent; ++i){
    13         result = result * base;
    14     }
    15 
    16     return result;
    17 }

    main.cpp:

     1 #include <opencv2/opencv.hpp>
     2 #include <opencv2/core/core.hpp>
     3 #include <opencv2/highgui/highgui.hpp>
     4 #include <iostream>
     5 #include <math.hpp>
     6 
     7 
     8 using namespace std;
     9 using namespace cv;
    10 
    11 int main(){
    12     cout<<CV_VERSION<<endl;
    13     Mat im=imread("test.jpg");
    14     if(!im.empty())
    15         cout<<"读取图片成功"<<endl;
    16     else
    17         cout<<"读取图片失败"<<endl;
    18     double res=power(2,3);
    19     cout<<res<<endl;
    20     return 0;
    21 }

    CMakeLists.txt:

    #指定cmake 版本
    CMAKE_MINIMUM_REQUIRED(VERSION 3.2)
    #指定项目名称,一般和项目的文件夹名称对应
    PROJECT(test)
    SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/../bin) 
    #设置可执行文件的输出目录
    SET(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/../lib) 
    #上面的语句能设置库文件的输出目录
    
    #头文件目录
    INCLUDE_DIRECTORIES(include /usr/local/include/)
    #等价的设置方式
    #set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -I$/usr/local/include")
    
    #链接库路径
    LINK_DIRECTORIES(/usr/local/lib/)
    #等价的设置方式
    #set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -L$/usr/local/lib")
    
    #查找到指定的预编译库,并将它的路径存储在变量中
    FIND_LIBRARY(OPENCV_LIB_PATH opencv_core)
    MESSAGE(${OPENCV_LIB_PATH})#/usr/local/lib/libopencv_core.so
    
    #源文件目录
    AUX_SOURCE_DIRECTORY(. SRC_LIST)
    #也可以这样匹配
    #FILE(GLOB SRC_LIST "*.cpp" "src/*.cpp")
    
    #添加子目录,子目录里有另一CMakeLists.txt待编译
    #ADD_SUBDIRECTORY(src)
    
    #生成链接库,可以指定SHARED,STATIC参数
    #ADD_LIBRARY(math STATIC math.cpp)
    ADD_LIBRARY(math SHARED src/math.cpp)
    
    #设置一些变量SET,LIST(APPEND/REMOVE_ITEM)
    #SET(SRC_LIST main.cpp)
    #LIST(APPEND SRC_LIST src/math.cpp)
    #LIST(REMOVE_ITEM SRC_LIST src/math.cpp)
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11  -g")
    SET(OPENCV_LIBS opencv_highgui opencv_imgcodecs opencv_core)
    
    #添加要编译的可执行文件
    ADD_EXECUTABLE(main ${SRC_LIST})
    
    #添加可执行文件所需要链接的库
    TARGET_LINK_LIBRARIES(main ${OPENCV_LIBS} math)
  • 相关阅读:
    20174303刘金晖 Exp 8 Web基础
    2019-2020-2 网络对抗技术 20174303刘金晖 Exp7 网络欺诈防范
    20174303刘金晖 Exp6 MSF基础应用
    20174303刘金晖 Exp5 信息搜集与漏洞扫描
    20174303刘金晖 Exp4 恶意代码分析
    2019-2020-2 20174303刘金晖 Exp3 免杀原理与实践
    2019-2020-2 20174303刘金晖 Exp2 后门原理与实践
    2019-2020-2 20174303刘金晖 Exp1 PC平台逆向破解
    20174307周俊锴《网络对抗技术》Exp9 Web安全基础
    20174307周俊锴Exp8 Web基础
  • 原文地址:https://www.cnblogs.com/buyizhiyou/p/10757476.html
Copyright © 2020-2023  润新知