C++ 函数“int __cdecl invoke_main(void)”(?invoke_main@@YAHXZ)中引用的错误LNK2019未解析的外部符号_main
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33400777/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error LNK2019 unresolved external symbol _main referenced in function "int __cdecl invoke_main(void)" (?invoke_main@@YAHXZ)
提问by Marcel Ceban
Severity Code Description Project File Line Error LNK2019 unresolved external symbol _main referenced in function "int __cdecl invoke_main(void)" (?invoke_main@@YAHXZ) Severity Code Description Project File Line Error LNK1120 1 unresolved externals
严重性代码描述项目文件行错误 LNK2019 未解析的外部符号 _main 在函数“int __cdecl invoke_main(void)”(?invoke_main@@YAHXZ) 中引用 严重性代码描述项目文件行错误 LNK1120 1 个未解析的外部
#include "windows.h"
#include "tchar.h"
#include "d3d9.h"
#pragma comment(lib, "d3d9.lib")
LPDIRECT3D9 pDirect3D=NULL;
LPDIRECT3DDEVICE9 pDirect3DDevice=NULL;
const int segment = 50;
const int NV = segment*13;
struct CUSTOMVERTEX
{
float x, y, z, rhv;
DWORD color;
};
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZRHW|D3DFVF_DIFFUSE)
LPDIRECT3DVERTEXBUFFER9 pVertexBuffer=NULL;
HRESULT InitialDirect3D(HWND hvnd)
{
if((pDirect3D=Direct3DCreate9(D3D_SDK_VERSION))==NULL)
return E_FAIL;
D3DDISPLAYMODE Display;
if(FAILED(pDirect3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &Display)))
return E_FAIL;
D3DPRESENT_PARAMETERS Direct3DParameter;
ZeroMemory(&Direct3DParameter, sizeof Direct3DParameter);
Direct3DParameter.Windowed=TRUE;
Direct3DParameter.SwapEffect=D3DSWAPEFFECT_DISCARD;
Direct3DParameter.BackBufferFormat=Display.Format;
if(FAILED(pDirect3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hvnd,
D3DCREATE_HARDWARE_VERTEXPROCESSING,
&Direct3DParameter, &pDirect3DDevice)))
return E_FAIL;
return S_OK;
}
HRESULT RenderingDirect3D()
{
if(pDirect3DDevice==NULL)
return E_FAIL;
pDirect3DDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(90, 150, 100), 1.f, 0);
pDirect3DDevice->BeginScene();
pDirect3DDevice->SetStreamSource(0, pVertexBuffer, 0, sizeof(CUSTOMVERTEX));
pDirect3DDevice->SetFVF(D3DFVF_CUSTOMVERTEX);
pDirect3DDevice->DrawPrimitive(D3DPT_LINELIST, 0, NV);
pDirect3DDevice->EndScene();
pDirect3DDevice->Present(NULL, NULL, NULL, NULL);
return S_OK;
}
void DeleteDirect3D()
{
if(pVertexBuffer)
pVertexBuffer->Release();
if(pDirect3DDevice)
pDirect3DDevice->Release();
if(pDirect3D)
pDirect3D->Release();
}
HRESULT InitialVertexBuffer()
{
float u = 0.0;
int z = 0;
float points[][2] = {
{150,150},
{125,100},
{150,50},
{200,50},
{225,55},
{285,60},
{325,25},
{342,30},
{340,35},
{340,40},
{325,75},
{300,125},
{300,175},
{305,250},
{295,287},
{257,347},
{200,350},
{150,325},
{140,290},
{142,282},
{160,280},
{165,286},
{175,325},
{215,340},
{250,335},
{275,300},
{275,250},
{255,200},
{250,150},
{275,100},
{305,65},
{275,85},
{240,95},
{215,85},
{170,65},
{140,90},
{160,135},
{160,150},
{152.5,150},
{150,150}
};
CUSTOMVERTEX Vertexes[NV*2];
int i = 0;
while( i < NV*2 )
{
u = 0.f;
for(int j = 0; j < segment; j++)
{
Vertexes[i].x = (1-3*u+3*u*u-u*u*u)*points[z][0]+(3*u-6*u*u+3*u*u*u)*points[z+1][0]+(3*u*u-3*u*u*u)*points[z+2][0]+(u*u*u)*points[z+3][0];
Vertexes[i].y = (1-3*u+3*u*u-u*u*u)*points[z][1]+(3*u-6*u*u+3*u*u*u)*points[z+1][1]+(3*u*u-3*u*u*u)*points[z+2][1]+(u*u*u)*points[z+3][1];
Vertexes[i].z = 0.5f;
Vertexes[i].color = 0x00ffffff;
Vertexes[i].rhv = 1.f;
u += (float)1/segment;
i++;
Vertexes[i].x = (1-3*u+3*u*u-u*u*u)*points[z][0]+(3*u-6*u*u+3*u*u*u)*points[z+1][0]+(3*u*u-3*u*u*u)*points[z+2][0]+(u*u*u)*points[z+3][0];
Vertexes[i].y = (1-3*u+3*u*u-u*u*u)*points[z][1]+(3*u-6*u*u+3*u*u*u)*points[z+1][1]+(3*u*u-3*u*u*u)*points[z+2][1]+(u*u*u)*points[z+3][1];
Vertexes[i].z = 0.5f;
Vertexes[i].color = 0x00ffffff;
Vertexes[i].rhv = 1.f;
i++;
}
z += 3;
}
if(FAILED(pDirect3DDevice->CreateVertexBuffer(NV*2*sizeof(CUSTOMVERTEX), 0, D3DFVF_CUSTOMVERTEX,
D3DPOOL_DEFAULT, &pVertexBuffer, NULL)))
return E_FAIL;
void *pVB=NULL;
if(FAILED(pVertexBuffer->Lock(0, sizeof Vertexes, (void**)&pVB, 0)))
return E_FAIL;
memcpy(pVB, Vertexes, sizeof Vertexes);
pVertexBuffer->Unlock();
return S_OK;
}
LRESULT CALLBACK MainWinProc(HWND hwnd,
UINT msg,
WPARAM wparam,
LPARAM lparam)
{
switch(msg)
{
//
case WM_PAINT:
RenderingDirect3D();
ValidateRect(hwnd, NULL);
break;
case WM_DESTROY:
DeleteDirect3D();
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, msg, wparam, lparam);
}
int WINAPI WinMain(HINSTANCE hinstance,
HINSTANCE hprevinstance,
LPSTR lpcmdline,
int ncmdshow)
{
WNDCLASSEX windowsclass;
HWND hwnd;
MSG msg;
//
windowsclass.cbSize=sizeof(WNDCLASSEX);
windowsclass.style=CS_DBLCLKS|CS_OWNDC|CS_HREDRAW|CS_VREDRAW;
windowsclass.lpfnWndProc=MainWinProc;
windowsclass.cbClsExtra=0;
windowsclass.cbWndExtra=0;
windowsclass.hInstance=hinstance;
windowsclass.hIcon=LoadIcon(NULL, IDI_APPLICATION);
windowsclass.hCursor=LoadCursor(NULL, IDC_ARROW);
windowsclass.hbrBackground=(HBRUSH)GetStockObject(GRAY_BRUSH);
windowsclass.lpszMenuName=NULL;
windowsclass.lpszClassName=_T("WINDOWSCLASS");
windowsclass.hIconSm=LoadIcon(NULL, IDI_APPLICATION);
if(!RegisterClassEx(&windowsclass))
return 0;
if(!(hwnd=CreateWindowEx(
NULL,
_T("WINDOWSCLASS"),
_T("Desenam litera G"),
WS_OVERLAPPEDWINDOW|WS_VISIBLE,
CW_USEDEFAULT, 0,
CW_USEDEFAULT, 0,
NULL,
NULL,
hinstance,
NULL)))
return 0;
if(SUCCEEDED(InitialDirect3D(hwnd)))
{
if(SUCCEEDED(InitialVertexBuffer()))
{
ShowWindow(hwnd, SW_SHOWDEFAULT);
UpdateWindow(hwnd);
ZeroMemory(&msg, sizeof msg);
while(msg.message!=WM_QUIT)
{
if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
RenderingDirect3D();
}
}
}
return msg.wParam;
}
回答by Michael Nastenko
Check project configuration. Linker->System->SubSystemshould be Windows.
检查项目配置。Linker-> System-> SubSystem应该是Windows。
回答by Hans One
回答by Anand j. Kadhi
I had same Problem when i was trying to create executable from program that having no main() method. When i included sample main() method like this
当我尝试从没有 main() 方法的程序创建可执行文件时,我遇到了同样的问题。当我包含这样的示例 main() 方法时
int main(){
return 0;
}
It solved
它解决了
回答by u4869482
I faced the same problem too and I found out that I selected "new Win32 application" instead of "new Win32 console application". Problem solved when I switched. Hope this can help you.
我也遇到了同样的问题,我发现我选择了“新的 Win32 应用程序”而不是“新的 Win32 控制台应用程序”。当我切换时问题解决了。希望这可以帮到你。
回答by Jeff Gros
Right click on project. Properties->Configuration Properties->General->Linker.
右键单击项目。属性->配置属性->常规->链接器。
I found two options needed to be set. Under System: SubSystem = Windows (/SUBSYSTEM:WINDOWS) Under Advanced: EntryPoint = main
我发现需要设置两个选项。在系统下:子系统 = Windows (/SUBSYSTEM:WINDOWS) 在高级下:EntryPoint = main
回答by Nicolas Holthaus
This is an edge case, but you can also get this error if you are building an MFC application with CMake.
这是一个边缘情况,但如果您使用 CMake 构建 MFC 应用程序,您也可能会收到此错误。
In that case, you need to add the following definitions:
在这种情况下,您需要添加以下定义:
ADD_DEFINITIONS(-D_AFXDLL)
SET(CMAKE_MFC_FLAG 2) # or 1 if you are looking for the static library
ADD_DEFINITIONS(-D_AFXDLL)
SET(CMAKE_MFC_FLAG 2) # or 1 if you are looking for the static library
If you are compiling with unicode, the following properties also need to be added:
如果使用unicode编译,还需要添加以下属性:
set_target_properties(MyApp PROPERTIES
COMPILE_DEFINITIONS
_AFXDLL,_UNICODE,UNICODE,_BIND_TO_CURRENT_CRT_VERSION,_BIND_TO_CURRENT_MFC_VERSION
LINK_FLAGS "/ENTRY:\"wWinMainCRTStartup\""
)
set_target_properties(MyApp PROPERTIES
COMPILE_DEFINITIONS
_AFXDLL,_UNICODE,UNICODE,_BIND_TO_CURRENT_CRT_VERSION,_BIND_TO_CURRENT_MFC_VERSION
LINK_FLAGS "/ENTRY:\"wWinMainCRTStartup\""
)
Soure: FAQ: How to use MFC with CMake
Soure:常见问题解答:如何在 CMake 中使用 MFC
回答by daniel
just add:
只需添加:
int main()
{
//you can leave it empty
return 0;
}
to your project and everything will work its causes because of visual studio try to find the main function to start the project and didn't found it
到您的项目,一切都会正常工作,因为 Visual Studio 尝试找到启动项目的主要功能,但没有找到它
回答by Prashant Singh
Select the project. Properties->Configuration Properties->Linker->System.
选择项目。属性->配置属性->链接器->系统。
My problem solved by setting below option. Under System: SubSystem = Console(/SUBSYSTEM:CONSOLE)
通过设置以下选项解决了我的问题。系统下:子系统=控制台(/SUBSYSTEM:CONSOLE)
Or you can choose the last option as "inherite from the parent".
或者您可以选择最后一个选项作为“从父级继承”。
回答by langlauf.io
Similar to @仲耀晖 I had the wrong application type configured for a dll. I guess that the project type changed due to some bad copy pasting, as @Daniel Struhl suggested.
与@仲耀晖类似,我为dll配置了错误的应用程序类型。正如@Daniel Struhl 所建议的那样,我猜想项目类型由于某些错误的复制粘贴而发生了变化。
How to verify:
Right click on the project -> properties
-> Configuration Properties
-> General
-> Project Defaults
-> Configuration Type
.
如何验证:右键单击项目 -> properties
-> Configuration Properties
-> General
-> Project Defaults
-> Configuration Type
。
Check if this field contains the correct type, e.g. "Dynamic Library (.dll)" in case the project is a dll.
检查此字段是否包含正确的类型,例如“动态库 (.dll)”,以防项目是 dll。
回答by Brian Tung
This worked for me:
这对我有用:
(I don't have enough rep to embed pictures yet -- sorry about this.)
(我还没有足够的代表来嵌入图片——对此很抱歉。)
I went into Project --> Properties --> Linker --> System.
我进入了项目 --> 属性 --> 链接器 --> 系统。
IMG: Located here, as of Dec 2019 Visual Studio for Windows
IMG:位于此处,截至 2019 年 12 月 Visual Studio for Windows
My platform was set to Active(Win32) with the Subsystem as "Windows". I was making a console app, so I set it to "Console".
我的平台设置为 Active(Win32),子系统为“Windows”。我正在制作一个控制台应用程序,所以我将它设置为“控制台”。
IMG: Changing "Windows" --> "Console"
Then, I switched my platform to "x64".
然后,我将平台切换到“x64”。