#error/#if defined() && defined() /#elif /#else /#endif

本文介绍了 C/C++ 中 #error 预处理命令的使用方法及其语法结构,通过示例展示了如何利用 #error 在编译时输出自定义错误消息,并给出了一种检查编译器类型的应用场景。
#error命令是C/C++语言的 预处理命令之一,当预处理器预处理到#error命令时将停止编译并输出用户自定义的错误消息。
语法:
#error [用户自定义的错误消息]
注:上述语法成份中的方括号“[]”代表用户自定义的错误消息可以省略不写。
举例1:
#error Sorry,an error has occurred!
举例2:
#error
用法示例:
/*
*检查编译此源文件的编译器是不是C++编译器
*如果使用的是C语言编译器则执行#error命令
*如果使用的是 C++ 编译器则跳过#error命令
*/
#ifndef __cplusplus
#error 亲,您当前使用的不是C++编译器噢!
#endif
#include <stdio.h>
int main()
{
printf("Hello,World!");
return 0;
}
把下面的window代码修改成移植到linux代码#if defined (_MSC_VER) && (_MSC_VER >= 1000) #pragma once #endif #ifndef _HEADER_BASESYSTEMCONFIG #define _HEADER_BASESYSTEMCONFIG #if _MSC_VER >= 1400 # ifndef _CRT_SECURE_NO_DEPRECATE # define _CRT_SECURE_NO_DEPRECATE # endif // [11/8/2006 Tgame.Tang] //# ifndef _DEBUG //# ifndef _SECURE_SCL //# define _SECURE_SCL 0//在release下关闭STL的安全特性,提高效率 //# else //# if _SECURE_SCL != 0 //# error("_SECURE_SCL must be defined as 0 while compile no debug version,You must include stl after include this file") //# endif //# endif //# endif #endif #pragma warning(disable:4995) #pragma warning (disable:4251) //!系统平台 #if defined(_WIN32) // // Comment out the following block if you want to run on Windows 9x // or Windows NT 3.51. // # ifndef _WIN32_WINNT // // Necessary for TryEnterCriticalSection. // # define _WIN32_WINNT 0x0500 # endif #define WIN32_LEAN_AND_MEAN // 从 Windows 头中排除极少使用的资料 # include <windows.h> # include <winsock2.h> # pragma comment( lib, "wsock32.lib" ) # pragma comment( lib, "Ws2_32.lib" ) #elif (defined(__sun) && defined(__sparc)) || (defined(__hpux)) # include <inttypes.h> #else // // The ISO C99 standard specifies that in C++ implementations the // macros for minimum/maximum integer values should only be defined if // explicitly requested with __STDC_LIMIT_MACROS. // # ifndef __STDC_LIMIT_MACROS # define __STDC_LIMIT_MACROS # endif # include <stdint.h> #endif //!字节序 #if defined(__i386) || defined(_M_IX86) || defined (__x86_64) # define SERIAL_LITTLE_ENDIAN #elif defined(__sparc) || defined(__sparc__) || defined(__hppa) || defined(__ppc__) || defined(_ARCH_COM) # define SERIAL_BIG_ENDIAN #else # error "Unknown architecture" #endif //!STL #include <vector> #include <string> #include <cassert> #include <iostream> #include <sstream> #include <string> #include <map> #include <queue> #include <stack> #include <list> #ifdef _Foundation_Import_ # define _Foundation_Export_ __declspec( dllimport ) #else # define _Foundation_Export_ __declspec( dllexport ) //# pragma warning (disable:4251) #endif //#define _Disable_Vt_ #endif
最新发布
09-28
#ifndef _AES_H_ #define _AES_H_ #include <stdint.h> // #define the macros below to 1/0 to enable/disable the mode of operation. // // CBC enables AES encryption in CBC-mode of operation. // CTR enables encryption in counter-mode. // ECB enables the basic ECB 16-byte block algorithm. All can be enabled simultaneously. // The #ifndef-guard allows it to be configured before #include'ing or at compile time. #ifndef CBC #define CBC 1 #endif #ifndef ECB #define ECB 1 #endif #ifndef CTR #define CTR 1 #endif #define AES128 1 //#define AES192 1 //#define AES256 1 #define AES_BLOCKLEN 16 //Block length in bytes AES is 128b block only #if defined(AES256) && (AES256 == 1) #define AES_KEYLEN 32 #define AES_keyExpSize 240 #elif defined(AES192) && (AES192 == 1) #define AES_KEYLEN 24 #define AES_keyExpSize 208 #else #define AES_KEYLEN 16 // Key length in bytes #define AES_keyExpSize 176 #endif struct AES_ctx { uint8_t RoundKey[AES_keyExpSize]; #if (defined(CBC) && (CBC == 1)) || (defined(CTR) && (CTR == 1)) uint8_t Iv[AES_BLOCKLEN]; #endif }; void AES_init_ctx(struct AES_ctx* ctx, const uint8_t* key); #if defined(CBC) && (CBC == 1) void AES_init_ctx_iv(struct AES_ctx* ctx, const uint8_t* key, const uint8_t* iv); void AES_ctx_set_iv(struct AES_ctx* ctx, const uint8_t* iv); #endif #if defined(ECB) && (ECB == 1) // buffer size is exactly AES_BLOCKLEN bytes; // you need only AES_init_ctx as IV is not used in ECB // NB: ECB is considered insecure for most uses void AES_ECB_encrypt(struct AES_ctx* ctx, const uint8_t* buf); void AES_ECB_decrypt(struct AES_ctx* ctx, const uint8_t* buf); #endif // #if defined(ECB) && (ECB == !) #if defined(CBC) && (CBC == 1) // buffer size MUST be mutile of AES_BLOCKLEN; // Suggest https://en.wikipedia.org/wiki/Padding_(cryptography)#PKCS7 for padding scheme // NOTES: you need to set IV in ctx via AES_init_ctx_iv() or AES_ctx_set_iv() // no IV should ever be reused with the same key void AES_CBC_encrypt_buffer(struct AES_ctx* ctx, uint8_t* buf, uint32_t length); void AES_CBC_decrypt_buffer(struct AES_ctx* ctx, uint8_t* buf, uint32_t length); #endif // #if defined(CBC) && (CBC == 1) #if defined(CTR) && (CTR == 1) // Same function for encrypting as for decrypting. // IV is incremented for every block, and used after encryption as XOR-compliment for output // Suggesting https://en.wikipedia.org/wiki/Padding_(cryptography)#PKCS7 for padding scheme // NOTES: you need to set IV in ctx with AES_init_ctx_iv() or AES_ctx_set_iv() // no IV should ever be reused with the same key void AES_CTR_xcrypt_buffer(struct AES_ctx* ctx, uint8_t* buf, uint32_t length); #endif // #if defined(CTR) && (CTR == 1) #endif //_AES_H_
09-12
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值