error LNK2019: 无法解析的外部符号 _main,该符号在函数 ___tmainCRTStartup 中被引用

问题描述:


最近在学习MFC,学习windows的API函数。
在按网络上的视频教程用全手写的方式创建窗口的时候出现Bug。

源码
#include <Windows.h>

HINSTANCE hinst;//实例句柄太重要了,要用一个全局变量将其保存起来
HWND hwnd;

LRESULT CALLBACK MainWndProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam);


int WINAPI WinMain(HINSTANCE hinstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{
	//定义窗口类
	WNDCLASSEX wcx;
	MSG msg;
	BOOL fGotMessage;

	hinst=hinstance;
	wcx.lpszClassName = "MainWClass";

	wcx.cbSize = sizeof(wcx);

	wcx.style			= CS_HREDRAW | CS_VREDRAW;
	wcx.lpfnWndProc	= MainWndProc;
	wcx.cbClsExtra		= 0;
	wcx.cbWndExtra		= 0;
	wcx.hInstance		= hinstance;
	wcx.hIcon			= LoadIcon(NULL, IDI_APPLICATION);
	wcx.hCursor		= LoadCursor(NULL, IDC_ARROW);
	wcx.hbrBackground	= (HBRUSH)GetStockObject(WHITE_BRUSH);
	wcx.lpszMenuName	= NULL;
	wcx.hIconSm		= (HICON)LoadImage(hinstance,
		MAKEINTRESOURCE(5),
		IMAGE_ICON,
		GetSystemMetrics(SM_CXSMICON),
		GetSystemMetrics(SM_CYSMICON),
		LR_DEFAULTCOLOR);
	if(!RegisterClassEx(&wcx))
	{
		return 1;
	}

	//使用窗口类创建窗口

	hwnd = CreateWindow("MainWClass",
		"Hello!",
		WS_OVERLAPPEDWINDOW,
		CW_USEDEFAULT,
		CW_USEDEFAULT,
		CW_USEDEFAULT,
		CW_USEDEFAULT,
		(HWND)NULL,
		(HMENU)NULL,
		hinstance,
		(LPVOID)NULL);

	if(!hwnd)
	{
		return 1;
	}

	ShowWindow(hwnd,nCmdShow);
	UpdateWindow(hwnd);//ShowWindow之后要马上UpdateWindow一下,否则窗口显示不出来

	while((fGotMessage = GetMessage(&msg,(HWND)NULL,0,0)) != 0)
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}

	return msg.wParam;
}

//窗口过程函数,处理消息
LRESULT CALLBACK MainWndProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
	switch(uMsg)
	{
	case WM_DESTROY:
		PostQuitMessage(0);
		return 0;
	case WM_LBUTTONDOWN:
		MessageBox(hwnd,"Hello 窗口!","你好",MB_OK);
		return 0;
	default:
		return DefWindowProc(hwnd,uMsg,wParam,lParam);
	}
}


错误提示
1>------ 已启动生成: 项目: test, 配置: Debug Win32 ------
1>生成启动时间为 2017/1/1 星期日 16:01:39。
1>InitializeBuildStatus:
1>  正在创建“Debug\test.unsuccessfulbuild”,因为已指定“AlwaysCreate”。
1>ClCompile:
1>  所有输出均为最新。
1>ManifestResourceCompile:
1>  所有输出均为最新。
1>Link:
1>  所有输出均为最新。
1>MSVCRTD.lib(crtexe.obj) : error LNK2019: 无法解析的外部符号 _main,该符号在函数 ___tmainCRTStartup 中被引用
1>C:\Users\Administrator\Documents\Visual Studio 2010\Projects\test\Debug\test.exe : fatal error LNK1120: 1 个无法解析的外部命令
1>
1>生成失败。
1>
1>已用时间 00:00:00.34
========== 生成: 成功 0 个,失败 1 个,最新 0 个,跳过 0 个 ==========


问题分析:


因为Win32 console Application的入口函数是Main(),而Win32 Application的入口函数才是WinMain()


解决方案:

右键项目,打开【属性】页,【 配置属性】——【 链接器】——【 系统】中的【 子系统】改为【 窗口 (/SUBSYSTEM:WINDOWS)】 ,如下图所示:


再次编译即可过通过测试!

  • 8
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值