简体   繁体   English

Visual Studio:构建一个纯 static C++ 库(“无依赖关系”)

[英]Visual Studio: Build a pure static C++ Library ("no dependencies")

I am trying to build a static library as a release others can use (and they should be able to use it whether they debug their own code or not).我正在尝试构建一个 static 库作为其他人可以使用的版本(无论他们是否调试自己的代码,他们都应该能够使用它)。 My library on the other hand should not contain any debug symbols or so.另一方面,我的库应该包含任何调试符号左右。 I only want to release the headers and the.lib file (no.dll or so).我只想发布头文件和.lib文件(no.dll左右)。

The User of the lib should be able to use his runtime-environment and build flavor (debug or release) without any hassle. lib 的用户应该能够毫不费力地使用他的运行时环境和构建风格(调试或发布)。

Is this even possible with Visual Studio?这甚至可以通过 Visual Studio 实现吗?
[==> I already have read, that some users switch over to build this kind of lib with gcc] [==> 我已经读过,有些用户转而使用 gcc 构建这种库]

I have tried several cases and I always get something like "_ITERATOR_DEBUG_LEVEL" conflicts or so.我已经尝试了几种情况,但我总是遇到类似“_ITERATOR_DEBUG_LEVEL”冲突的情况。 Static linking of the runtime libraries seems not the solution too, but what else?运行时库的 Static 链接似乎也不是解决方案,但还有什么? Is it possible to switch off all references and let the project incorporating the library decide what runtime to use?是否可以关闭所有引用并让包含库的项目决定使用哪个运行时?

Even if this question is similar to the follwing I haven't found the correct answer there: Pure static C++ Library (no dependency of MSVC C++ runtime) or here Microsoft Visual Studio ~ C/C++ Runtime Library ~ Static/dynamic linking即使这个问题类似于以下问题,我也没有在那里找到正确的答案: Pure static C++ 库(不依赖于 MSVC ZF6F87C9FDCF8B3C3F07F93F1EE8712C ++ 运行时库或此处为静态/动态 Microsoft Visual Studio ~//C++ 运行时库

The solution is as follows (as stated by @drescherjm and @Eljay):解决方案如下(如@drescherjm 和@Eljay 所述):

You can build static libraries (.lib files) with VisualStudio (currently VS2019) that are usable within debug and release environments if only "C ABI" is used and exposed to the "outside" (the header file(s)).如果仅使用“C ABI”并暴露于“外部”(header 文件),您可以使用 VisualStudio(当前为 VS2019)构建 static 库(.lib 文件),这些库可在调试和发布环境中使用。

"C ABI" does NOT mean, that no C++ functionality may be used, it means that no "std::*" functions/containers/etc. “C ABI”并不意味着不能使用 C++ 功能,这意味着没有“std::*”函数/容器/等。 may be used.可能用过了。
Neither as function parameters nor as class members.既不是 function 参数也不是 class 成员。

#ifndef LIB_STATIC_H__
#define LIB_STATIC_H__

#include <stdint.h>

class MyLib
{
public:
    MyLib();
    ~MyLib();
    uint64_t TestFunc(char* param, uint32_t x);
};

#endif

This will work, but this NOT:这将起作用,但这不是:

#ifndef LIB_STATIC_H__
#define LIB_STATIC_H__

#include <stdint.h>
#include <string>

class MyLib
{
public:
    MyLib();
    ~MyLib();
    uint64_t TestFunc(char* param, uint32_t x);
private:
    std::string m_MyString;
};

#endif

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM