0

I have written the following code as a sample:

#include <stdio.h>
#include <windows.h>
#include <tchar.h>

extern "C" BOOL WINAPI DllMain(HINSTANCE arg_instance, DWORD arg_reason, LPVOID arg_reserved)
{
    switch(arg_reason)
    {
        case DLL_PROCESS_ATTACH: puts("DllMain called for DLL_PROCESS_ATTACH"); break;
        case DLL_PROCESS_DETACH: puts("DllMain called for DLL_PROCESS_DETACH"); break;
    }

    return TRUE;
}

Then I try to link it with the following command:

link DllWithEntryPoint.obj /DLL /ENTRY:DllMain

It gives me the following errors:

LIBCMT.lib(crt0.obj) : error LNK2019: unresolved external symbol main referenced in function __tmainCRTStartup something.dll : fatal error LNK1120: 1 unresolved externals

Where is my mistake and how can I link it correctly?

lightning
  • 37
  • 4
  • Does this answer your question? [error LNK2019: unresolved external symbol \_main referenced in function \_\_\_tmainCRTStartup](https://stackoverflow.com/questions/4845410/error-lnk2019-unresolved-external-symbol-main-referenced-in-function-tmainc) – Simon Mourier Nov 28 '21 at 11:03
  • No, it doesn't work. @SimonMourier – lightning Nov 28 '21 at 11:04
  • 3
    You are missing the [/DLL](https://learn.microsoft.com/en-us/cpp/build/reference/dll-build-a-dll) linker option. Once that is fixed make sure to remove the `/ENTRY` option. It's probably not what you want when linking against the CRT. – IInspectable Nov 28 '21 at 11:04
  • 1
    Contrary to its name, DllMain is not actually a usable entrypoint for a DLL. It is very important that *first* the C and C++ runtime libraries get initialized, then it is safe for the runtime library to invoke DllMain(). You'll have to do this without the /ENTRY option. Or write code that doesn't use the runtime libraries at all, that's very hard to do. – Hans Passant Nov 28 '21 at 11:32

0 Answers0