【问题标题】:DLL include unordered_map is not compiling with visual studio compilerDLL 包含 unordered_map 未使用 Visual Studio 编译器进行编译
【发布时间】:2013-04-22 13:51:45
【问题描述】:

我正在尝试使用 MinGW 编译 DLL,并从使用 Visual Studio 编译器编译的可执行文件中使用它。

DLL中的一个源文件正在使用hash_map,它可以用MinGW成功编译。

当我将 hash_map<> 更改为 std::tr1::unordered_map<> 并将 #include <tr1/unordered_map> 添加到我的代码中时,它正在为 Visual Studio 编译器成功编译。(How can I force MinGW to use tr1 namespace?)

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

My 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

【问题讨论】:

  • 所有这些容器都应该是 Dll 实现的一部分,而不是接口。在这种情况下,它们的标头应仅包含在 .cpp 文件中,并且不应影响使用其他编译器构建的 Dll 客户端。如果您尝试将模板化容器包含到 Dll 接口中,这是错误的,即使对于相同的编译器也是如此。
  • 您实际上是在尝试#include tr /unordered_map 还是您的问题中的拼写错误?

标签: c++ compiler-construction hashmap mingw unordered-map


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-12
    • 1970-01-01
    • 1970-01-01
    • 2012-11-29
    • 1970-01-01
    • 2014-05-26
    相关资源
    最近更新 更多