我對C++很陌生,我試圖用Visual C++ Express 2010創建一個簡單的窗體應用程序。當它只是一個頭文件和一個.cpp文件,它在我的電腦上運行得非常好。當我試圖讓我的朋友編譯.exe運行時,它沒有運行。他有.NET框架4.5和Visual C++ 2010可再發行組件包,但仍然拒絕工作。他說它根本不會啓動(我不知道它是否給了他一個錯誤信息)。所有的程序都是一個改變標籤的按鈕。我在這裏把我的頭髮扯出來,因爲我似乎無法讓它在任何其他計算機上工作,只能是我自己的。請幫我理解爲什麼這不會在另一臺計算機上運行。這可能是我犯的一個錯誤,所以代碼在下面。正如你所看到的,我已經將它轉換成3個.cpp文件來查看這是否可行。這裏有很多無意義的代碼,但它仍然編譯得很好。Visual C++窗體應用程序不能在其他計算機上運行
//Source2.cpp
#pragma once
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 Form1
/// </summary>
public ref class Header1 : public System::Windows::Forms::Form
{
public:
Header1(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
}
protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~Header1()
{
if (components)
{
delete components;
}
}
public: System::Windows::Forms::Button^ button1;
System::Windows::Forms::Label^ label1;
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->button1 = (gcnew System::Windows::Forms::Button());
this->label1 = (gcnew System::Windows::Forms::Label());
this->SuspendLayout();
//
// button1
//
this->button1->Location = System::Drawing::Point(13, 13);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(75, 23);
this->button1->TabIndex = 0;
this->button1->Text = L"button1";
this->button1->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this, &Header1::button1_Click);
//
// label1
//
this->label1->AutoSize = true;
this->label1->Location = System::Drawing::Point(13, 43);
this->label1->Name = L"label1";
this->label1->Size = System::Drawing::Size(46, 17);
this->label1->TabIndex = 1;
this->label1->Text = L"label1";
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(8, 16);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(282, 253);
this->Controls->Add(this->label1);
this->Controls->Add(this->button1);
this->Name = L"Form1";
this->Text = L"Form1";
this->ResumeLayout(false);
this->PerformLayout();
}
#pragma endregion
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
this->label1->Text = "works";
}
};
^^請注意,這是最初叫那麼header1.h
//Source1.cpp
#include "stdafx.h"
#include "Source2.cpp"//used to include Header1.h
public class runpr
{
public:
runpr()
{
create();
}
~runpr();
private:
void create()
{
// Enabling Windows XP visual effects before any controls are created
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
// Create the main window and run it
Application::Run(gcnew Header1());
//^^all of this was originally in the main function
}
};
此文件中的頭文件是很沒有意義的,因爲所有的東西它可能只是在主函數或類來完成甚至可以在主項目文件中。我只是試圖看看它是否會起作用,但無濟於事。
// Testing123.cpp : main project file.
#include "stdafx.h"
#include "Source1.cpp"
[STAThreadAttribute]
int main(array<System::String ^> ^args)
{
runpr* start = new runpr;
//creates an instance of runpr class
return 0;
}
再次,任何幫助,將不勝感激。對不起,如果這太長和愚蠢的一個問題
你在發佈模式下編譯?目標機器上是否安裝了VC++ 2010 SP1?你可以將可執行文件添加到文章中嗎? – Tobias
@Tobias感謝您的回覆。問題是我沒有在發佈模式下編譯。簡直不敢相信它是如此簡單。我應該可以學習更多關於Visual C++的知識...... – foolsworld
太棒了。我在答案中添加了解決方案。 – Tobias