5-4 条件编译

1. #ifndef 含义是 if not define,如果没有定义
    #ifdef 含义是 if define,如果已经定义
    #if 配合函数 defined() 使用,if defined(MACRO) == ifdef MACRO,如果已经定义了宏 MACRO
    三者要以 endif 进行结尾

// 头文件中必有的条件编译,以免一些头文件被重复引用
#ifndef CHAPTER5_INCLUDE_FACTORIAL_H_
#define CHAPTER5_INCLUDE_FACTORIAL_H_

unsigned int Factorial(unsigned int n);

unsigned int FactorialByIteration(unsigned int n);

#endif //CHAPTER5_INCLUDE_FACTORIAL_H_


// #if的用法
#ifdef DEBUG //  == if defined(DEBUG)

  puts(message);

#endif

2. 利用宏打印调试信息的 demo,没用 define DEBUG 的情况下,dump 函数的内容不会输出;定义了 DEBUG 的情况下,dump 函数内容才会输出

#define DEBUG  // 该宏定义存在的情况下,输出结果为 start! hello end
               // 该宏定义不存在的情况下,输出结果为 hello

void Dump(char *message){
    #ifdef DEBUG
       puts(message);
    #endif
}

int main(){
    Dump("start!");

    printf("hello\n");  

    Dump("end");
    return 0;
}

    如果不在代码中去增加或删除宏 DEBUG,可以在 CMakeLists.txt 中进行调整

cmake_minimum_required(VERSION 3.17)

get_filename_component(ProjectId ${CMAKE_CURRENT_SOURCE_DIR} NAME)
string(REPLACE " " "_" ProjectId ${ProjectId})
project(${ProjectId} C)

set(CMAKE_C_STANDARD 90)

include_directories("include")

file(GLOB files "${CMAKE_CURRENT_SOURCE_DIR}/*.c")
foreach(file ${files})
    get_filename_component(name ${file} NAME)
    add_executable(${name} ${file} src/factorial.c)
    target_compile_definitions(${name} PUBLIC DEBUG) # 添加该句即意味着定义了宏 DEBUG
endforeach()

4. 判断一段代码是在 C 语言下还是 C++ 语言下定义的,在 C++ 环境下运行的代码会多有 extern C{}; 这些东西;C 环境下就没有

#ifdef __cplusplus  // 如果是在 c++ 环境下,会查询是否定义了宏 __cplusplus 
extern "C" {
#endif

// ... code

// int Add(int left, int right);

#ifdef __cplusplus  // 相当于在代码外围添加了一层
};
#endif

5. 判断代码运行在哪个 C 语言版本下,可以利用 __STDC_VERSION__ 查看,顺便附赠一个在宏中使用 ifelse 的 demo(注意, __STDC_VERSION__ 需使用 mingw 编译器,msvc 编译器不支持)

int main() {

  printf("%d\n",__STDC_VERSION__);

#if __STDC_VERSION__ >= 201112
  puts("C11!!");

#elif __STDC_VERSION__ >= 199901
  puts("C99!!");

#else
  puts("maybe C90?");
#endif

  return 0;
}

6. 此外,还可以利用条件编译判断代码运行的平台,是在 Windows 环境还是 Linux 环境,或者 Mac 环境,从而调用他们系统本身的 API 进行使用

5-4 条件编译

上一篇:解决windows.h函数中无cin、cout的困难


下一篇:关于网页前端的html使用技巧1