0

I have few queries as mentioned below regarding command line options and its behavior in msvc:

1) How to build/create dynamic libraries ?

say I have two files mul.cpp and sum.cpp:

mul.cpp

#include "mul.h"
int mul(int a, int b)
{
    return (a*b);
}

sum.cpp

#include "sum.h"
int sum(int a, int b)
{
    return (a+b);
}

This is very generic platform independent implementations.

main.cpp

#include <iostream>
#include "mul.h"
#include "sum.h"

int main(int argc, char **argv)
{
    std::cout << "mul(2,5) = " << mul(2,5) << std::endl;
    std::cout << "sum(2,5) = " << sum(2,5) << std::endl;
    return 0;
}

I want to create shared libraries sum.dll and mul.dll and finally link them with main.o to generate executable using command prompt?

I came across a link which describes a solution

But it doesn't explains command line to use it. So How can I create a dll without worrying about manually creating .def file or using __declspec(dllexport) for functions ?

As over the link it's mentioned that /DEF or /DUMBIN can be used for the same, But how to use it for this example ?

2) How can I implement the same example for debug build?

Community
  • 1
  • 1
  • 1
    you are giving constraints that contradict the goal. If you compile the libraries themselves then the export libraries will also exist (commonly suffixed `.lib`), simply reference these in your linker options. The only other ways when the export libs are unavailable is to create your own set the way laid out in the answer you reference or dynamically retrieve the DLL function pointers at runtime (`GetProcAddress` and friends). – 0xC0000022L Jun 30 '14 at 13:26
  • @0xC0000022L Can you please lmk What constraint I have added that contradicts the goal ? I am asking a simple question about creating dll libraries without creating .def files manually and using __dllexports. I want my code to be platform independent and For above code in linux ".so" can be generated easily. But linux '.so' is different then windows 'dll' – Harry Cruise Jun 30 '14 at 19:30
  • yes, ruling out the use of `/def` and `/dumpbin` which is the way to go unless you have import libs provided by the author/owner of the library. DLLs and `.so` files on Linux are in so many ways different, it's one of the things were a platform-specific solution is called for for each respective platform. – 0xC0000022L Jun 30 '14 at 22:07

1 Answers1

0

Take these steps:

  1. Create a sample DLL and executable in MSVC that work together, the project wizard will help you with that.
  2. In places where __dllexport needs to go, you already have a macro. #define that to empty for anything that's not MS Windows. You might want to use GCC visibility annotations though.
  3. Locate the place in the project setups where you can tell it to give you the whole compiler and linker commandlines. These are then written to the output window during build.
  4. Study the setups used by MSVC's IDE. You can do the exact same steps on the commandline.

Also, check out cross-platform libraries like Boost, libSigC++, wxWidgets, STLport, etc for examples of code that can be compiled on multiple platforms.

Ulrich Eckhardt
  • 16,572
  • 3
  • 28
  • 55