user1745815
user1745815

Reputation: 23

Simple c style export import in c

I am trying to do a simple c style function export and import with visual studios 2012. I made two projects. My ultimate goal is to call c++ functions from a Fortran program, so I'm trying to make the dll as straightforward as possible. The first project simply contains 1 file which has this code:

#include <stdio.h>

extern "C" __declspec(dllexport) void __cdecl hello()
{
  printf("Hello, World!\n");
}

This is compiled as a .dll. I then include that in the linking input to the second project which simply contains a file with this:

extern "C" __declspec(dllimport) void __cdecl hello();

int main(int argc, char *argv[])
{
    hello();
    return 0;
}

When i link without common language runtime support i get the error:

Error   1   error LNK1107: invalid or corrupt file: cannot read at 0x2B8    

If i compile with common language runtime support i get the error:

Error   1   error LNK1302: only support linking safe .netmodules; unable to link 

To fix that the internet wants me to compile with /clr:pure or /clr:safe, but my vs tels me that doesnt work with c style exports.

my linking command line call is currently:

/OUT:"c:\users\kevin\documents\visual studio 2012\Projects\Project1\Release\Project2.exe" /MANIFEST /NXCOMPAT /PDB:"c:\users\kevin\documents\visual studio 2012\Projects\Project1\Release\Project2.pdb" /DYNAMICBASE "c:\users\kevin\documents\visual studio 2012\Projects\Project1\Release\*" "kernel32.lib" "user32.lib" "gdi32.lib" "winspool.lib" "comdlg32.lib" "advapi32.lib" "shell32.lib" "ole32.lib" "oleaut32.lib" "uuid.lib" "odbc32.lib" "odbccp32.lib" /DEBUG /MACHINE:X86 /OPT:REF /SAFESEH /PGD:"c:\users\kevin\documents\visual studio 2012\Projects\Project1\Release\Project2.pgd" /MANIFESTUAC:"level='asInvoker' uiAccess='false'" /ManifestFile:"Release\Project2.exe.intermediate.manifest" /OPT:ICF /ERRORREPORT:PROMPT /NOLOGO /TLBID:1 

Thanks in advance for you help.

Also, if any of you understand linking between fortran and c better then me do i want /clr when making the .dll to link with fortran (i assume that I don't)?

Upvotes: 0

Views: 4651

Answers (1)

Jan Hudec
Jan Hudec

Reputation: 76296

Shared library consists of two files on Windows. A .dll and a .lib. You have to provide the .lib one as linker input and you have to put the .dll in the same directory as the executable or somewhere in %PATH% at runtime.

Upvotes: 1

Related Questions