views:

962

answers:

4

When creating a DLL with Visual C++ 2008 I have a couple of choices. I can create a "Class Library", which I understand will actually give me a .Net Library that uses the CLI (managed) extenstion of C++.

Since I don't want that, and I assumed that I need a static .LIB file to link into another Visual C++ windows executable project, I choose instead "Win32 Project" and, on the Application Settings panel, specify a C++ (no MFC) DLL.

This will create a project with a .cpp file which is supposed to be where I define "the exported functions for the DLL application".

This doesn't seem to be what I want either. Basically, what I'm looking for is the native C++ equivalent of what would, in C# .NET be a class library assembly. I want to package some classes into a DLL, then have a .EXE project use the DLL's classes by including the DLL project header files and link with a .LIB to resolve references.

What's the usual way of doing this?

+3  A: 

You're doing it right. What you'll need is to mark your classes with __declspec(dllexport) to make them available from outside the project. When you build the project, you'll generate both a .DLL and a .LIB.

Mark Ransom
Great. And a similar approach on the other side to import?
Buggieboy
Take a look at what the auto-generated code does. It actually macros the dllexport/import based on a precompiler directive so the consumer doesn't have to think about import versus export.
ctacke
See my answer here for an elaboration on what ctacke is talking about (http://stackoverflow.com/questions/1179103/visual-studio-2005-linker-problem/1179118#1179118)
Nick Meyer
Thanks. That clarifies the comment in the header file that the wizard generates.
Buggieboy
+1  A: 
  • Create a new Project
  • Visual C++ : Win32 : Win32 Project
  • Application Settings select DLL and check 'Export Symbols"

When you generate the project, it will stub out an exported class for you, typically named C{MyLib}.

ctacke
+1  A: 

You are right to make a C++ (no MFC) DLL. You can create your classes and those entry points which you define will be exported from that DLL for use by other C++ code (for example, a Win32 application written in C++).

Since C++ names get mangled automatically by the compiler to weird and wonderful values, it's not practical to export them as is if the DLL's clients are, for example, C programs. But if everything is in C++, you should be OK.

If you create some classes, you can choose to have them linked dynamically (as a DLL) but you will need an import library (created for you automatically) which contains the DLL's symbol definitions. You can also choose to link statically to your code from an application - in this case you would end up with a static library (also a .LIB) which contains the actual object code in your classes rather than symbols in a DLL.

The advantage of a DLL is, of course, that if you write several applications using your library, they can all share the DLL; with a static library, they would each contain a copy of your library code.

Vinay Sajip
+1  A: 

I think this article describes what you are trying to do: http://www.codeproject.com/KB/mcpp/usingcppdll.aspx

Personally I also prefer exporting C functions (as opposed to C++) where I make the this pointer explicit to avoid having to care about compiler specific method name decoration and exposing compiler generated functions.

Laserallan