sir i have written a simple program to display a message box in visual c++ 2008 but the problem is that when i run this code a dialog box shows that "your project is out of date would you like to build it"
when i press yes it shows error
so what is the problem???
the code is here
#include <Windows.h> /* The standard "windows.h" inclusion */
int WINAPI WinMain( HINSTANCE hInstance, /* A handle to the current instance of the application. */
HINSTANCE hPrevInstance, /* A handle to the previous instance of the application. */
LPSTR lpCmdLine, /* The command line for the application, excluding the program name. */
int nCmdShow) /* Controls how the window is to be shown. */
{
/* Call to the MessageBox function */
MessageBox(NULL, "Hello, Windows API!", "Hello", MB_OK);
/* WinMain returns 0 if we exit before we enter message loop, more on that to come */
return 0;
}
Whenever i run this in visuall c++ 2008 it says project out of date, do u want to build so i click yes but then it cant
down the bottom it says
1>Linking...
1>MSVCRTD.lib(crtexe.obj) : error LNK2001: unresolved external symbol _main
1>C:\Documents and Settings**\My Documents\Visual Studio 2008\Projects\msg\Debug\msg.exe : fatal error LNK1120: 1 unresolved externals
1>Build log was saved at "file://c:\Documents and Settings**\My Documents\Visual Studio 2008\Projects\msg\msg\Debug\BuildLog.htm"
1>Wrath Lands - 2 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
VS creates UNICODE projects by default so macros like MessageBox evaluates to MessageBoxW which expects LPCWSTR arguments and not LPCSTR. Try change to: MessageBox(NULL, _T("Hello, Windows API!"), _T("Hello"), MB_OK);
The error message error LNK2001: unresolved external symbol _main is important. It looks like you've created a console project but there is no main() function defined, hence the linker error.
When you create a new Visual Studio project as a Win32 Console Application it assumes the entry point to your program will be the normal C/C++ main() function and it links with the C/C++ library startup code. If you instead create a Win32 Project it assumes the entry point will be WinMain() and links with Windows application startup code.
To avoid this problem you should begin with a Win32 Project. To fix it after the fact you could try going into Project Properties -> Linker -> System and change the SubSystem option from Console (/SUBSYSTEM:CONSOLE) to Windows (/SUBSYSTEM:WINDOWS). Note that there may be other settings that should be changed as well so I suggest you just start fresh with a new Win32 Project instead.
Related
This is the only code I have:
#include <windows.h>
int APIENTRY wWinMain(
_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPWSTR pCmdLine,
_In_ int nCmdShow)
{
OutputDebugStringA("Hello\n");
return 0;
}
The wWinMain function with the exact same signature works in a separate project I created with the "Windows Desktop Application" (or something) template.
However I am getting a LNK2019 with the message:
unresolved external symbol _main referenced in function "int __cdecl invoke_main(void)" (?invoke_main##YAHXZ)
coming from MSVCRTD.lib(exe_main.obj)
I am using Visual Studio 2019 Community Edition with MSVC 2019 on Windows 10. I created an "Empty Project" and only have a "main.cpp" file in the "Source Files" directory.
Can anyone help me try to diagnose what may be going wrong here? I am having no luck reading msdn/searching the web for the solution, it is very frustrating as I am just trying to get off the ground here.
Thank you.
Set Subsystem to Windows in linker settings (for all configurations and targets).
Visual Studio can compile for different Windows subsystems, each requiring its own program entrypoint. An error saying "_main not found" suggests your subsystem is set to Console, since the linker is looking for a main() function.
If you have a WinMain() entrypoint (or wWinMain() for Unicode mode), it means you're targeting the Windows subsystem.
Here is a simple example for DHCP client API, https://msdn.microsoft.com/en-us/library/windows/desktop/aa363345(v=vs.85).aspx
I'm working in Visual Studio 2012 with console application c++, it has a failure when building:
1>------ Build started: Project: ConsoleApplication2, Configuration: Debug Win32 ------
1>MSVCRTD.lib(crtexew.obj) : error LNK2019: unresolved external symbol _WinMain#16 referenced in function ___tmainCRTStartup
1>D:\visual work place\ConsoleApplication2\Debug\ConsoleApplication2.exe : fatal error LNK1120: 1 unresolved externals
Can anybody know how to solve this problem?, thanks
There are lots of hits when Google-ing this error. Here's one.
There's a mismatch between your C code and your VStudio project type.
Your app's entrypoint is probably int main(int argc, char **argv) (this is one of its most general forms) which in MS world is corresponding to a Console application.
But MS has defined other application types. One of them is a GUI (window based) which doesn't require a console. For that one MS defined an entrypoint as:
int CALLBACK WinMain(_In_ HINSTANCE hInstance, _In_ HINSTANCE hPrevInstance, _In_ LPSTR lpCmdLine, _In_ int nCmdShow). You can find more details about it on MSDN.
When building the application's executable, the linker must know what type the application has. That is specified by the /SUBSYSTEM setting.
That is set when you create your (Visual C++) project based on your choice:
Win32 Console Application
Win32 Project
And I think the latter is the default.
In order to fix things, you need to change your linker setting to match your code (well there could be the other way around too, but that would be more complicated). In order to do that, in VStudio IDE go to your Project Properties -> Linker -> System -> SubSystem and change it from Windows (/SUBSYSTEM:WINDOWS) to Console (/SUBSYSTEM:CONSOLE).
When creating a new MFC application of the simplest kind in Visual Studio 2012, and setting it to link statically to MFC, linking fails.
Here's a gallery of 7 screenshots starting from the creation of the project via the wizard, up to the building (resulting in the linker error).
No source editing was done in-between.
The error logs state:
1>------ Build started: Project: Test, Configuration: Debug Win32 ------
1> stdafx.cpp
1> TestDlg.cpp
1> Test.cpp
1> Generating Code...
1>uafxcwd.lib(afxctrlcontainer2.obj) : error LNK2005: "void __cdecl AfxRegisterMFCCtrlClasses(void)" (?AfxRegisterMFCCtrlClasses##YAXXZ) already defined in afxnmcdd.lib(afxctrlcontainer2.obj)
1>uafxcwd.lib(afxctrlcontainer2.obj) : error LNK2005: "protected: void __thiscall CMFCControlContainer::PreUnsubclassControl(class CWnd *)" (?PreUnsubclassControl#CMFCControlContainer##IAEXPAVCWnd###Z) already defined in afxnmcdd.lib(afxctrlcontainer2.obj)
1>uafxcwd.lib(afxctrlcontainer2.obj) : error LNK2005: "public: int __thiscall CMFCControlContainer::SubclassDlgControls(void)" (?SubclassDlgControls#CMFCControlContainer##QAEHXZ) already defined in afxnmcdd.lib(afxctrlcontainer2.obj)
1>C:\Users\XXXXXXXX\Documents\Visual Studio 2012\Projects\Test\Debug\Test.exe : fatal error LNK1169: one or more multiply defined symbols found
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Is this just me? Would you have a suggestion on how to address this?
I got the same problem when I try static link my project together.
As Michael Burr suggested, it seems fine after commented out the line of #define _AFX_NO_MFC_CONTROLS_IN_DIALOGS in stdafx.h.
I've come across the same issue. If you decide to #define _AFX_NO_MFC_CONTROLS_IN_DIALOGS and get rid of a lot MFC overhead you should also be sure you change the dialog base classes, method calls,etc from CDialogEx to CDialog.
If you don't change the background color or the image of your dialog, you will find that CDialogEx is pretty useless anyway.
After tested, v100 works well and v110 does not work. Try to report to MS connect as this link: http://connect.microsoft.com/VisualStudio/feedback/details/581084/linker-cant-locate-some-external-symbol-when-use-mfc-in-a-static-library
The simple code fails:
#include <Windows.h>
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
MessageBox(NULL, L"Hello World!", L"Just another Hello World program!", MB_ICONEXCLAMATION | MB_OK);
return 0;
}
Errors:
Error 1 error LNK2019: unresolved external symbol main referenced in function __tmainCRTStartup ... Projects\DX11_3\DX11_3\MSVCRTD.lib(crtexe.obj) DX11_3
Error 2 error LNK1120: 1 unresolved externals ... Projects\DX11_3\Debug\DX11_3.exe 1 1 DX11_3
What could be wrong? I've downloaded and installed Windows SDK, added det include folder to the project.
The program's entry point is where execution starts at. For a console application, this defaults to main. For a Windows application with no console, this defaults to WinMain.
The linker is searching for main, most likely because you created a console application. Go into your project settings and change the subsystem to Windows. You can find this option in Configuration Settings -> Linker -> System
Strictly and formally speaking, your program doesn't contain a function named main, so it isn't valid C++.
To enable non-standard extensions like WinMain, you will have to make sure you are creating a Windows project, or that the compiler options are set to compile a Windows program.
you are compiling your application as a "Console Application", so Visual Studio tries to find an entry point named main(). but your code defines a "GUI Application" with an entry point named WinMain().
you should edit your project settings and set the application type to "Console application" in the Linker settings.
i just tried to compile and run a simple example which was found on Programming Windows - Win32 API by Charles Petzold here is the code :
#include <windows.h>
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
MessageBox (NULL, TEXT ("Hello, Windows!"), TEXT ("HelloMsg"), 0) ;
return 0 ;
}
and i got the following error :
Error 1 error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup MSVCRTD.lib(crtexe.obj)
i am using Visual Studio 2010 on Windows 7. how to solve this problem?
is it because i am running some outdated piece of code in new OS if the win32 API for Windows 7 has been changed can anybody suggest me any resources to learn about win32 API for windows 7 with C/C++ or even assembly
There's no doubt this is a duplicate question but i'll answer anyway.
You have created the wrong project type. You need to create a Win32 Project not a Win32 Console Application.