_tWinMain in static lib LNK2019 - c++

I try put main(WinMain) in static library:
#include <tchar.h>
#include <Windows.h>
int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPTSTR lpCmdLine, int nCmdShow) {...}
but I got:
MSVCRTD.lib(crtexew.obj) : error LNK2019: unresolved external symbol _WinMain#16 referenced in function ___tmainCRTStartup
I tried to build as "Unicode", and as "Multi-byte".
I tried write without "_t" (and LPSTR).
I tried write extern "C".
Linker flags include /SUBSYSTEM:WINDOWS and my static lib
I know that it's possible, because it's done in libraries like SDL, SFML, etc.

Your problem is that the function in the lib is called _tWinMain.
Just call it WinMain and you are good to go.

Related

unresolved external symbol _MsiLocateComponentW#12 [duplicate]

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.

Linker error unresolved external symbol with only wWinMain MSVC 2019

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.

Cannot find solution for Unresolved external symbol _wWinMain#16

I am sorry if this is a repetitive question but I feel like I have done everything in every answer that I have found for this problem. I am trying to follow the tutorial on Microsoft's website for creating a Windows Desktop Application and I ran into this. I have checked to see if I made the correct project and I have checked if the subsystem is set to Windows. This is my code.
#include <Windows.h>
#include <tchar.h>
int CALLBACK WinMain(
_In_ HINSTANCE hInstance,
_In_ HINSTANCE hPrevInstance,
_In_ LPSTR lpCmdLine,
_In_ int nCmdShow
);
LRESULT CALLBACK WndProc(
_In_ HWND hwnd,
_In_ UINT uMsg,
_In_ WPARAM wParam,
_In_ LPARAM lParam
);`
And here is my error:
Error LNK2019 unresolved external symbol _wWinMain#16 referenced in
function "int __cdecl invoke_main(void)" (?invoke_main##YAHXZ)
TextEditor C:\Users\jcoch\Source\Repos\TextEditor\TextEditor
\MSVCRTD.lib(exe_wwinmain.obj)
For some reason used entry point and main function does not match. Use wWinMain instead WinMain or set WinMainCRTStartup property in Project Properties -> Linker -> Entry Point (or just add the line #pragma comment(linker, "/ENTRY:WinMainCRTStartup"to code)

Link Error with selfmade Lib

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.

Unresolved external symbol fail in VERY simple application

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.