[编程基础] C和C++内置宏说明


C和C++内置宏在代码调试、跨系统平台代码中会经常使用,本文记录说明一下。内置宏不需要调用头文件,可直接使用。在使用预定义的宏之间需要了解常用的条件编译指令,具体条件编译指令可见:
if、#else、#endif、#elif、#ifdef、#ifndef的区别和使用

1 内置的宏定义

这些宏在代码中可直接调用。

宏说明
__DATE__程序最后编译日期宏
__TIME__程序最后编译时间宏
__LINE__当前行数宏
__FILE__当前运行文件名宏
__FUNCTION__当前运行函数宏
__func__当前运行函数宏

示例代码

cout << "程序最后编译日期宏 " << __DATE__ << endl;
cout << "程序最后编译时间宏 " << __TIME__ << endl;
cout << "当前行数宏 " << __LINE__ << endl;
cout << "当前运行文件名宏 " << __FILE__ << endl;
cout << "当前运行函数宏 " << __FUNCTION__ << endl;
cout << "当前运行函数宏 " << __func__ << endl;

windows输出结果

程序最后编译日期宏 May  8 2020
程序最后编译时间宏 11:19:24
当前行数宏 12
当前运行文件名宏 c:\users\admin\desktop\test\define.cpp
当前运行函数宏 main
当前运行函数宏 main

linux输出结果

程序最后编译日期宏 May  8 2020
程序最后编译时间宏 11:19:00
当前行数宏 9
当前运行文件名宏 define.cpp
当前运行函数宏 main
当前运行函数宏 main

2 运行平台宏

这些宏主要是判断当前系统运行平台。

宏说明
WIN32、_WIN32、_WIN32_、WIN64、_WIN64、_WIN64_windows
ANDROID、_ANDROID_android
__linux__linux
__APPLE__、TARGET_OS_IPHONE、TARGET_IPHONE_SIMULATOR、TARGET_OS_MACios、mac

示例代码

	// windows
#if defined(WIN32) || defined(_WIN32) || defined(_WIN32_) || defined(WIN64) || defined(_WIN64) || defined(_WIN64_)
	cout << "hello windows" << endl;

	// android
#elif defined(ANDROID) || defined(_ANDROID_)
	cout << "hello android" << endl;

	// linux
#elif defined(__linux__)
	cout << "hello linux" << endl;

	// ios or mac
#elif defined(__APPLE__) || defined(TARGET_OS_IPHONE) || defined(TARGET_IPHONE_SIMULATOR) || defined(TARGET_OS_MAC)
	cout << "hello ios/mac" << endl;

	// other
#else
	cout << "hello unknown" << endl;
#endif

windows输出结果

hello windows

linux输出结果

hello linux

3 编译器宏

这些宏主要是判断当前程序的编译器类型。

宏说明
_MSC_VERvisual studio
__GNUC__gcc、g++
__SUNPRO_C、__SUNPRO_CCsun cc

示例代码

	// visual studio
#if defined(_MSC_VER)
	cout << "hello VC" << endl;

	// gcc/g++
#elif defined(__GNUC__)
	cout << "hello GCC / G++ " << endl;

	// SunCC
#elif defined(__SUNPRO_C)||defined(__SUNPRO_CC)
	cout << "hello SunCC" << endl;
#endif

windows输出结果

hello VC

linux输出结果

hello GCC / G++ 

4 调试类型宏

这些宏主要是判断当前程序的调试类型。

宏说明
_DEBUGdebug模式

示例代码

#if defined(_DEBUG)
	cout << "debug" << endl;
#else
	cout << "release" << endl;
#endif 

windows输出结果

debug

linux输出结果

release

5 代码

所有示例运行代码如下:

#include <iostream>

using namespace std;

int main()
{

	cout << "程序最后编译日期宏 " << __DATE__ << endl;
	cout << "程序最后编译时间宏 " << __TIME__ << endl;
	cout << "当前行数宏 " << __LINE__ << endl;
	cout << "当前运行文件名宏 " << __FILE__ << endl;
	cout << "当前运行函数宏 " << __FUNCTION__ << endl;
	cout << "当前运行函数宏 " << __func__ << endl;

	// 运行平台宏
	// windows
#if defined(WIN32) || defined(_WIN32) || defined(_WIN32_) || defined(WIN64) || defined(_WIN64) || defined(_WIN64_)
	cout << "hello windows" << endl;

	// android
#elif defined(ANDROID) || defined(_ANDROID_)
	cout << "hello android" << endl;

	// linux
#elif defined(__linux__)
	cout << "hello linux" << endl;

	// ios or mac
#elif defined(__APPLE__) || defined(TARGET_OS_IPHONE) || defined(TARGET_IPHONE_SIMULATOR) || defined(TARGET_OS_MAC)
	cout << "hello ios/mac" << endl;

	// other
#else
	cout << "hello unknown" << endl;
#endif

	// 编译器宏
	// visual studio
#if defined(_MSC_VER)
	cout << "hello VC" << endl;

	// gcc/g++
#elif defined(__GNUC__)
	cout << "hello GCC / G++ " << endl;

	// SunCC
#elif defined(__SUNPRO_C)||defined(__SUNPRO_CC)
	cout << "hello SunCC" << endl;
#endif

// 调试类型
#if defined(_DEBUG)
	cout << "debug" << endl;
#else
	cout << "release" << endl;
#endif 

	return 0;
}
posted @ 2020-05-09 21:09  落痕的寒假  阅读(27)  评论(0编辑  收藏  举报