[英]Is it safe to mix UNICODE and non-UNICODE translation units?
I'm integrating a library which requires _UNICODE
and UNICODE
to be defined;我正在集成一个需要定义
_UNICODE
和UNICODE
的库; I can't set these definitions globally on my project for now, so I was wondering if I can safely build only the library code with these definitions.我现在不能在我的项目中全局设置这些定义,所以我想知道我是否可以安全地只用这些定义构建库代码。
I'm worried about ODR violations, but as far as I understand these definitions only impact macro definitions in the Windows and C runtime headers, so I would expect no ODR violations (as long as my own headers shared between translation units don't depend on UNICODE
), but is it really a guarantee?我担心 ODR 违规,但据我了解,这些定义仅影响 Windows 和 C 运行时标头中的宏定义,因此我预计不会出现 ODR 违规(只要我自己在翻译单元之间共享的标头不依赖于在
UNICODE
上),但这真的是保证吗?
Is it safe to mix translation units built with and without UNICODE
/ _UNICODE
?混合使用和不
UNICODE
/ _UNICODE
构建的翻译单元是否安全?
In other words, is it safe to compile these two files into the same binary:换句话说,将这两个文件编译成同一个二进制文件是否安全:
// a.cpp
#define _UNICODE
#define UNICODE
#include <tchar.h>
// maybe other windows header inclusion
// some code
// b.cpp
//#define _UNICODE
//#define UNICODE
#include <tchar.h>
// maybe other windows header inclusion
// some other code
If there is one inline function with _TEXT()
/ TCHAR
/... in different translation unit, one with preprocessor defined and one not (even if function is not used), then you got ODR -violation.如果在不同的翻译单元中有一个带有
_TEXT()
/ TCHAR
/... 的内联函数,一个定义了预处理器,一个没有定义(即使没有使用函数),那么你会得到ODR违规。
"is it safe?"
“安全吗?”
No.不。
Do you have currently ODR violations?
您目前是否存在 ODR 违规行为?
Not sure, maybe, maybe not.不确定,也许,也许不是。
Currently thattchar.h only #define
/ typedef
some aliases.目前那个tchar.h只有
#define
/ typedef
一些别名。 so doesn't do ODR-violation by itself.所以不会自行违反 ODR。
Can Microsoft change <tchar.h> or other windows headers in a way it might produce ODR violation for your code?
Microsoft 是否可以更改 <tchar.h> 或其他窗口标题,但可能会导致您的代码违反 ODR?
Yes.是的。
Would they do it?
他们会做吗?
Maybe, maybe not.也许,也许不是。
You can mix UNICODE and non-UNICODE compiles when there are no typedef
, for example if tchar.h
is not included.当没有
typedef
,您可以混合 UNICODE 和非 UNICODE 编译,例如,如果不包括tchar.h
。
Otherwise UNICODE
will just determine the macro translation of SetWindowText
, and other APIs and macros.否则
UNICODE
将仅确定SetWindowText
以及其他 API 和宏的宏转换。 It is possible to combine Unicode and ANSI, but it's confusing.可以将 Unicode 和 ANSI 结合使用,但这很令人困惑。
Note that in a single function, it is valid to call both SetWindowTextA(hwnd, "ansi")
and SetWindowTextW(hwnd, L"unicode")
.请注意,在单个函数中,可以同时调用
SetWindowTextA(hwnd, "ansi")
和SetWindowTextW(hwnd, L"unicode")
。 You can compile the project as Unicode and use the ANSI APIs for optional interface.您可以将项目编译为 Unicode,并将 ANSI API 用于可选接口。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.