I'm attempting to build a program with Visual Studio 2008 on the command line. After reading Walkthrough: Compiling a Native C++ Program on the Command Line. I tried the following:
Run the vcvaralls.bat to setup the enviroment:
"C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\vcvarsall.bat"
Wrote this simple C++ application:
#define _WIN32_WINNT 0x501
#include <windows.h>
INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
MessageBoxA(0,"Hello","Hello",MB_OK);
return 0;
}`
And attempted to compile it:
cl /EHsc /GA simple.cpp
And this happens:
/out:simple.exe
simple.obj
simple.obj : error LNK2019: unresolved external symbol __imp__MessageBoxA@16 referenced in function _WinMain@16
simple.exe : fatal error LNK1120: 1 unresolved externals
Which leads me to believe that I need to include user32.lib or similar. I cant figure out from the visual studio manuals how to do that.
Right-click on the application project node in Solution Explorer and then choose Properties. In the VC++ Directories property page, add the path to the directory that contains the LIB file to Library Paths. Then, add the path to the library header file(s) to Include Directories.
Yes, to use MessageBox you need to link at least with User32.lib, as shown here . Use:
cl /EHsc /GA /MT simple.cpp User32.lib
/MT chooses the Run-time library. In this example I used multi-threaded static library.
You're correct, MessagBoxA
is defined in User32.lib
and you need to link your code with it. You can provide the linker options to the CL
compiler and it will pass it to the linker. All you need to do is to add the User32.lib
to your compilation string: cl /EHsc /GA simple.cpp User32.lib
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With