1

currently I am writing on a cpp-DLL. Afaik I have to put the functions into a class and a namespace if another cpp-program wants to use them. But I want to use the DLL with Labview too. Labview only recognizes the functions if they are free, e.g. neither in a namespace nor in a class. How can I implement this in my DLL? At the moment, I have set a #define-variable. If this variable is set, the functions are enclosed in a namespace and a class, if not, then they are free, but I have to compile the whole thing twice and I get two separate DLL files. So, what can I do if I only want one DLL file for both applications? (Please don't tell me to write the functions twice, the administrative outlay is even worse, I have tried this before). Or do I simply have to call the DLL via LoadLibrary() when not using namespaces?
Thank you very much!

IInspectable
  • 46,945
  • 8
  • 85
  • 181
arc_lupus
  • 3,942
  • 5
  • 45
  • 81
  • 1
    You DO NOT need to use classes or namespaces in order to make your DLL usable to other C++ projects. Where did you ever get that idea from? What you should be doing is exporting your DLL functions as free functions unconditionally, and then provide a separate wrapper class for C++ compilers, if desired. You only need 1 DLL (unless you are compiling for 32bit and 64bit). – Remy Lebeau Sep 16 '13 at 19:05
  • http://msdn.microsoft.com/de-de/library/ms235636.aspx from here – arc_lupus Sep 16 '13 at 19:07
  • @arc_lupus This explains how to add a **class** to a .dll. If you don't need a class, don't add one. And simply export symbols using `extern "C"`. Also read [Exporting C Functions for Use in C or C++ Language Executables](http://msdn.microsoft.com/en-us/library/ys435b3s.aspx). – IInspectable Sep 16 '13 at 19:11
  • So I can simply put several functions in my dll without considering about a class or a namespace and I can call them from every programming language I want? – arc_lupus Sep 16 '13 at 19:13
  • Yes, you export **functions**, not **classes**. You *may* provide a class that internally calls your DLL functions, if you so desire. But that is not a requirement. – Remy Lebeau Sep 16 '13 at 21:00

1 Answers1

0

Afaik I have to put the functions into a class and a namespace if another cpp-program wants to use them.

This is plain wrong. You do not need to do this at all. On the contrary, DLL were originally introduced as libraries of C functions. C++ uses mangled names to represent namespace/class and types of parameters. There is no standard on this. Different compilers use their own scheme.

To summarize:

  • If you export simple C function from your dll, this will always work.
  • If you export classes or something from a namespace, this will definitely work if other .exe/.dll is compiled with the same version of the compiler. If not - this depends.

Regarding the LoadLibrary: it should be used when you do not know the name of the DLL or a name of the function in this DLL ahead or when you do not want to load this DLL at the beginning of your process. Otherwise (simple case) link your executable with implib for that DLL. This perfectly works for simple c-functions. LoadLibrary should be used when direct linking is not good for some reason.

Kirill Kobelev
  • 10,252
  • 6
  • 30
  • 51
  • 1
    Exporting C++ classes from DLLs is very shaky. In general, the only times you are guaranteed it will work are the times you don't actually need it (use static libraries or just add the source files to the project). http://stackoverflow.com/questions/12314101/creating-c-dll-without-static-methods/12314276#12314276 – tenfour Sep 16 '13 at 19:49
  • This is when you use LoadLibrary: 1. You want the option of not loading the DLL, or 2. You are not in a position to guarantee its presence on the library search path. – David Heffernan Sep 16 '13 at 22:21