[英]TCHAR and unresolved external symbol (LNK2019) error in C++ program?
My project has two cpp files and one header file. 我的项目有两个cpp文件和一个头文件。 One cpp file contains the implementation of a single class and its declaration is in the header file.
一个cpp文件包含单个类的实现,其声明在头文件中。 The other cpp file is which contains the
int main
function. 另一个cpp文件包含
int main
函数。
One of the constructors of the class includes a TCHAR
parameter and it is cited as the unresolved function in LNK2019
linker error. 该类的一个构造函数包含一个
TCHAR
参数,它被引用为LNK2019
链接器错误中的未解析函数。
I'm using visual studio 2010 and I have set the Character set
option in the project properties to Not Set
so that I can choose between char
and wchar_t
using UNICODE
and _UNICODE
macros. 我正在使用visual studio 2010,我已将项目属性中的
Character set
选项Not Set
为Not Set
以便我可以使用UNICODE
和_UNICODE
宏在char
和wchar_t
之间进行选择。
Currently I have defined these in the beginning of my main cpp file and the header files are included after those two. 目前我已经在我的主cpp文件的开头定义了这些,并且头文件包含在这两个之后。 However, if I define these macros in the beginning of header file, the project compiles perfectly.
但是,如果我在头文件的开头定义这些宏,则项目编译完美。
Is there anyway to solve this issue ? 反正有没有解决这个问题? Or do I have to hard code the class to use either
char
or wchar_t
? 或者我必须硬编码类使用
char
或wchar_t
?
Thanks. 谢谢。
You are getting the linker error because you are defining the UNICODE
/ _UNICODE
macros inside of main.cpp but not in your class's implementation .cpp. 您收到链接器错误,因为您在main.cpp中定义了
UNICODE
/ _UNICODE
宏,但在类的实现.cpp中没有。 As such, when main.cpp includes your class's header file, it sees TCHAR
as wchar_t
, but when your implementation .cpp includes your header file, it sees TCHAR
as char
instead. 因此,当main.cpp包含您的类的头文件时,它将
TCHAR
视为wchar_t
,但是当您的实现.cpp包含您的头文件时,它会将TCHAR
视为char
。 You have a mismatch that causes the linker error because main.cpp calls a wchar_t
constructor that you have not actually implemented. 您有一个不匹配导致链接器错误,因为main.cpp调用您尚未实际实现的
wchar_t
构造函数。
You are supposed to look for the presence of the UNICODE
/ _UNICODE
macros, not actually define them manually. 您应该查找
UNICODE
/ _UNICODE
宏的存在,而不是实际手动定义它们。 Set the "Character Set" option to MBCS or Unicode so the IDE/compiler can manage the macros globally for the entire project as a whole for you. 将“字符集”选项设置为MBCS或Unicode,以便IDE /编译器可以为整个项目全局管理宏。 I don't know what setting it to "Not Set" actually does, but it is not what you actually need in this situation.
我不知道它设置为“未设置”实际上是什么,但它不是你在这种情况下实际需要的。
Macros are preprocessor constructs that apparently confuse you. 宏是预处理器构造,显然让您感到困惑。 Your code, even if you succeed will confuse others too.
你的代码,即使你成功也会让别人感到困惑。 The macros
UNICODE
and _UNICODE
have to be defined before TCHAR
is defined, defining them before TCHAR
is used is too late. 宏
UNICODE
和_UNICODE
有之前定义TCHAR
被定义,之前定义它们TCHAR
使用为时已晚。 Better leave it to your project settings. 最好将它留给您的项目设置。
Set the project's Character Set
to Unicode
, that is the character set what Windows uses internally anyway. 将项目的
Character Set
为Unicode
,即Windows内部使用的字符集。 Then that TCHAR
is wchar_t
and the API call macros Something()
always expand to SomethingW()
. 然后,
TCHAR
是wchar_t
,API调用宏Something()
总是扩展为SomethingW()
。 You can use char
and wchar_t
explicitly everywhere and the readers of code will exactly see what is what, no dim and unclear TCHAR
for them. 你可以在任何地方明确地使用
char
和wchar_t
,代码的读者将准确地看到它们是什么,没有昏暗和不清楚的TCHAR
。
Windows API functions SomethingA()
are just wrappers around SomethingW()
so using the A versions a lot is inefficient. Windows API函数
SomethingA()
只是包装了SomethingW()
因此使用A版本有很多是无效的。 If you ever need to call API function SomethingA()
, then do it explicitly, so everybody see that you had to do something inefficient. 如果你需要调用API函数
SomethingA()
,那么明确地做,所以每个人都看到你必须做一些效率低下的事情。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.