0

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)?

user1745815
  • 23
  • 2
  • 6
  • 1
    I think you should not link directly with the dll, instead you create an *import library*, with the same name as the dll but .lib and link with that. – john Oct 24 '12 at 09:32
  • Did you link with DLL, or lib? – Eric Z Oct 24 '12 at 09:35
  • `"c:\users\kevin\documents\visual studio 2012\Projects\Project1\Release\*"` in the command-line makes no sense. `*` is all files and there are several files that the linker will barf on (if it expands the `*` as in Windows each application has to do that itself, but if it does not, the file does not exist). – Jan Hudec Oct 25 '12 at 08:09

1 Answers1

1

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.

Jan Hudec
  • 73,652
  • 13
  • 125
  • 172
  • I attempted to have it generate that following [the second answer here](http://stackoverflow.com/questions/584041/how-do-i-build-an-import-library-lib-and-a-dll-in-visual-c). Now it generates the .lib file but still gives the same error. – user1745815 Oct 24 '12 at 11:06
  • I also tried recreating the first projects as a win32 dll project and that didn't change anything either. – user1745815 Oct 24 '12 at 11:19
  • @user1745815: And have you fixed invocation of the linker building the final executable? Generating the .lib file is one thing, but you have to use it too. – Jan Hudec Oct 24 '12 at 11:34
  • I did the same thing i did to add the original .dll namely added as an additional dependency in the linker input. – user1745815 Oct 24 '12 at 23:21
  • @user1745815: Check the resulting command line (there is command-line tab in each tool setting) and if unsure, add the command-lines to the question. – Jan Hudec Oct 25 '12 at 06:41
  • Its there. I changed the line to a * to include any other files necessary, adding command line to the question – user1745815 Oct 25 '12 at 07:45