简体   繁体   English

在VC ++ 2008项目中使用VC ++ 2010运行时库

[英]Use VC++ 2010 runtime libraries in VC++ 2008 project

I work on the optimization algorithm so the performance really matters. 我致力于优化算法,因此性能非常重要。 The algorithm is about 8 times faster when compiled in VS 2010 compared to VS 2008. Googling shows that it is not my fault (see eg https://stackoverflow.com/a/5560184/890355 ). 与VS 2008相比,在VS 2010中编译时,该算法的速度提高了约8倍。谷歌搜索显示这不是我的错(参见例如https://stackoverflow.com/a/5560184/890355 )。 The problem is that the final project must be built under VS 2008. 问题是最终项目必须在VS 2008下构建。

The solution I tend to is to built my algorithm as DLL in VS 2010 and then link it to the main project. 我倾向于在VS 2010中将我的算法构建为DLL,然后将其链接到主项目。 Is it possible to use VC++ 2010 run-time libraries with my DLL under VS 2008? 是否可以在VS 2008下使用VC ++ 2010运行时库和我的DLL? If so, what is the least painful way to do it? 如果是这样,那么最痛苦的方法是什么? Any other ideas? 还有其他想法吗? Thanks. 谢谢。

The runtimes are not an issue. 运行时不是问题。 Nothing stops you from linking your DLL against the VC2010 runtime and then using that DLL in other projects. 没有什么能阻止您将DLL与VC2010运行时链接,然后在其他项目中使用该DLL。 It doesn't matter if those projects are built using Visual C++ 2008 or any other language. 如果这些项目是使用Visual C ++ 2008或任何其他语言构建的,则无关紧要。

The tricky part is designing the DLL interface. 棘手的部分是设计DLL接口。 Simply exporting some C++ classes is risky since it exposes you to incompatibilities between the different compilers. 简单地导出一些C ++类是有风险的,因为它会让您暴露出不同编译器之间的不兼容性。 I think your best bet would be to either expose a C-style interface or use COM. 我认为最好的办法是公开C风格的界面或使用COM。 I think COM is the best approach, but if you're unfamiliar with the technology, then a C-style interface will work fine. 我认为COM是最好的方法,但如果你不熟悉这项技术,那么C风格的界面就能正常运行。 (COM could also be over-kill if the interface is simple.) (如果界面很简单,COM也可能被过度杀死。)

If you ask for any other way to combine 2008 and 2010 libraries in one executable other than moving 2010 part outside into a DLL, than the answer is probably "there is no other easy way to achieve this". 如果你要求使用任何其他方法将2008和2010库组合在一个可执行文件中,而不是将2010部分外部移动到DLL中,那么答案可能是“没有其他简单的方法可以实现这一点”。

But if you don't want to "VC++ 2010 run-time libraries ... under VS 2008" (that is building against 2010 libraries in old 2008 IDE), but "use a 2010-compiled DLL in your 2008-compiled program", it is perfectly possible. 但是,如果您不想“VC 2008 2010运行时库......在VS 2008下”(即在旧版2008 IDE中针对2010库构建),而是“在2008编译的程序中使用2010编译的DLL” ,这是完全可能的。

The easiest way, as we do it in our projects, is to build (both .exe and DLL) against statically linked standard libraries (MFC, if you use it) and then use LoadLibrary in your .exe to load the DLL. 正如我们在项目中所做的那样,最简单的方法是针对静态链接的标准库(MFC,如果您使用它)构建(.exe和DLL),然后在.exe中使用LoadLibrary来加载DLL。 In the DLL you can export ( _declspec (dllexport) ) a function (preferably inside extern "C" {} guards) and use it in the .exe through GetProcAddress . 在DLL中,您可以导出( _declspec (dllexport) )函数(最好在extern "C" {}内部),并在.exe到GetProcAddress使用它。

Static linkage and explicit loading save you from a lot of inconsistency bugs caused by different runtimes. 静态链接和显式加载可以避免因不同运行时导致的大量不一致错误。

If you are worried about DLL loading and function calling costs, you can try to make these calls as rare as possible (maybe by moving not only the algorithm, but also some more high-level logics into the DLL). 如果您担心DLL加载和函数调用成本,您可以尝试尽可能少地进行这些调用(可能不仅要移动算法,还要移动更高级别的逻辑到DLL中)。 See this issie too. 看到这个也是。

And you can build all your code in one IDE (2010) using native multitargeting (however you will still need to build you main app and DLL separately against v9 and v10 libraries respectively). 您可以使用本机多目标在一个IDE(2010)中构建所有代码(但是您仍然需要分别针对v9和v10库分别构建主应用程序和DLL)。

You can if you're careful, and the MS documentation provides some hints. 如果您小心,可以使用MS文档提供一些提示。 I have answered this question before here: 我在此之前已经回答了这个问题:

Wondering if the lower version of visual studio can use the dll built using higher version of visual studio? 想知道Visual Studio的低版本是否可以使用使用更高版本的visual studio构建的dll?

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

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