Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linking lots of .libs to make a DLL: unresolved external symbol _DllMainCRTStartup

Tags:

build

dll

linker

I'm performing the (terrifying) task of building LLVM 3.3 on windows and I have got to the stage where I have a load of LLVM*.lib files. I want to link them together to one huge shared DLL but am struggling (this is my first time linking stuff on windows). I've tried:

link /DLL /MACHINE:X64 /OUT:LLVM3.3.dll LLVM*.lib

but to no avail. It errors with:

LINK : warning LNK4001: no object files specified; libraries used
LINK : error LNK2001: unresolved external symbol _DllMainCRTStartup
LLVM3.3.dll : fatal error LNK1120: 1 unresolved externals

The internet suggested adding the /DEFAULTLIB:corelib switch, so I did that but again it has problems:

> link /DLL /MACHINE:X64 /DEFAULTLIB:corelibc /OUT:LLVM3.3.dll LLVM*.lib

LINK : warning LNK4001: no object files specified; libraries used
LINK : fatal error LNK1104: cannot open file 'corelibc.lib'

How do I do this?


EDIT: I managed to fix the above problem, by implementing an empty DllMain and making an EmptyDllMain.obj from it:

#include <windows.h>

BOOL APIENTRY DllMain( HANDLE hModule, 
                   DWORD  ul_reason_for_call, 
                   LPVOID lpReserved
                 )
{
    return TRUE;
}

and then trying:

link /DLL /OUT:LLVM3.3.dll LLVM*.lib EmptyDllMain.obj

but the DLL I get out is just 8kb - it seems to have missed out the many megabytes of LLVM libraries! How do I get them included?


EDIT2: I solved the LLVM compilation on Windows problem, take a look at this document on github.

like image 446
Callum Rogers Avatar asked Jul 18 '13 17:07

Callum Rogers


2 Answers

I had this once while linking one lib with a wrong platform set together (X86 to X64). Make sure all the LLVM*.lib are build and linked with the correct toolchain:

[...]\Microsoft visual Studio 10.0\VC\bin\amd64\ cl.exe and link.exe

which you get by calling

"%PROGRAMFILES(X86)%\Microsoft Visual Studio 10.0\VC\vcvarsall.bat" amd64

Also I had similar problems when mixing MT and MD CRTs, I recommend you stick to

/MD (or /MDd for debug) 

when compiling the objects for any of the LLVM*.lib (and any other objects from other external libraries you link into these).

[edit]

And kick out that ugly EmptyDllMain.obj !

[/edit]

like image 52
Oliver Zendel Avatar answered Sep 21 '22 20:09

Oliver Zendel


If you manually entered the _DllMainCRTStartup, be sure you spelled it (watch case) correctly. I had _DLLMainCRTStartup and took a while to catch why I still received the linker error. For Windows CE, the required link lib is corelibc.lib.

like image 30
Kermit Komm Avatar answered Sep 20 '22 20:09

Kermit Komm