简体   繁体   中英

Create MS Visual C++ DLL project out of existing sources

My goal is to compile existing C++ classes (legacy code, stored in a set of *.h files) into a DLL so that it can be further integrated into a C# application.

For that purpose, it seems best to use MS Visual Studio. I have no experience with this environment, so I tried the naive approach found on MSDN and other SO answers:

  • File | New | Project from existing code
  • selected Visual C++
  • selected file location that is base for include references used in those .h files
  • specified a project name
  • let the wizard find and add all C++ files below the directory
  • selected "Use Visual Studio" for build, with project type "Dynamically Linked Library (DLL) project"
  • checked none of the checkboxes below (ATL, MFC, CLR)
  • specified . dir in the "Include search paths (/I)" in Debug settings
  • checked "Same as Debug configuration" in "Release settings"
  • clicked Finish button

This creates couple of VS files in the directory:

mylibrary.sln
mylibrary.vcxproj
mylibrary.vcxproj.filters
mylibrary.vcxproj.user

With a project created this way, I press F6 or select Build | Rebuild solution from the menu.

Then I expect the build to produce the .dll file somewhere, but it does not appear. Only these files appear:

.vs/mylibrary/v15/.suo
.vs/mylibrary/v15/Browse.VC.db
.vs/mylibrary/v15/Browse.VC.opendb
.vs/mylibrary/v15/ipch/AutoPCH/efad7c74cd39331b/EXAMPLE.ipch
Debug/mylibrary.log
Debug/mylibrary.tlog/mylibrary.lastbuildstate

Next, I decided to try creating a fresh new library project, just to observe the differences to get some hints, but that did not help - there were too many differences, even in the file structure...

My questions are:

  • is my choice of MS Visual C++ a good one for given purpose?
  • if so, what am I doing wrong here?

I think your steps are probably correct and I think that the right approach to use the code from a C# application. You definitely can call a C++ library from C# by importing the methods.

You missed only to export the methods that you want to use from your library. try using __declspec(dllexport) with these methods. please check this link:

https://msdn.microsoft.com/en-us/library/a90k134d.aspx .

Also, the output should be at the build folder, not the source code folder

Compiling .h files into libraries is ok, the compiler does not care - however, the UI does. Still, you can tweak this by directly editing the .vcxproj file.

While doing so, make sure that the <ClCompile> sections contain:

  <RuntimeLibrary>MultiThreadedDLL</RuntimeLibrary>

Note that you can use commandline for building the DLL project:

"%ProgramFiles(x86)%\Microsoft Visual Studio\2017\Community\MSBuild\15.0\Bin\MSBuild.exe" -target:Clean,Build

(this assumes that your current directory is the one with your .vcxproj )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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