4

I'm trying to compile a dynamic library for Julia using C++. Am using CLion on windows. When I compile with MinGW, ccall works perfectly with the dll. When i'm compiling with MSVC, Julia ccall can't find the function. Does anybody have any idea why and how to solve this? I have to use MSVC..

sample code:

test.h

extern "C" int add2(int in);


test.cpp

#include "test.h"

int add2(int in){
return in+2;
}
Adam
  • 743
  • 1
  • 6
  • 11

1 Answers1

1

Found the answer. the MSVC compiler requires explicit instructions in order to output/input extern "C" functions. The following code works with MSVC and is recognized by Julia's ccall:

test.h

extern "C" __declspec(dllexport) int add2(int in);


test.cpp

#include "test.h"

int add2(int in){
return in+2;
}

In order to import an extern "C" function one may use:

 __declspec(dllimport)

Edit: This is not compiler related, but rather needed for all dll-files. Apparentely MinGW does this automatically.

Adam
  • 743
  • 1
  • 6
  • 11
  • This is not related to the functions being `extern "C"`. You always need to use `__declspec(dllexport)` to export functions in DLLs. – walnut Mar 03 '20 at 18:16
  • ok, but for some reason it worked with MinGW without the instruction – Adam Mar 03 '20 at 20:08
  • 1
    Seems that MinGW's linker exports all symbols by default, see https://stackoverflow.com/questions/2810118/how-to-tell-the-mingw-linker-not-to-export-all-symbols. – walnut Mar 03 '20 at 20:16
  • Thanks, added to answer – Adam Mar 03 '20 at 20:24