-2

Visual Studio 2017 (fresh install- command line version) gives error:

LNK2019: unresolved external symbol Mhook_SetHook referenced in function wmain LNK2019: unresolved external symbol Mhook_UnHook referenced in function wmain

Compile command: cl /Y- /EHsc /DUNICODE mhook-test.cpp gdi32.lib user32.lib

I thought this might be related to C / C++ mixup, so I modified mhook.h as follows:

#ifdef __cplusplus
extern "C" {
#endif

BOOL Mhook_SetHook(PVOID *ppSystemFunction, PVOID pHookFunction);
BOOL Mhook_Unhook(PVOID *ppHookedFunction);

#ifdef __cplusplus
} // extern "C"
#endif

This did not make a difference in results.

I don't understand why the linker is having a problem.

mhook-test.cpp came with the mhook library.

  • 2
    Possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – user0042 Sep 09 '17 at 16:16
  • Check about c++ name mangling. If that library was compiled using the C++ compiler the `extern "C"` might be wrong here. – user0042 Sep 09 '17 at 16:17
  • Your .h file basically says "there is a function named Mhook_SetHook but it is defined somewhere else". You did not provide the "somewhere else" to the linker. You are linking gdi32.lib and user32.lib but not mhook.lib. So of course the linker can't find it. – Hans Passant Sep 09 '17 at 16:28
  • @HansPassant: I thought that might be the issue, but I have only the source code for the mhook library, not the library itself. In other words, I don't have a "mhook.lib" file; I thought it would be created along with the test program? – user2057674 Sep 09 '17 at 16:53
  • You did not compile it. Do learn how to use a project in Visual Studio, projects avoid many newbie mistakes. – Hans Passant Sep 09 '17 at 16:59
  • @HansPassant: I figured out how to create the libraries; had to individually create obj files, then create lib files out of those (https://msdn.microsoft.com/en-us/library/ms235627.aspx). – user2057674 Sep 09 '17 at 17:17
  • @HansPassant: You're right, using the newbie friendly GUI does eliminate newbie mistakes, but I prefer doing things more down to earth. Thank you for your comment, you pointed me in the right direction. – user2057674 Sep 09 '17 at 17:19

1 Answers1

0

The mhook library is not distributed precompiled; it must be compiled into libraries which can then be specified to linker.

So, for each ".c" and ".cpp" file in the source code of the mhook directory:

"First, run cl /c /EHsc MathFuncsLib.cpp to compile the code and create an object file that's named MathFuncsLib.obj. (The cl command invokes the compiler, Cl.exe, and the /c option specifies compile without linking). Second, run lib MathFuncsLib.obj to link the code and create the static library MathFuncsLib.lib." - https://msdn.microsoft.com/en-us/library/ms235627.aspx

These must then be specified for the final compile command: "cl /Y- /EHsc /DUNICODE /D_UNICODE mhook-test.cpp gdi32.lib user32.lib mhook.lib disasm.lib misc.lib disasm_x86.lib cpu.lib"