DLL包含unordered_map不能用visual studio编译器编译

DLL include unordered_map is not compiling with visual studio compiler

本文关键字:visual studio 编译器 编译 不能 包含 unordered map DLL      更新时间:2023-10-16

我正试图用MinGW编译一个DLL,并从visual studio编译器编译的可执行文件中使用它。

DLL中的一个源文件使用了hash_map<>,可以用MinGW成功编译。

当我将hash_map<>更改为std::tr1::unordered_map<>并将#include <tr1/unordered_map>添加到我的代码时,它成功地编译了visual studio编译器。(如何强制MinGW使用tr1命名空间?)

但是当我试图用MinGW作为DLL编译代码并从visual studio编译器编译的可执行文件中使用它时,它会给出错误:无法打开包含文件'tr/unordered_map'

我的DLL必须同时兼容cl和MinGW吗?

编辑:我的编译命令如下:

g++ -shared -o stemming.dll stemming.cpp alphabet.cpp basic.cpp determinise.cpp fst.cpp hopcroft.cpp operators.cpp utf8.cpp -Wl,--output-def,stemming.def,--out-implib,libstemming.a
lib /machine:i386 /def:stemming.def
cl sfstMinGW.cpp SFST/stemming.lib

vc++试图打开一个头文件,但在包含路径中找不到它。VC使用INCLUDE环境变量来确定搜索头文件时要使用的路径。因为VC不使用tr/目录,所以它不会找到它。你需要同时为VC和g++提供include语句,并选择使用哪一个,如下所示。

#if defined(_MSC_VER)
# include <unordered_map>
#else
# include <tr/unordered_map>
#endif

您需要确保使用与DLL相同的unordered_map实现来编译应用程序。这意味着您需要更新包含路径以使用GCC的TR1版本,而不是MS的标准头文件实现。