专业的编程技术博客社区

网站首页 > 博客文章 正文

C++开发:常见的预设变量和宏,用途示例

baijin 2024-10-02 11:14:48 博客文章 5 ℃ 0 评论

在 C++ 中,除了标准预定义宏和编译器特定宏外,还有一些常见的预设变量和宏用于特定的编译环境和平台检测:

标准预定义宏

__FILE__:表示当前文件名的字符串字面量。

__LINE__:表示当前行号的整数。

__DATE__:表示编译日期的字符串字面量(格式如 "Sep 29 2024")。

__TIME__:表示编译时间的字符串字面量(格式如 "16:30:19")。

__TIMESTAMP__:表示源文件最后一次修改的日期和时间的字符串字面量。

__cplusplus:表示当前 C++ 标准的版本号。例如,199711L 表示 C++98,201103L 表示 C++11,201402L 表示 C++14,201703L 表示 C++17,202002L 表示 C++20。

__STDC__:如果编译器遵循 ANSI C 标准,则定义此宏。

__STDC_HOSTED__:如果编译环境是一个完整的宿主环境,则定义为 1,否则为 0。

__STDC_VERSION__:表示 C 标准版本号(如 C11 为 201112L)。

编译器特定宏(扩展)

GCC/Clang

__GNUC__:GCC 版本号。

__GNUG__:G++ 版本号。

__clang__:Clang 编译器标识。

__VERSION__:表示编译器版本信息的字符串。

__OPTIMIZE__:如果启用了优化选项,则定义此宏。

__unix__ / __unix:标识 Unix 系统。

__APPLE__:标识 Apple 平台。

__MACH__:标识 macOS 系统。

MSVC

_MSC_VER:微软编译器版本号。

_WIN32:标识 Windows 平台。

_WIN64:标识 64 位 Windows 平台。

_DEBUG:在调试模式下定义。

_MT:多线程编译时定义。

_DLL:使用 DLL 运行时库时定义。

平台和架构检测宏

__x86_64__ / _M_X64:标识 x86_64 架构。

__i386__ / _M_IX86:标识 x86 架构。

__arm__ / _M_ARM:标识 ARM 架构。

C++ 标准库相关宏

__cpp_exceptions:如果支持异常处理,则定义此宏。

__cpp_rtti:如果支持运行时类型识别(RTTI),则定义此宏。

条件编译使用的宏

可以通过 #define 和 #undef 自定义宏,用于条件编译。

这些宏和变量帮助开发者在不同的编译器和平台上编写可移植的代码,通过条件编译来适应不同的编译环境和配置。

示例用法:

以下是这些预处理宏常量的详细用法介绍:

__FILE__ 和 __LINE__

用途:用于调试和日志记录,帮助定位代码中的位置。

示例:

#include <iostream>
void logError(const char* message) {
	std::cout << "Error: " << message << " in file " << __FILE__ << " at line " << __LINE__ << std::endl;
}
int main() {
logError("Something went wrong");
return 0;
}

__DATE__ 和 __TIME__

用途:用于生成编译时信息,帮助识别编译的时间点。

示例:

#include <iostream>
int main() {
  std::cout << "Compiled on " << __DATE__ << " at " << __TIME__ << std::endl;
  return 0;
}

__TIMESTAMP__

用途:提供源文件最后修改的日期和时间,可以用于版本控制和调试。

注意:并非所有编译器都支持。

示例:

#include <iostream>
int main() {
  std::cout << "Last modified: " << __TIMESTAMP__ << std::endl;
  return 0;
}

__cplusplus

用途:用于检测当前 C++ 标准版本,以便根据版本调整代码。

示例:

#include <iostream>
int main() {
  std::cout << "C++ Standard version: " << __cplusplus << std::endl;
  return 0;
}

__STDC__

用途:用于检查编译器是否遵循 ANSI C 标准。

示例:

#include <stdio.h>

int main() {
#ifdef __STDC__
    printf("ANSI C compliant\n");
#else
    printf("Non-ANSI C compliant\n");
#endif
    return 0;
}

__STDC_HOSTED__

用途:用于检查编译环境是否是完整的宿主环境。

示例:

#include <stdio.h>

int main() {
#if __STDC_HOSTED__ == 1
    printf("Hosted environment\n");
#else
    printf("Freestanding environment\n");
#endif
    return 0;
}

__STDC_VERSION__

用途:用于检测当前 C 标准版本。

示例:

#include <stdio.h>
int main() {
#ifdef __STDC_VERSION__
	printf("C Standard version: %ld\n", __STDC_VERSION__);
#else
	printf("C89 or earlier\n");
#endif
  return 0;
}


__GNUC__ 和 __GNUG__

用途:用于检测 GCC 和 G++ 的版本号,以便根据不同的编译器版本调整代码。

示例:

#include <iostream>
int main() {
#ifdef __GNUC__
	std::cout << "GCC version: " << __GNUC__ << std::endl;
#endif
#ifdef __GNUG__
	std::cout << "G++ version: " << __GNUG__ << std::endl;
#endif
	return 0;
}

__clang__

用途:用于检测是否使用 Clang 编译器。

示例:

#include <iostream>
int main() {
#ifdef __clang__
	std::cout << "Compiled with Clang" << std::endl;
#else
	std::cout << "Not compiled with Clang" << std::endl;
#endif
	return 0;
}

__VERSION__

用途:用于获取编译器的版本信息。

示例:

#include <iostream>
int main() {
	std::cout << "Compiler version: " << __VERSION__ << std::endl;
	return 0;
}

__OPTIMIZE__

用途:用于检测是否启用了编译器优化选项。

示例:

#include <iostream>
int main() {
#ifdef __OPTIMIZE__
	std::cout << "Optimizations are enabled" << std::endl;
#else
	std::cout << "Optimizations are not enabled" << std::endl;
#endif
	return 0;
}

__unix__ / __unix

用途:用于检测 Unix 系统。

示例:

#include <iostream>
int main() {
#if defined(__unix__) || defined(__unix)
	std::cout << "Running on Unix" << std::endl;
#else
	std::cout << "Not running on Unix" << std::endl;
#endif
	return 0;
}

__APPLE__

用途:用于检测 Apple 平台。

示例:

#include <iostream>
int main() {
#ifdef __APPLE__
	std::cout << "Running on Apple platform" << std::endl;
#else
	std::cout << "Not running on Apple platform" << std::endl;
#endif
	return 0;
}

__MACH__

用途:用于检测 macOS 系统。

示例:

#include <iostream>
int main() {
#if defined(__APPLE__) && defined(__MACH__)
	std::cout << "Running on macOS" << std::endl;
#else
	std::cout << "Not running on macOS" << std::endl;
#endif
return 0;
}

_MSC_VER

用途:用于检测 Microsoft 编译器的版本号。

示例:

#include <iostream>
int main() {
#ifdef _MSC_VER
	std::cout << "Microsoft compiler version: " << _MSC_VER << std::endl;
#else
	std::cout << "Not using Microsoft compiler" << std::endl;
#endif
	return 0;
}

_WIN32

用途:用于检测 Windows 平台(包括 32 位和 64 位)。

示例:

#include <iostream>
int main() {
#ifdef _WIN32
	std::cout << "Running on Windows" << std::endl;
#else
	std::cout << "Not running on Windows" << std::endl;
#endif
	return 0;
}

_WIN64

用途:用于检测 64 位 Windows 平台。

示例:

#include <iostream>
int main() {
#ifdef _WIN64
	std::cout << "Running on 64-bit Windows" << std::endl;
#else
	std::cout << "Not running on 64-bit Windows" << std::endl;
#endif
	return 0;
}

_DEBUG

用途:在调试模式下编译时定义,用于启用调试相关的代码。

示例:

#include <iostream>
int main() {
#ifdef _DEBUG
	std::cout << "Debug mode is enabled" << std::endl;
#else
	std::cout << "Debug mode is not enabled" << std::endl;
#endif
	return 0;
}

_MT

用途:用于检测多线程编译。

示例:

#include <iostream>
int main() {
#ifdef _MT
	std::cout << "Compiled with multithreading support" << std::endl;
#else
	std::cout << "Compiled without multithreading support" << std::endl;
#endif
	return 0;
}

_DLL

用途:用于检测是否使用 DLL 运行时库。

示例:

#include <iostream>
int main() {
#ifdef _DLL
	std::cout << "Using DLL runtime library" << std::endl;
#else
	std::cout << "Not using DLL runtime library" << std::endl;
#endif
	return 0;
}

__x86_64__ / _M_X64

用途:用于检测 x86_64 架构。

示例:

#include <iostream>
int main() {
#if defined(__x86_64__) || defined(_M_X64)
	std::cout << "Running on x86_64 architecture" << std::endl;
#else
	std::cout << "Not running on x86_64 architecture" << std::endl;
#endif
	return 0;
}

__i386__ / _M_IX86

用途:用于检测 x86 架构(32 位)。

示例:

#include <iostream>
int main() {
#if defined(__i386__) || defined(_M_IX86)
 std::cout << "Running on x86 architecture" << std::endl;
#else
 std::cout << "Not running on x86 architecture" << std::endl;
#endif
 return 0;
}

__arm__ / _M_ARM

用途:用于检测 ARM 架构。

示例:

#include <iostream>
int main() {
#if defined(__arm__) || defined(_M_ARM)
 std::cout << "Running on ARM architecture" << std::endl;
#else
 std::cout << "Not running on ARM architecture" << std::endl;
#endif
 return 0;
}

__cpp_exceptions

用途:用于检测编译器是否支持异常处理。

说明:如果异常处理被启用,编译器会定义此宏,通常其值为启用该特性的年份。

示例:

#include <iostream>
int main() {
#ifdef __cpp_exceptions
	std::cout << "Exceptions are supported" << std::endl;
#else
	std::cout << "Exceptions are not supported" << std::endl;
#endif
	return 0;
}

__cpp_rtti

用途:用于检测编译器是否支持运行时类型识别(RTTI)。

说明:如果 RTTI 被启用,编译器会定义此宏,通常其值为启用该特性的年份。

示例:

#include <iostream>
#include <typeinfo>
class Base {};
class Derived : public Base {};

int main() {
#ifdef __cpp_rtti
	Base* b = new Derived();
	std::cout << "RTTI is supported: " << typeid(*b).name() << std::endl;
#else
	std::cout << "RTTI is not supported" << std::endl;
#endif
	return 0;
}

这两个宏可以帮助开发者在编写跨平台或需要特定功能

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表