【问题标题】:Is there a way to link static with STL only?有没有办法只将静态与 STL 联系起来?
【发布时间】:2016-06-14 05:42:25
【问题描述】:

我使用 Microsoft Visual Studio 2010 并使用 C++ 编写代码。

我的应用程序应该是轻量级的;因此,我使用来自 DDK 的 msvcrt.lib 将我的应用程序与存在于任何 Windows 中的 msvcrt.dll 链接起来。这个技巧让我不使用静态链接 (libcmt) 也不使用来自 MSVC Redistributables 的 DLL;所以它减少了我的可执行文件的大小。

但现在我需要在我的应用程序中使用 STL。有没有办法只为 STL 链接一些库?我从 DDK 拿了libcpmt.lib 并尝试与之链接;这就是我得到的:

1>main.obj : error LNK2001: unresolved external symbol "public: virtual char const * __thiscall std::exception::what(void)const " (?what@exception@std@@UBEPBDXZ)
1>main.obj : error LNK2001: unresolved external symbol "public: virtual __thiscall std::exception::~exception(void)" (??1exception@std@@UAE@XZ)
1>main.obj : error LNK2001: unresolved external symbol "public: __thiscall std::exception::exception(char const * const &)" (??0exception@std@@QAE@ABQBD@Z)
1>main.obj : error LNK2001: unresolved external symbol "public: __thiscall std::exception::exception(class std::exception const &)" (??0exception@std@@QAE@ABV01@@Z)
1>main.obj : error LNK2001: unresolved external symbol "void __cdecl std::_Xlength_error(char const *)" (?_Xlength_error@std@@YAXPBD@Z)

如何将我的应用程序与来自 DDK 的 libcpmt.lib 链接?链接后将添加多少字节到可执行文件?是否有另一个(第三方)轻量级 STL?

【问题讨论】:

  • 只是提示::exception 不是 STL 的一部分,它是 c++ 标准库的一部分。
  • 我知道;但它在libcpmt.lib,不是吗?
  • IIRC MSVC 中的 STL 根本不需要链接。话虽如此,您的应用程序可能比您想象的要重得多。那个msvcrt你觉得便宜?它在你不应该依赖的微软黑名单上。当您触摸它时,您的过程会自动受到怀疑。期望 Windows 启动它的应用程序兼容性,以确保您过时的应用程序仍然在现代 Windows 版本上运行(现代 = 本世纪;MSVCRT 可以追溯到 1998 年)。
  • @MSalters 将 c++ 标准库链接到带有 /MT or /MTD 之类的标志的 Visual C++ 编译器,但在幕后仍然链接运行时库(并添加一些定义)。如果不是这种情况,您将无法编译或链接不依赖于任何运行时的东西。
  • @PeterT:std::vector<T>(SL 中的 STL)之类的东西只有标题,这就是我的观点。 (嗯,你最终需要operator new,没办法)

标签: c++ visual-c++ stl static-linking


【解决方案1】:

这可能是不可能的,至少不可靠。 C++ 标准库可能依赖于 C 标准库的某些功能,但您尝试使用的未版本化 msvcrt.dll 是非标准的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-13
    • 2015-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-29
    • 2011-08-24
    相关资源
    最近更新 更多