Answered by:
junk.obj : error LNK2019: unresolved external symbol __imp_MessageBoxA referenced in function main

Question
-
This simple program:
#include <windows.h>
int main( int argc, char **argv ) {
MessageBox( NULL, "Some text", NULL, 0 );
return 0;
}
Should compile and link with just this command:
C:\test>cl junk.c
Microsoft (R) C/C++ Optimizing Compiler Version 15.00.21022.08 for x64
Copyright (C) Microsoft Corporation. All rights reserved.
junk.c
Microsoft (R) Incremental Linker Version 9.00.21022.08
Copyright (C) Microsoft Corporation. All rights reserved.
/out:junk.exe
junk.obj
junk.obj : error LNK2019: unresolved external symbol __imp_MessageBoxA referenced in function main
junk.exe : fatal error LNK1120: 1 unresolved externalsbut it doesn't.
Why not?
And please do not refer me to previous answers to similar questions, because they do not answer my question.
Simple programs like this have always compiled and linked clean with just
cl file.c
but suddenly ... yes, I know ... but I have not installed anything new since this worked perfectly earlier in the week.
Something has obviously changed, but what?
I'm not looking for a fix; eg. #pragma comment(lib, 'some.lib'); it shouldn't be necessary. I want to understand what could have changed and how I change it back again.
Thanks. Buk
Friday, May 13, 2016 8:08 AM
Answers
-
Using the VS2015 Developer Command Prompt the command "cl junk.c" produced exactly the same result -- LNK1120 error. I don't know why you think this is unusual since there is no default setting in the command line environment that specifies the inclusion of user32.lib by the linker.
The unwanted "fix" is to properly specify the lib in the command line -- cl junk.c user32.lib.
See .Lib Files as linker Input at https://msdn.microsoft.com/en-us/library/ba1z7822.aspx
The simple "file.c" programs that previously linked for you probably only used C/C++ standard library functions. The VC++ compiler automatically builds a reference to these standard libraries into the object module so the linker will look for them without having to specify them on the command line.
Friday, May 13, 2016 12:32 PM
All replies
-
I never compiled my stuff by hand from commandlione, but I'd say you also need to link against user32.lib:
Best regards
Bordon
Note: Posted code pieces may not have a good programming style and may not perfect. It is also possible that they do not work in all situations. Code pieces are only indended to explain something particualar.Friday, May 13, 2016 10:37 AM -
Using the VS2015 Developer Command Prompt the command "cl junk.c" produced exactly the same result -- LNK1120 error. I don't know why you think this is unusual since there is no default setting in the command line environment that specifies the inclusion of user32.lib by the linker.
The unwanted "fix" is to properly specify the lib in the command line -- cl junk.c user32.lib.
See .Lib Files as linker Input at https://msdn.microsoft.com/en-us/library/ba1z7822.aspx
The simple "file.c" programs that previously linked for you probably only used C/C++ standard library functions. The VC++ compiler automatically builds a reference to these standard libraries into the object module so the linker will look for them without having to specify them on the command line.
Friday, May 13, 2016 12:32 PM