locked
DLL and LIB?

    Question

  • I'm curious as to how I can have my project compile and produce both a DLL and a LIB file. I have seen it done before (such as SDL does so) and I have been unsuccessful in doing it myself (I have been only successful in building a DLL or a LIB, not both).

    Any help is appreciated; thanks!

    Thursday, February 01, 2007 1:07 AM

Answers

  • First hit I got when I typed .def in the MSDN library index.  You don't want to use .def files for DLLs written in C++ without extern "C".  You'd have to provide the mangled C++ symbol names.  When you use __declspec(dllexport), the compiler does it for you.
    Thursday, February 01, 2007 5:08 PM
    Moderator

All replies

  • I assume you mean a static link library, not a DLL import library.  I can start a LIB project with the Win32 + Win32 Project template.  Next, select "Static library".  Not sure if that's available in the Express edition.  Not recommended btw, static libraries are a maintenance headache due to their CRT version dependency.
    Thursday, February 01, 2007 9:28 AM
    Moderator
  • How would I link my DLL into a new project, then?
    I don't really want to have to do the whole __dllimport stuff.
    I mainly just want to link it in and have it work for me (similar to .libs).
    I have seen a .lib and .dll both be generated by the same project, though - would this be the best way or is there a way to easily link a DLL into my project that wouldn't cause a headache ?
    Thursday, February 01, 2007 1:14 PM
  • When the linker builds the DLL, it automatically creates a .lib file for it.  That's the import library for the DLL.  You can link that .lib with other projects that use the DLL and satisfy the linker without using dllimport.  Note that the platform SDK has a bunch of .lib files that work the same way (kernel32.lib, user32.lib etc).
    Thursday, February 01, 2007 1:46 PM
    Moderator
  • That's actually the main portion of my problem - I have set my project as being a DLL, but when I do build it, I only have a DLL output with no LIB in sight.
    I think that part of the problem might be that I am using Visual C++ Express, which does not have a DLL project type to choose from initially (I had to choose 'Application' and modify that to a DLL project).
    Thursday, February 01, 2007 2:25 PM
  • Are you exporting any functions?  For example:

    __declspec(dllexport) void Test() {
    }

    Thursday, February 01, 2007 2:54 PM
    Moderator
  • No, I'm not. As mentioned above, I don't really want to have to add the DLLExport to every single function I have. If I compile my library in G++, it creates the .so fine without me having to add any exporting additions at all. Also, I have looked at the SDL library's project and it seems they easily are able to create a .lib and .dll without having to add DLLexport to every function that they have, so I presume it is possible for me to do the same.
    Thursday, February 01, 2007 3:20 PM
  • If you don't export anything, either through __declspec(dllexport) or through a .def file, your DLL won't have any entrypoints for functions that can be called by a client.  Note that you can dllexport a class, all its methods will become available.  I can't comment on g++, it seems highly unlikely it would export every single non-static function.  There's probably some kind of .def style file too.
    Thursday, February 01, 2007 4:25 PM
    Moderator
  • Ah I see. Is there any resources you would recommend for me to read up on how to create and use these .def files? I checked real quick and found one or two, but they simply provided an example with minimal explanation(s).

    It also helps me quite a lot that you mentioned that an entire classes can be exported, instead of single functions, since my library is heavily object-oriented (the classes reside in their own namespace, as well; would be awesome if I could export the entire namespace).
    Thursday, February 01, 2007 4:51 PM
  • First hit I got when I typed .def in the MSDN library index.  You don't want to use .def files for DLLs written in C++ without extern "C".  You'd have to provide the mangled C++ symbol names.  When you use __declspec(dllexport), the compiler does it for you.
    Thursday, February 01, 2007 5:08 PM
    Moderator
  • Thanks
    I guess I will go ahead and modify my source to have the __declspec(dllexport) commands and (finally) have my working library.
    Thursday, February 01, 2007 5:35 PM