Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 e.g. https://stackoverflow.com/a/5560184/890355). The problem is that the final project must be built under 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. Is it possible to use VC++ 2010 run-time libraries with my DLL under VS 2008? If so, what is the least painful way to do it? Any other ideas? Thanks.

like image 480
AdelNick Avatar asked Aug 27 '12 13:08

AdelNick


People also ask

Do I need Microsoft Visual C++ 2008 redistributable?

There may be different versions of Microsoft Visual C++ Redistributable files. They would be for 2005, 2008 and 2010. Many times software requires these files and automatically install the required files. I do not suggest you delete them.

How do I add an external C++ library to my project?

How to Add an External C++ Library to Your Project Using the Visual Studio IDE. Step 1: Go to the website of the library. Step 2: Download the zip file that contains all the code. Step 3: Unzip the zip file to your computer.


2 Answers

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. It doesn't matter if those projects are built using Visual C++ 2008 or any other language.

The tricky part is designing the DLL interface. Simply exporting some C++ classes is risky since it exposes you to incompatibilities between the different compilers. I think your best bet would be to either expose a C-style interface or use 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 could also be over-kill if the interface is simple.)

like image 105
Peter Ruderman Avatar answered Oct 26 '22 23:10

Peter Ruderman


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".

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.

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. In the DLL you can export (_declspec (dllexport)) a function (preferably inside extern "C" {} guards) and use it in the .exe through 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). 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).

like image 38
Steed Avatar answered Oct 26 '22 23:10

Steed