Visual Express 2012 C++ error LNK2001

Visual Express 2012 C++ error LNK2001

本文关键字:error LNK2001 C++ 2012 Express Visual      更新时间:2023-10-16

我知道这是一个常见的错误。我已经试着解决这个问题一个星期了。我正在使用Visual Express 2012和c++编码。我开始一个新的,但空的,win32应用程序。然后向项目添加一个UI表单。在解决方案中有一个头文件和我正在使用的cpp文件。当我尝试编译时,我得到一个错误LNK2001和LNK1120。

错误1错误LNK2001:未解析的外部符号__tWinMain c: users me desktop visual c++ workspacewinFormExp01LINK winFormExp01

错误2错误LNK1120: 1 unresolved外部c: users me desktop visual c++工作区 winFormExp01 调试 winFormExp01.exe winFormExp01

头文件:

#pragma once
namespace winFormExp01 {
    using namespace System;
    using namespace System::ComponentModel;
    using namespace System::Collections;
    using namespace System::Windows::Forms;
    using namespace System::Data;
    using namespace System::Drawing;
    /// <summary>
    /// Summary for MyForm
    /// </summary>
    public ref class MyForm : public System::Windows::Forms::Form
    {
    public:
        MyForm(void)
        {
            InitializeComponent();
            //
            //TODO: Add the constructor code here
            //
        }
    protected:
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        ~MyForm()
        {
            if (components)
            {
                delete components;
            }
        }
    private:
        /// <summary>
        /// Required designer variable.
        /// </summary>
        System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        void InitializeComponent(void)
        {
            this->SuspendLayout();
            // 
            // MyForm
            // 
            this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
            this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
            this->ClientSize = System::Drawing::Size(284, 262);
            this->Name = L"MyForm";
            this->Text = L"MyForm";
            this->Load += gcnew System::EventHandler(this, &MyForm::MyForm_Load);
            this->ResumeLayout(false);
        }
#pragma endregion
    private: System::Void MyForm_Load(System::Object^  sender, System::EventArgs^  e) {
             }
    };
}
cpp文件:
#include "MyForm.h"
using namespace System;
using namespace System::Windows::Forms;
[STAThread]
int Main(array<String^>^ args)
{
    Application::EnableVisualStyles();
    Application::SetCompatibleTextRenderingDefault(false);
    winFormExp01::MyForm form;
    Application::Run(%form);
    return 0;
}

我试图得到一个基本的windows应用程序运行与尽可能少的脂肪。非常感谢所有的帮助。谢谢!

你必须使用WinMain而不是main。

我不确定WinMain应该是什么样子,但是如果你从模板中创建一个新的"Windows应用程序",或者"Windows窗体"项目,希望其中一个会有它,你可以复制WinMain函数减速。

你可以试试这个吗?

void __stdcall WinMain(HINSTANCE hInstance,
             HINSTANCE hPrevInstance,
          long lpCmdLine,
          int nCmdShow)

起始地址是C运行时库中的函数名。链接器根据程序的属性选择它。

mainCRTStartup (or wmainCRTStartup) An application using /SUBSYSTEM:CONSOLE; 
calls main (or wmain)
WinMainCRTStartup (or wWinMainCRTStartup) An application using /SUBSYSTEM:WINDOWS; 
calls WinMain (or wWinMain), which must be defined with __stdcall

那么,链接器在你的程序中寻找WinMain,但是它找不到并且抱怨