【linux/c】cmake一步步来

说在前面

先安装

sudo apt-get install cmake

简单的

  • 编写CMakeLists.txt
    # 使用#来注释
    #最低版本要求
    cmake_minimum_required (VERSION 2.6)
    # 项目名称
    project (Tutorial)
    # 将tutorial.cpp编译成名为Tutotial的可执行文件
    add_executable(Tutorial tutorial.cpp)
    
  • 编写tutorial.cpp
    // A simple program that computes the square root of a number
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    int main (int argc, char *argv[])
    {
      if (argc < 2)
        {
        fprintf(stdout,"Usage: %s number\n",argv[0]);
        return 1;
        }
      double inputValue = atof(argv[1]);
      double outputValue = sqrt(inputValue);
      fprintf(stdout,"The square root of %g is %g\n",
              inputValue, outputValue);
      return 0;
    }
    
  • 尝试一下
    cmake .
    make
    
    xx@xx:/mnt/h/opencv/project$ cmake .
    -- The C compiler identification is GNU 7.4.0
    -- The CXX compiler identification is GNU 7.4.0
    -- Check for working C compiler: /usr/bin/cc
    -- Check for working C compiler: /usr/bin/cc -- works
    -- Detecting C compiler ABI info
    -- Detecting C compiler ABI info - done
    -- Detecting C compile features
    -- Detecting C compile features - done
    -- Check for working CXX compiler: /usr/bin/c++
    -- Check for working CXX compiler: /usr/bin/c++ -- works
    -- Detecting CXX compiler ABI info
    -- Detecting CXX compiler ABI info - done
    -- Detecting CXX compile features
    -- Detecting CXX compile features - done
    -- Configuring done
    -- Generating done
    -- Build files have been written to: /mnt/h/opencv/slam/project
    xx@xx:/mnt/h/opencv/project$ make
    Scanning dependencies of target Tutorial
    [ 50%] Building CXX object CMakeFiles/Tutorial.dir/tutorial.cpp.o
    [100%] Linking CXX executable Tutorial
    [100%] Built target Tutorial
    xx@xx:/mnt/h/opencv/project$ ls
    CMakeCache.txt  CMakeFiles  CMakeLists.txt  Makefile  Tutorial  cmake_install.cmake  tutorial.cpp
    xx@xx:/mnt/h/opencv/project$ ./Tutorial  5
    The square root of 5 is 2.23607
    

复杂一点点

  • 目录结构

    include文件夹中test.h中是TEST类的定义,而src文件夹中src.cpp是TEST类的实现
  • 编写 ./src/CMakeLists.txt
    #查找当前目录下所有源文件并记录文件名到DIR_LIB_SRCS
    aux_source_directory(. DIR_LIB_SRCS)
    #由这些源文件生成名为mytest的链接库
    add_library( mytest ${DIR_LIB_SRCS})
    
  • 编写 ./CMakeLists.txt
    cmake_minimum_required (VERSION 2.6)
    project (Tutorial)
    #包含头文件目录
    include_directories( ${PROJECT_SOURCE_DIR}/include )
    #添加src子目录,src下的源文件与CMakeLists也会被处理
    add_subdirectory( src )
    
    add_executable(Tutorial.out tutorial.cpp)
    #链接mytest库
    target_link_libraries(Tutorial.out mytest)
    
  • 编写 ./include/test.h
    #ifndef _TEST_H
    #define _TEST_H
    class TEST{
    private:
    	int val;
    	int res;
    
    public:
    	TEST(int a, int b);
    	int get_val();
    };
    #endif
    
  • 编写 ./src/test.cpp
    注意:这里的包含目录为"test.h",而不是"include/test.h",或者其他
    #include "test.h"
    
    TEST::TEST(int a, int b)
    {
    	this->val = a;
    	this->res = b;
    }
    
    int TEST::get_val()
    {
    	return this->val;
    }
    
  • 编写 ./tutorial.cpp
    注意:这里的包含目录为"test.h",而不是"include/test.h",或者其他
    // A simple program that computes the square root of a number
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    #include "test.h"
    int main (int argc, char *argv[])
    {
      if (argc < 2)
        {
        fprintf(stdout,"Usage: %s number\n",argv[0]);
        return 1;
        }
      double inputValue = atof(argv[1]);
      double outputValue = sqrt(inputValue);
      fprintf(stdout,"The square root of %g is %g\n",
              inputValue, outputValue);
    
      TEST testa(1,0);
      fprintf(stdout,"Test val is %d\n", testa.get_val());
    
      return 0;
    }
    

再复杂一点

  • find_package()
    find_package(<package> [version] [EXACT] [QUIET] [MODULE]
                 [REQUIRED] [[COMPONENTS] [components...]]
                 [OPTIONAL_COMPONENTS components...]
                 [NO_POLICY_SCOPE])
    
  • 文件目录
    多了张图片,用于测试OpenCV
    在这里插入图片描述
  • 以使用OpenCV为例,编写 ./CMakeLists.txt
    cmake_minimum_required (VERSION 2.6)
    project (Tutorial)
    
    # 添加c++ 11标准支持
    set( CMAKE_CXX_FLAGS "-std=c++11" )
    # 寻找OpenCV库
    find_package( OpenCV 4 REQUIRED )
    # 添加头文件
    include_directories( ${OpenCV_INCLUDE_DIRS} )
    
    include_directories( ${PROJECT_SOURCE_DIR}/include )
    add_subdirectory( src )
    add_executable(Tutorial tutorial.cpp)
    target_link_libraries(Tutorial mytest)
    #链接OpenCV库
    target_link_libraries(Tutorial ${OpenCV_LIBS} )
    
  • 编写./tutorial.cpp
    // A simple program that computes the square root of a number
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <opencv2/core/core.hpp>
    #include <opencv2/highgui/highgui.hpp>
    
    #include "test.h"
    
    int main (int argc, char *argv[])
    {
      if (argc < 2)
        {
        fprintf(stdout,"Usage: %s number\n",argv[0]);
        return 1;
        }
      double inputValue = atof(argv[1]);
      double outputValue = sqrt(inputValue);
      fprintf(stdout,"The square root of %g is %g\n",
              inputValue, outputValue);
    
      TEST testa(1,0);
      fprintf(stdout,"Test val is %d\n", testa.get_val());
    
      cv::Mat image = cv::imread("./ubuntu.png");
    
      //cv::namedWindow("image");
      cv::imshow("image", image);
    
      cv::waitKey(0);
    
      return 0;
    }
    

未完待续…

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值