Treat wchar_t as built-in type不一致导致的链接错误

时间:2023-01-15 18:56:20

今天用VS2013新建了一个工程,生成时出现很多怪异的链接错误,比如:

error LNK2019: unresolved external symbol "__declspec(dllimport) public: static class MTString __cdecl MTString::fromWCharArray(wchar_t const *,int)"(__imp_?fromWCharArray@MTString@@SA?AV1@PB_WH@Z) referenced in function "public: void __thiscall MTIO::queryMtInDevices(void)" (?queryMtInDevices@MTIO@@QAEXXZ)

用Notepad2打开导入库xxx.lib,里面确实搜不到__imp_?fromWCharArray@MTString@@SA?AV1@PB_WH@Z,但可以搜到前半部分,网上一搜发现是将“wchar_t视为内置类型”配置不一致导致的。
VS2005之前项目的默认配置是不将wchar_t视为内置类型(即wchar_t 是视作unsigned short的),而从VS2005开始默认是将wchar_t视为内置类型。这导致用VS2005及之后的VS版本升级以前的dll工程时,会和新建的工程在配置上不一致,从而导致链接错误。

MSDN给出了详细的解释:https://msdn.microsoft.com/en-us/library/dh8che7s.aspx

To set this compiler option in the Visual Studio development environment:
1. Open the project's Property Pages dialog box. For details, see Working with Project Properties.
2. In the left pane, expand Configuration Properties, C/C++, and then select Language.
3. Modify the Treat wchar_t as Built-in Type property.