MFC(1):第一个MFC程序创建及遇见的问题解决

xiaoxiao2022-05-17  23

1 项目创建及代码编写

(1)创建一个win32空项目,创建完成后,右键属性-->项目默认值-->MFC的使用-->在共享 DLL 中使用 MFC-->确定

(2)编写头文件mfc.h

#include <afxwin.h>

class CMyApp : public CWinApp { public:     //MFC程序的入口函数     //基类的函数,子类继承重写     virtual BOOL InitInstance(); };

class CMyFrame : public CFrameWnd {     public:          CMyFrame(); };

(3)创建源文件mfc.cpp

#include "mfc.h"

//创建唯一的应用程序对象 CMyApp app;

BOOL CMyApp::InitInstance() {     //创建框架类对象     CMyFrame* myFrame = new CMyFrame;     myFrame->ShowWindow(SW_SHOWNORMAL);     myFrame->UpdateWindow();

    m_pMainWnd = myFrame;

    return TRUE; }

CMyFrame::CMyFrame() {     Create(NULL, TEXT("MyWindow")); }

 

2 遇到问题及解决办法

(1)IntelliSense: #error 指令: Please use the /MD switch for _AFXDLL builds

解决办法:

工程(Project)-> 属性(Properties)-> 配置属性(Configuration Properties)-> c/c++-> 

代码生成(Code Generation)->运行库(Use run-time library)->多线程调试DLL(/MD)

 

(2) LNK4098: 默认库“MSVCRT”与其他库的使用冲突;请使用 /NODEFAULTLIB:library  

方案一:【项目】->【属性】->【配置属性】->【连接器】->【输入】->【忽略指定库】,输入:msvcrt.lib

方案二:【项目】->【属性】->【配置属性】->【连接器】->【命令行】,输入:/NODEFAULTLIB:msvcrt.lib

 

3 使用win32控制台创建mfc项目时遇到的问题分析

errorLNK2019: 无法解析的外部符号 _WinMain@16,该符号在函数 ___tmainCRTStartup 中被引用

因为控制台应用程序的入口(main)与Win32程序的入口(WinMain)是不一致,因此导致该错误。

解决方法:

【项目】->【属性】->【配置属性】-> C/C++ ->预处理器 -> 预处理器定义 ,设置为WIN32;_DEBUG;_WINDOWS;%(PreprocessorDefinitions);

【项目】->【属性】->【配置属性】-> 【连接器】 -> 【系统】 -> 【子系统】的/subsystem:console改为/subsystem:windows.

转载请注明原文地址: https://www.6miu.com/read-4884256.html

最新回复(0)