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.
Related
This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 3 years ago.
I know that simply posting code and asking for a solution isn't a good idea, but I have no idea what's causing this.
I'm trying to find the installation path of PowerPoint based on this code, however, the compiler gives this error:
error LNK2019: unresolved external symbol _MsiLocateComponentW#12 referenced in function _WinMain#16
I'm using Visual Studio 2019 and IntelliSense doesn't notice the error, only the compiler. Here's the code:
#include <Windows.h>
#include <msi.h>
int APIENTRY WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR lpCmdLine, _In_ int nShowCmd) {
LPCWSTR PowerPoint = L"{CC29E94B-7BC2-11D1-A921-00A0C91E2AA2}";
DWORD size = 300;
INSTALLSTATE installstate;
LPWSTR sPath;
sPath = new wchar_t[size];
installstate = MsiLocateComponent(PowerPoint, sPath, &size);
if (installstate == INSTALLSTATE_LOCAL || installstate == INSTALLSTATE_SOURCE)
MessageBox(NULL, sPath, L"PowerPoint path", MB_OK | MB_ICONASTERISK );
delete[] sPath;
return 0;
}
As you can see, I included the msi.h header. What's wrong with my code?
You are getting a linker error, not a compiler error.
Make sure your project is linking to msi.lib. Using msi.h is not enough by itself.
msi.h tells the compiler what the function LOOKS LIKE so that your code can make calls to it.
But you also need to tell the linker WHERE the function is actually located. msi.lib tells the linker that the function is exported from msi.dll, so then the linker can link your function call to that DLL.
To link to msi.lib, you can either specify msi.lib as an "Additional Dependency" for Linker Input in your project options, or you can use a #pragma comment(lib, "msi.lib") statement directly in your code.
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).
We are trying to build a Editor for our Game Engine and so we made a lib from the Engine und link that in the Editor Proj.
The Path for the Lib and the Include Dir are remarked in the Proj. prop.
I tryed it with and without WinAPI but the Link Error still remains.
The Functions which are defined directly in the Header work, but the functions which are defined in the c++ files are creating the link errors. To i have to add the .cpp files in the Proj., isn't the lib exactly for that? -.-
OS: Windows 7 64Bit
Visual Studio 2010
#include "pch.h"
Application* g_pApp = NULL;
INT WINAPI WinMain( __in HINSTANCE hInstance, __in_opt HINSTANCE hPrevInstance, __in LPSTR lpCmdLine, __in int nShowCmd )
{
BlocoApp blocoApp(&g_pApp);
// start engine
Editor mainForm;
if(!g_pApp->Init(L"Bloco", 1600, 900, true, hInstance))
{
g_pApp->Exit();
return 1;
}
// main loop
DXUTMainLoop();
g_pApp->Exit();
return 0;
}
The Compiler Error:
error LNK2001: Nicht aufgelöstes externes Symbol ""public: bool __thiscall Application::Init(class std::basic_string<unsigned short,struct std::char_traits<unsigned short>,class std::allocator<unsigned short> >,unsigned int,unsigned int,bool,struct HINSTANCE__ *,struct HWND__ *)" (?Init#Application##QAE_NV?$basic_string#GU?$char_traits#G#std##V?$allocator#G#2##std##II_NPAUHINSTANCE__##PAUHWND__###Z)".
The Path for the Lib ... are remarked in the Proj. prop
I suspect that you've configured the directory where the library is in the project's "Additional Library Directories" setting. That configures locations where the linker will look for library files it's been told to look for.
However, you also need to tell the linker to actually include the library as an input file. This is done in the IDE using the "Input/Additional Dependencies" property.
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.