Exporting STL class from DLL - why is there no warning from the return type?

前端 未结 2 736
青春惊慌失措
青春惊慌失措 2021-02-18 20:50

My question is related to exporting a C++ class with STL inside. For example:

class __declspec(dllexport) Hello
{
    std::string   name;

public:
    std::strin         


        
2条回答
  •  旧时难觅i
    2021-02-18 21:33

    Various articles seems to indicate that this is very bad

    Yes, it can be. And your project settings are going to get you into the kind of trouble they are warning about. Exposing C++ objects by value requires the client of your DLL to use the same CRT so that objects that are created in the DLL can be safely destroyed by the client app. And the other way around. Which requires that these modules use the same heap.

    And your project settings prevent that from being possible, the gist of the compiler warning. You must specify the shared version of the CRT so that all modules load the one-and-only implementation of the CRT.

    Fix that with Project + Properties, C/C++, Code Generation, Runtime library setting. You have it now at /MT, it must be /MD. Change this for all modules and all configurations.

提交回复
热议问题