how to resolve the warning warning C4251: …. needs to have dll-interface to be used by clients of class 'TestClass'

huangapple go评论69阅读模式
英文:

how to resolve the warning warning C4251: .... needs to have dll-interface to be used by clients of class 'TestClass'

问题

我创建了以下简单的类并构建它

```cpp
class __declspec(dllexport) TestClass
{
private:
    std::unique_lock<std::mutex> lock;
public:
    void doIt() {
        // do something
    }
};

但是我遇到了以下警告

1>C:\gitview\frm_multi\framework\appCpp\acout.h(48,35): 警告 C4251: 'TestClass::lock':类 'std::unique_lockstd::mutex' 需要具有 dll 接口,以便被 'TestClass' 类的客户端使用
1>C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Tools\MSVC\14.29.30133\include\mutex(593): 消息:请参阅 'std::unique_lockstd::mutex' 的声明。

在我查找了一些文章之后,我将代码更新如下

template<> class __declspec(dllexport) std::unique_lock<std::mutex>;

class __declspec(dllexport) TestClass
{
private:
    std::unique_lock<std::mutex> lock;
public:
    void doIt() {
        // do something
    }
};

此时,我遇到了以下错误...

1>C:\gitview\frm_multi\framework\appCpp\acout.h(45,9): 错误 C2908: 显式特化;'std::unique_lockstd::mutex' 已经被实例化

如何解决这种情况?
我首先尝试的是将我的类暴露在 DLL 外部。


<details>
<summary>英文:</summary>

I create below simple class and build it


class __declspec(dllexport) TestClass
{
private:
std::unique_lock<std::mutex > lock;
public:
void doIt() {
// do something
}
};


but I met the below warning

1&gt;C:\gitview\frm_multi\framework\appCpp\acout.h(48,35): warning C4251: &#39;TestClass::lock&#39;: class &#39;std::unique_lock&lt;std::mutex&gt;&#39; needs to have dll-interface to be used by clients of class &#39;TestClass&#39;
1&gt;C:\Program Files\Microsoft Visual Studio22\Enterprise\VC\Tools\MSVC.29.30133\include\mutex(593): message : see declaration of &#39;std::unique_lock&lt;std::mutex&gt;&#39;

after I googled some article, I updated my code like below


template<> class __declspec(dllexport) std::unique_lock<std::mutex>;

class __declspec(dllexport) TestClass
{
private:
std::unique_lock<std::mutex > lock;
public:
void doIt() {
// do something
}
};


at this moment, I met below error...

1&gt;C:\gitview\frm_multi\framework\appCpp\acout.h(45,9): error C2908: explicit specialization; &#39;std::unique_lock&lt;std::mutex&gt;&#39; has already been instantiated




how to solve this situation?
what I first try is just to expose my class outside DLL.

</details>


# 答案1
**得分**: 1

你可以这样消除警告:

```cpp
使用 PrivateData = 结构 {
  std::unique_lock<std::mutex> lock;
};

类 __declspec(dllexport) TestClass {
private:
  PrivateData data;
public:
  void doIt() {
    // 做一些事情
  }
};

如果你的组件的用户不需要知道私有成员,你可以考虑只公开一个接口:

 __declspec(dllexport) TestClass {
  virtual void doIt() = 0;
};

 TestClassImplementation: public TestClass {
private:
  std::unique_lock<std::mutex> lock;
public:
  void doIt() {
    // 做一些事情
  }
};
英文:

You can get rid of your warning like this:

using PrivateData = struct {
  std::unique_lock&lt;std::mutex &gt; lock;
};

class __declspec(dllexport) TestClass {
private:
  PrivateData data;
public:
  void doIt() {
    // do something
  }
};

If users of your component don't need to know about private members, you can consider to expose only an interface :

class __declspec(dllexport) TestClass {
  virtual void doIt() = 0;
};

class TestClassImplementation: public TestClass {
private:
  std::unique_lock&lt;std::mutex&gt; lock;
public:
  void doIt() {
    // do something
  }
};

huangapple
  • 本文由 发表于 2023年2月24日 13:25:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/75552891.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定