静态库能够与任何其他对象链接,而不管C++该对象使用的运行时

发布于 2025-01-25 05:52:44 字数 823 浏览 4 评论 0原文

我正在尝试确定是否有可能创建一个静态库,该库:

  1. 内部使用Microsoft/stl,静态发布运行时(/mt)
  2. 可以链接到不使用/MT的对象(例如,动态DLL释放,静态调试,静态调试,,等等。)

我要工作的特定测试代码如下:

标题:

#pragma once

#ifdef __cplusplus
extern "C" {
#endif

    int EncapLibConcatenate(int a, int b);

#ifdef __cplusplus
}
#endif

来源:

#include "EncapLib.h"
#include <string>

int EncapLibConcatenate(int a, int b)
{
    try {
        return std::stoi(std::to_string(a) + std::to_string(b));
    } catch (...) {
        return 0;
    }
}

链接到上述可以预见的是,当不使用 /MT用于链接到上述库的项目时,会导致A /FailifMismatch错误。我想了解任何编译器 /链接器选项以避免此问题。如果不存在,那么也欢迎其他任何方法(例如修改.OBJ COFF文件)。

附带说明,我知道创建动态库是封装库依赖关系的惯用方法。这是关于技术可能性的问题,而不是最佳实践。我还能够通过避免使用标准库(限制到C Runtime和Win32)来创建一个运行时不稳定的静态库。这个问题是专门针对内部使用C ++标准库的静态库(尽管不是在其界面上)。

I am trying to determine if it is possible at all to create a static library that:

  1. Internally uses Microsoft/STL, static release runtime (/MT)
  2. Can be linked to objects not using /MT (e.g., Dynamic DLL Release, Static Debug, etc.)

The specific test code I'm trying to get working is as follows:

Header:

#pragma once

#ifdef __cplusplus
extern "C" {
#endif

    int EncapLibConcatenate(int a, int b);

#ifdef __cplusplus
}
#endif

Source:

#include "EncapLib.h"
#include <string>

int EncapLibConcatenate(int a, int b)
{
    try {
        return std::stoi(std::to_string(a) + std::to_string(b));
    } catch (...) {
        return 0;
    }
}

Linking to the above predictably results in a /FAILIFMISMATCH error when not using /MT for the project that links to the above library. I would like to learn of any compiler / linker options to circumvent this issue. If such do not exist, then any other approaches (such as modifying the .obj COFF files post-build) would also be welcome.

As a side note, I am aware that creating a dynamic library is the idiomatic approach to encapsulating library dependencies; this is a question regarding technical possibilities, not best practices. I have also been able to create a runtime-agnostic static library by eschewing any use of the standard library (restricting to C runtime and win32). This question is specifically about a static library that uses the C++ standard library internally (though pointedly not at its interface).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文