在CMake中自定义宏 add_definitions(-DDEBUG)

发布时间:2024年01月13日
heheda@linux:~/Linux/loveDBTeacher-v6$ tree
.
├── CMakeLists.txt
└── test.c

0 directories, 2 files
heheda@linux:~/Linux/loveDBTeacher-v6$
  • test.c
#include <stdio.h>
#define NUMBER 3

int main() {
    int a = 10;
#ifdef DEBUG
    printf("我是一个程序猿,我不会爬树...\n");
#endif
    for(int i=0;i<NUMBER;i++) {
        printf("Hello,I am Heheda\n");
    }
    return 0;
}
  • CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
project(MyProject)
add_definitions(-DDEBUG)
add_executable(app test.c)
执行命令:
1.mkdir build
2.cd build
3.cmake ..
4.make
5./app

执行结果:
heheda@linux:~/Linux/loveDBTeacher-v6$ mkdir build
heheda@linux:~/Linux/loveDBTeacher-v6$ cd build
heheda@linux:~/Linux/loveDBTeacher-v6/build$ cmake ..
-- The C compiler identification is GNU 7.5.0
-- The CXX compiler identification is GNU 7.5.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/heheda/Linux/loveDBTeacher-v6/build
heheda@linux:~/Linux/loveDBTeacher-v6/build$ make
Scanning dependencies of target app
[ 50%] Building C object CMakeFiles/app.dir/test.c.o
[100%] Linking C executable app
[100%] Built target app
heheda@linux:~/Linux/loveDBTeacher-v6/build$ ./app
我是一个程序猿,我不会爬树...
Hello,I am Heheda
Hello,I am Heheda
Hello,I am Heheda
heheda@linux:~/Linux/loveDBTeacher-v6/build$

文章来源:https://blog.csdn.net/weixin_41987016/article/details/135573730
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。