简体   繁体   English

将DLL C ++类库移植到Visual Studio的问题

[英]issues with porting a DLL C++ class library to Visual Studio

I wrote a class library in C++ and successfully compiled it in Linux with g++ as a shared object, then created a few apps that use it. 我用C ++编写了一个类库,并使用g ++作为共享对象在Linux中成功编译了它,然后创建了一些使用它的应用程序。 Now I have to port it to VS2008. 现在我必须将它移植到VS2008。 I gave all the classes the required __declspec(dllexport) prefixes, then tried to compile it. 我给了所有类所需的__declspec(dllexport)前缀,然后尝试编译它。 I get a pile of warnings, which basically have to do with: 我得到一堆警告,基本上与以下有关:

  1. my custom exception classes, derived from std::runtime_error, which yield: "warning C4275: non dll-interface class 'std::runtime_error' used as base for dll-interface class 'cci::FileOperationException'". 我的自定义异常类,派生自std :: runtime_error,它产生:“警告C4275:非dll接口类'std :: runtime_error'用作dll接口类'cci :: FileOperationException'的基础”。 How am I supposed to make a standard library class dll-exportable? 我怎么能让标准库类dll-exportable?
  2. exception specifications in member functions' declarations, which cause "warning C4290: C++ exception specification ignored except to indicate a function is not __declspec(nothrow)". 成员函数声明中的异常规范,导致“警告C4290:忽略C ++异常规范,但指示函数不是__declspec(nothrow)”。 I read somewhere that VS doesn't support these, and that it does somewhere else. 我读到某个地方,VS不支持这些,并且它在其他地方。 How very confusing. 多么令人困惑。

I read people saying that exporting classes in a DLL is generally a Bad Idea, that there's a myriad of things that can go wrong, and now I have my head full of concepts like binary incompatibility, dll hell, compiler version mismatches etc, and to be honest I can't really make heads or tails of it. 我读到人们说在DLL中导出类通常是一个坏主意,有无数的东西可能出错,现在我的头脑中充满了二进制不兼容,dll地狱,编译器版本不匹配等概念,以及说实话,我不能真正做出正面或反面。 What is the correct, safe and easy way to create a shared class library in Windows, then? 那么在Windows中创建共享类库的正确,安全和简便的方法是什么?

Thanks. 谢谢。

I maintain a C++ class library that is typically used as DLL on Windows, so it can be done. 我维护一个C ++类库 ,通常在Windows上用作DLL,因此可以完成。 Regarding your issues: 关于你的问题:

  1. That doesn't happen in my library. 这不会发生在我的图书馆。 Perhaps you need to be using the /MD and /MDd build options? 也许您需要使用/ MD和/ MDd构建选项? That way your C++ run-time-library comes from a DLL, too, which is the sort of picky thing VC++ is famous for. 这样你的C ++运行时库也来自DLL,这是VC ++着名的那种挑剔的东西。

  2. Don't use throw-specs. 不要使用throw-specs。 They are evil . 他们是邪恶的 If you feel you must do it anyway, just put something like this in a header file that every module includes before it gets to code that uses throw-specs. 如果您认为必须这样做,只需将这样的内容放在每个模块包含的头文件中,然后再使用throw-specs代码。

#pragma warning(disable: 4290) #pragma warning(禁用:4290)

First you should ask yourself if you really need a dynamic library here. 首先,你应该问自己,你真的需要一个动态库。 Static libraries or or even better including the source code directly in your project is a good solution that comes without those problems. 静态库或甚至更好地直接在项目中包含源代码是一个很好的解决方案,没有这些问题。

If you really need the DLL, the way I would do this (given much time) is wrapping your class in a C interface. 如果你真的需要DLL,我会这样做(给定很多时间)将你的类包装在C接口中。 You can then recreate your old C++ interface as a C++ header only library that interacts only with the C calls exported from the DLL. 然后,您可以将旧的C ++接口重新创建为仅与C ++标头库相互作用,该库仅与从DLL导出的C调用进行交互。 A bonus feature for this approach is that it will be trivial to use your library from almost any programming language since importing C functions from DLL's is highly supported. 这种方法的一个额外功能是,从几乎任何编程语言中使用您的库都是微不足道的,因为从DLL中导入C函数是非常受支持的。

Another way to go would be to use COM but since you are porting from Linux this is probably not an option. 另一种方法是使用COM,但由于你从Linux移植,这可能不是一个选择。

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

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