Hello friends,

Suppose I want to write a dll file with MS-VC8.0 compiler. I need to add __declspec(dllexport) for all of the functions which will be called from outside of that dll.
Now my question is that whether it is possible to export all functions of that dll at a time, ie, without writing __declspec(dllexport) for all of the functions.

Thanx,
Amit

Dani AI

Generated

Short answer: there is no single MSVC switch that blindly exports every function from a DLL — you either mark symbols or tell the linker exactly which names to export. is correct that the usual C/C++ pattern is to annotate symbols (commonly via a build-time macro), and 's class-level export trick is a convenient shorthand for whole types. Below are practical alternatives and caveats to consider.

A module-definition (.def) file is the standard way to export a list of names at link time without sprinkling __declspec(dllexport) through every source file. Example:

; mylib.def
LIBRARY "MyLib"
EXPORTS
    Init
    DoWork
    Shutdown

Put that .def into Linker -> Input (or pass /DEF:mylib.def) and the linker will export the listed symbols. Note: for C++ functions you either must use extern "C" to avoid name mangling or export the compiler-decorated names (which is fragile).

Other options: pass per-symbol /EXPORT:Name to the linker or use #pragma comment(linker, "/EXPORT:...") from a header to centralize exports. Both still require listing names, though you can script generation of that list (for example by parsing dumpbin /symbols output from compiled .obj files).

Do not export C++ implementation details you may change. Exporting whole classes exports vtables/typeinfo and ties ABI to compiler/runtime; it’s fine when you control both sides but brittle for public APIs. Best practice is to expose a small C-style API (opaque handles, plain C types) and use a MYDLL_API macro to toggle between export/import during build. Use the .def/linker methods only to avoid per-function annotations — not as a substitute for careful API design.

Recommended Answers

All 2 Replies

As far as I know, you have to put it before every function. I suppose you could save yourself a few keystrokes by doing this though.

#define DLLEXP __declspec( dllexport )

DLLEXP void someFunct() {
  // Code here
}

I think williamhemsworth's right, but I think you can export all members of a class in one swoop:

dll.h

#include <iostream>

#ifdef TWOMERS_BUILD_DLL
  #define TWOMERS_EXPORT __declspec(dllexport)
#else
  #define TWOMERS_EXPORT __declspec(dllimport)
#endif

class TWOMERS_EXPORT myClass {
  private:
    int thing;

  public:
    int returnThing();
    void setThing(int _thing);
};

dll.cpp:

#include "dll.h"

#define TWOMERS_BUILD_DLL

int myClass::returnThing() {
  return thing;
}
void myClass::setThing(int _thing) {
  thing = _thing;
}

Might be worth wrapping functions in a class if you want to save yourself some typing. Otherwise bite the bullet.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.