title: LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup with DLLs
body: <p>So here's another thread about the infamous ___tmainCRTStartup unresolved external symbol. Yes, it's about SDL again, but it's with a twist which AFAICS hasn't been covered here yet. 
  Let me first clear up some parameters:</p>
  
  <p>1) I am trying to build a DLL that statically links against SDL2.lib. </p>
  
  <p>2) I am not using Visual Studio but Visual C and a console (first cmake, then nmake).</p>
  
  <p>3) The project is entirely in C, no C++ code is in there (unless there's C++ in SDL2 but AFAIK SDL2 is completely in C)</p>
  
  <p>4) All objects that are collected in SDL2.lib and the other objects are compiled using /MT, i.e. I want to link statically against the Visual C runtime (libcmt.lib).</p>
  
  <p>5) SDL2.lib is then created like this:</p>
  
  <pre><code>link /nologo /lib /out:SDL2.lib file1.obj file2.obj....
  </code></pre>
  
  <p>6) The target DLL is linked like this:</p>
  
  <pre><code>link /dll /subsystem:WINDOWS /out:test.dll file1.obj file2.obj ... kernel32.lib user32.lib gdi32.lib shell32.lib ole32.lib oleaut32.lib imm32.lib winmm.lib version.lib SDL2.lib
  </code></pre>
  
  <p>What is confusing the hell out of me here is that the call above yields the following error:</p>
  
  <pre><code>LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup 
  </code></pre>
  
  <p>How can this possibly happen? I clearly pass <strong>/dll</strong> to link.exe so why on earth is the C runtime looking for main()? It should be looking for DllMain()! A main() symbol in a DLL doesn't make any sense at all, yet I get this confusing error! </p>
  
  <p>It must be something inside SDL2.lib that is causing this problem but I honestly have no clue what could force the linker to look for a main() entry point here. I mean, SDL2.lib is a linker library that should be completely <strong>target neutral</strong>, i.e. it should be possible to link SDL2.lib against a WinMain() executable but it should also be possible to link SDL2.lib against a DllMain() library but here it seems to refuse to be linked into a DLL! </p>
  
  <p>Does anybody have an idea what could be wrong here? I have searched for hours now and this totally escapes me.</p>
  
  <p><strong>EDIT:</strong> Interestingly, the problem does not occur when everything is compiled using /MD. It only occurs when using /MT. I'm really completely clueless as to what could be the cause here.</p>
  
  <p><strong>EDIT2:</strong> I've also tried to compile SDL2 with /DSDL_MAIN_HANDLED but it doesn't make the error go away.</p>