cmake简单入门1

github: https://github.com/ZhouHanyu18/cmake

参考链接:https://blog.csdn.net/lwljava/article/details/38517193?utm_medium=distribute.pc_relevant.none-task-blog-OPENSEARCH-1&depth_1-utm_source=distribute.pc_relevant.none-task-blog-OPENSEARCH-1

一、多目录编译demo1

1. 文件结构

2. main.cpp

#include <stdio.h>
#include <stdlib.h>
#include "../tools/math.h"

int main(int argc, char* argv[]) {
    if (argc < 2) {
        printf("please input %s number: \n", argv[0]);
        return 1;
    }   
    
    double input = atof(argv[1]);
    int n = atoi(argv[2]);
    double out = pow(input, n); 
    
    printf("pow(%f, %d) is %f\n", input, n, out); 
    return 0;
}

3. tools/

    math.h

#ifndef MyMath
#define MyMath

double pow(double a, int n);

#endif  // MyMath

    math .cpp

#include "math.h"

double pow(double a, int n) {
    double ret = 1;
    for (int i = 0; i < n; ++i) {
        ret *= a;
    }
    return ret;
}

4. tools/CMakeLists.txt

# 查找当前目录下的所有源文件, 并将名称保存到 DIR_LIB_SRCS 变量
aux_source_directory(. DIR_LIB_SRCS)

# 生成链接库
add_library (Tools ${DIR_LIB_SRCS})

5. CMakeLists.txt

# cmake 最低版本要求
cmake_minimum_required (VERSION 2.8)
# 项目名称
project (Main)
# 查找当前目录下的所有源文件, 并将名称保存到 DIR_SRCS 变量
aux_source_directory(main/ DIR_SRCS)
# 指定生成目标
add_executable(main ${DIR_SRCS})

# 添加 math 子目录
add_subdirectory(tools)
# 添加链接库
target_link_libraries(main Tools)

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值