1. C++ “#”
“#”修饰的都是指令,在编译的时候使用。
2.# include
1.<\xxx> 与 “xxx”
- <>搜索C++自己的库 include 的目录
- “xxx”优先搜索工程目录找不到再去搜索C++库
2. “xxx”与 “xxx.h”
- “xxx” 标准C++库,头文件里不再有*.h,仍保留18个C库*.h,引用方法时候用到命名空间 using namespace std;
- “xxx.h”以前运行库,头文件都是 *.h
- 编译时系统会根据是 xxx 还是 xxx.h自动选择对应的函数库
- 不能混合使用 #inclde “iostream” #include “math.h” 错误
3.#define 与 #undef
- 定义常量:#define PI 3.14 C++ 用 const 代替
- 带参数的宏:C++用内联函数代替 inline
- #undef 删除由 #define 定义的宏
4.条件编译指令
常量表达式非0执行
#if 常量表达式1
//
#elif 常量表达式2
//
#else
//
#endif
//int i; i 就是标示符
#ifdef 标示符
//
#else
//
#endif
#ifndef 标示符
//
#else
//
5.defined 操作符
defined 不是指令,是操作符
//效果一样
#ifndef 标示符
#define 标示符
#endif
#if ! defined(标示符)
#define 标示符
#endif
6.编译处理例子
xcode 创建一个带头文件的 CPP 文件,自动生成
File1.h
#ifndef __TestCpp__File1__
#define __TestCpp__File1__
#include <stdio.h>
#endif
如果 File1.h 被重复包含,在没有
#ifndef TestCpp__File1
#define TestCpp__File1
#endif
会被重复编译,第二次编译会报错
7.编译过程
定义类 xx.h point.h、实现类xx.cpp point.cpp、程序出入类 pmain.cpp
windows环境
- point.cpp 编译 point.obj
- pmain.cpp 编译 pmain.obj
- point.obj 和 pmain.obj 合成 pmain.exe 可执行文件
point.cpp 文件改变只需编译point.cpp生成point.obj即可,如果point.h文件发生改变则引进 #inlude “point.h” 的 point.cpp、pmain.cpp都需要重新编译。