Search code examples
cwindowscl

cl.exe not generating functions that can be accessed externally


I tried building an add function as a library with Microsoft's cl.exe, but none of the functions are able to be accessed externally. I also tried adding the Extern "C" in the header with this from another post:

#ifdef __cplusplus
#define EXTERNC extern "C"
#else
#define EXTERNC
#endif

EXTERNC int add(int a, int b);

Build script:

cl /LD add.c

Built binary in Dependency Walker


Solution

  • To export a function you need to use __declspec(dllexport) or a .def file.

    __declspec(dllexport) int add(int a, int b) { return a + b; }