zoukankan      html  css  js  c++  java
  • cmake 单个目录多个文件的情况

    参考:https://www.hahack.com/codes/cmake/#

    源文件一共有三个:main.cpp、MathFunctions.h、MathFunctions.cpp

    文件内容分别如下:

    main.cpp

     1 #include <stdio.h>
     2 #include <stdlib.h>
     3 #include "MathFunctions.h"
     4 
     5 int main(int argc, char *argv[])
     6 {
     7     if (argc < 3){
     8         printf("Usage: %s base exponent 
    ", argv[0]);
     9         return 1;
    10     }
    11     double base = atof(argv[1]);
    12     int exponent = atoi(argv[2]);
    13     double result = power(base, exponent);
    14     printf("%g ^ %d is %g
    ", base, exponent, result);
    15     return 0;
    16 }

    MathFunctions.h

    1 #ifndef POWER_H
    2 #define POWER_H
    3 
    4 extern double power(double base, int exponent);
    5 
    6 #endif

    MathFunctions.cpp

     1  /**
     2  * power - Calculate the power of number.
     3  * @param base: Base value.
     4  * @param exponent: Exponent value.
     5  *
     6  * @return base raised to the power exponent.
     7  */
     8 double power(double base, int exponent)
     9 {
    10     int result = base;
    11     int i;
    12 
    13     if (exponent == 0) {
    14         return 1;
    15     }
    16     
    17     for(i = 1; i < exponent; ++i){
    18         result = result * base;
    19     }
    20 
    21     return result;
    22 }

    CMakeLists.txt内容如下:

    # CMake 最低版本号要求
    cmake_minimum_required (VERSION 2.8)
    
    # 项目信息
    project (Demo2)
    
    # 查找目录下的所有源文件
    # 并将名称保存到 DIR_SRCS 变量
    aux_source_directory(. DIR_SRCS)
    
    # 指定生成目标
    add_executable(Demo ${DIR_SRCS})

    这里需要学习的是使用变量获取目录下的所有源文件:

    aux_source_directory(. DIR_SRCS)

    # 指定生成目标
    add_executable(Demo ${DIR_SRCS})

    编译及执行结果如下:

    wmz@ubuntu:~/Desktop/testcmake1$ cmake .
    -- The C compiler identification is GNU 5.4.0
    -- The CXX compiler identification is GNU 5.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: /home/wmz/Desktop/testcmake1
    wmz@ubuntu:~/Desktop/testcmake1$ make 
    Scanning dependencies of target Demo
    [ 33%] Building CXX object CMakeFiles/Demo.dir/MatnFunctions.cpp.o
    [ 66%] Building CXX object CMakeFiles/Demo.dir/main.cpp.o
    [100%] Linking CXX executable Demo
    [100%] Built target Demo
    wmz@ubuntu:~/Desktop/testcmake1$ ls
    CMakeCache.txt       CMakeLists.txt  Makefile
    CMakeFiles           Demo            MathFunctions.h
    cmake_install.cmake  main.cpp        MatnFunctions.cpp
    wmz@ubuntu:~/Desktop/testcmake1$ ./Demo 2 3
    2 ^ 3 is 8
  • 相关阅读:
    vue 对css样式进行修改
    vue之绑定数据+data、methods、computed、watch的基础用法
    vue安装 devtools失败的临时解决办法
    解决VMware Workstation 与 Device/Credential Guard 不兼容
    后渗透之meterpreter常用命令
    2020-7- 27
    PIL模块调整图片大小
    maven(一) maven到底是个啥玩意~
    noi前第十二场 题解
    noi前第十一场 题解
  • 原文地址:https://www.cnblogs.com/juluwangshier/p/11609381.html
Copyright © 2011-2022 走看看