CMake使用教程

https://blog.csdn.net/dabenxiong666/article/details/53998998

https://www.hahack.com/codes/cmake/


 

编写的测试的文件目录如下:

cmaketest
    |
    +--- MakeLists.txt
    |
    +--- config.h.in
    |
    +--- main.cpp
    |
    +--- math/
          |
          +--- MakeLists.txt
          |
          +--- MathFunction.cpp
          |
          +--- MathFunction.h
            

下面为每个文件中的内容,函数的功能是分别利用自定义的库和标准库实现求一个数的幂次方

1、根目录中的MakeLists.txt

# CMake 最低版本号要求
cmake_minimum_required (VERSION 2.8)
# 项目信息
project (Demo5)

#加入一个配置头文件,用于处理CMake对源码的设置
configure_file (
   	"${PROJECT_SOURCE_DIR}/config.h.in"
	"${PROJECT_BINARY_DIR}/config.h"
)

#是否使用自己的MathFunction库
option (USE_MYMATH "Use provided math implementation" ON)

#是否加入MathFunction库
if (USE_MYMATH)
	include_directories ("${PROJECT_SOURCE_DIR}/math")
	add_subdirectory (math)
	set (EXTRA_LIBS ${EXTRA_LIBS} MathFunction)
endif (USE_MYMATH)

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

#添加math子目录
#add_subdirectory(math)

#制定生成目标
add_executable(Demo5 ${DIR_SRCS})

#添加查找头文件
#include_directories("${PROJECT_SOURCE_DIR}/math")

#添加连接库
target_link_libraries(Demo5 ${EXTRA_LIBS})

# 指定安装路径
install (TARGETS Demo5 DESTINATION bin)
install (FILES "${PROJECT_BINARY_DIR}/config.h" DESTINATION include)

# 启用测试
enable_testing()

# 测试 5 的平方
add_test (test_5_2 Demo5 5 2)
set_tests_properties (test_5_2
 PROPERTIES PASS_REGULAR_EXPRESSION "is 100")


2、根目录中的config.h.in,用来配置代码中的配置选项

#cmakedefine USE_MYMATH

3、根目录中的main.cpp

#include <stdio.h>
#include <stdlib.h>
#include "config.h"

#ifdef USE_MYMATH
#include "MathFunction.h"
#else
#include <math.h>
#endif


int main(int argc, char *argv[])
{
    if (argc < 3){
        printf("Usage: %s base exponent \n", argv[0]);
        return 1;
    }
    double base = atof(argv[1]);
    int exponent = atoi(argv[2]);
     
     #ifdef USE_MYMATH
    double result = power(base, exponent);
    printf(" use my math %g ^ %d is %g\n", base, exponent, result);
     #else
    double result = pow(base, exponent);
    printf(" use standard math %g ^ %d is %g\n", base, exponent, result);
	#endif

    return 0;
}

4、math目录中的MakeLists.txt

# 查找当前目录下的所有源文件
# 并将名称保存到 DIR_LIB_SRCS 变量
aux_source_directory(. DIR_LIB_SRCS)
# 生成链接库
add_library (MathFunction ${DIR_LIB_SRCS})

#指定MathFunctions库的安装路径
install (TARGETS MathFunction DESTINATION bin)
install (FILES MathFunction.h DESTINATION include)

5、math目录中的MathFunction.cpp

/**
 * power - Calculate the power of number.
 * @param base: Base value.
 * @param exponent: Exponent value.
 *
 * @return base raised to the power exponent.
 */
#include "MathFunction.h"

double power(double base, int exponent)
{
    int result = base;
    int i;
    
    if (exponent == 0) {
        return 1;
    }
    
    for(i = 1; i < exponent; ++i){
        result = result * base;
    }
    return result;
}

6、math目录中的MathFunction.h

#ifndef _MATHFUNCTION_H_
#define _MATHFUNCTION_H_
double power(double base, int exponent);
#endif

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值