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

14,880

Solution 1

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]

Solution 2

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.

Share:
14,880
Callum Rogers
Author by

Callum Rogers

I am interested in Functional Programming, Compilers, Programming Languages, Interpreters and Reactive Programming. Github

Updated on June 08, 2022

Comments

  • Callum Rogers
    Callum Rogers about 2 years

    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.

  • user2284570
    user2284570 almost 9 years
    Excellent answer, but what is the library for visual c++ 6.0 and below (corelibc have an another name) ?
  • Axel Podehl
    Axel Podehl over 5 years
    I had the same issue and it was driving me crazy. In my case "C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin\vcvars32.bat" amd64_x86 was the trick to reset the toolchain.
  • lalala
    lalala over 5 years
    "And kick out that ugly EmptyDllMain.obj " so how to solve the linker error then instead?