簡體   English   中英

c++ 編譯后的文件在其他電腦上不起作用

[英]c++ compiled file doesn't work in other pcs

我做了一個簡單的C++程序並編譯,我把.exe文件分享給我的朋友們,讓他們也看看。 然而,當在他們的電腦上運行時,它說它需要一些 dll 個文件。 即使在下載 dll 文件后,它也只需要運行 2 秒左右,然后關閉。 順便說一句,他們的電腦上沒有安裝任何編譯器。

編輯:我在編譯時使用了 -static 標志並且它有效。 謝謝您的回答。

#include <iostream>
#include <cstdlib>
#include <ctime>


int toss(int choice); 

void start(int choice); 

void printstats(int runs, int wickets, int balls, int target); 

int makecomputerturnbatting(int turn);

int makecomputerturnbowling(int turn);

int main(int agrc, char *agrv[]) {
    

    int choice; 
    

    do {
        std::cout << "0. Head\n1. Tails\nChoose a valid option: "; 
        std::cin >> choice; 
    } while (choice > 1);

    

    if (toss(choice)) {

        std::cout << "You won the toss.. " << std::endl;

        int choice; 

        do {
            std::cout << "0. Batting\n1. Bowling\nChoose a valid Option: ";
            std::cin >> choice;
        } while (choice > 1);

        start(choice); 

    } else {

        std::cout << "You lost the toss.." << std::endl;

        

        srand(time(0));

        start(rand() % 2);

    }

    return 0;
}

// Defining subroutines

int toss(int choice) {

    int faces[3] = {0, 1, 1}; 

    srand(time(0)); // srand for rand()
    int flippedface = faces[rand() % 4]; 

    if (flippedface == choice) { 
        return 1;
    } else {
        return 0;
    }

}

void start(int choice) {


    if (choice) { 
        std::cout << "\n || Starting game as bowling.. ||" << std::endl;

        int turn; 

        // gamestats
        int runs = 0;
        int balls = 0; 
        int wickets = 5; 
        int target; 



        do {


            std::cout << " || Your turn: ";
            

            do {
                std::cin >> turn;
            } while (turn > 6);


            std::cout << " ||";

            
            int computerturn = makecomputerturnbatting(turn);

            std::cout << "Computer's play: " << computerturn << std::endl;

            if (turn != 0) {
                if (turn == computerturn) {
                    wickets -= 1;
                    balls += 1;

                } else {
                    runs += computerturn;
                    balls += 1;
                }

            } else {

                std::cout << "\n|| Dot Ball! ||\n";
            }

            printstats(runs, wickets, balls, 0);
 
        } while (wickets != 0);

        target = runs;
        runs = 0;
        balls = 0;
        wickets = 5;
        
        // session 2
        do {
            std::cout << " || Playing as batsman now.. ||\n";
            std::cout << " || Your turn: ";
            

            do {
                std::cin >> turn;
            } while (turn > 6);


            std::cout << " ||";

            
            int computerturn = makecomputerturnbowling(turn);

            std::cout << "Computer's play: " << computerturn << std::endl;

            if (computerturn != 0) {
                if (turn == computerturn) {
                    wickets -= 1;
                    balls += 1;

                } else {
                    runs += computerturn;
                    balls += 1;
                }

            } else {

                std::cout << "\n|| Dot Ball! ||\n";
            }

            printstats(runs, wickets, balls, target);
 
        } while (wickets != 0 || runs == target);
        
        if (runs = target) {
            std::cout << "You won!" << std::endl;
        } else {
            std::cout << "You lost.." << std::endl;
        }

        // end of the game..


    } else {
        
        std::cout << "\n || Starting game as batting.. ||" << std::endl;

        int turn; // player's play

        int runs = 0;
        int balls = 0; 
        int wickets = 5; // we will keep it at 5..
        int target;

        // session 1

        do {


            std::cout << " || Your turn: ";
            

            do {
                std::cin >> turn;
            } while (turn > 6);


            std::cout << " ||";

            
            int computerturn = makecomputerturnbowling(turn);

            std::cout << "Computer's play: " << computerturn << std::endl;

            if (computerturn != 0) {
                if (turn == computerturn) {
                    wickets -= 1;
                    balls += 1;

                } else {
                    runs += computerturn;
                    balls += 1;
                }

            } else {

                std::cout << "\n|| Dot Ball! ||\n";
            }

            printstats(runs, wickets, balls, 0);
 
        } while (wickets != 0);

        target = runs;
        runs = 0;
        balls = 0;
        wickets = 5;

        do {


            std::cout << " || Your turn: ";
            

            do {
                std::cin >> turn;
            } while (turn > 6);


            std::cout << " ||";

            // generating computer's play
            
            int computerturn = makecomputerturnbatting(turn);

            std::cout << "Computer's play: " << computerturn << std::endl;

            if (turn != 0) {
                if (turn == computerturn) {
                    wickets -= 1;
                    balls += 1;

                } else {
                    runs += computerturn;
                    balls += 1;
                }

            } else {

                std::cout << "\n|| Dot Ball! ||\n";
            }

            printstats(runs, wickets, balls, target);
 
        } while (wickets != 0 || runs == target);

        if (runs = target) {
            std::cout << "You lost.." << std::endl;
        } else {
            std::cout << "You won!" << std::endl;
        }

    }

}

void printstats(int runs, int wickets, int balls, int target) {
    /*

    ==========

    Runs : int runs
    Wickets : int wickets
    Balls : int balls
    Target : int target

    ==========

    */

    std::cout << "==========" << std::endl;
    std::cout << "          " << std::endl;
    std::cout << " || Runs : " << runs << std::endl;
    std::cout << " || Wickets : " << wickets << std::endl;
    std::cout << " || Balls : " << balls << std::endl;
    if (target) {std::cout<<"|| Target : " << target << std::endl;}
    std::cout << "          " << std::endl;
    std::cout << "==========" << std::endl;

}

int makecomputerturnbatting(int turn) {    



    int computerturn;
    int likelyturn;
    int possibleturns[10] = {0, 0, 1, 2, 3, 4, 5, 6};

    if (turn == 6) {
        likelyturn = turn - 1;
    } else {
        likelyturn = turn + 1;
    }

    possibleturns[9], possibleturns[10] = likelyturn;


    srand(time(0));
    
    computerturn = possibleturns[rand()%11];


    return computerturn;


}

int makecomputerturnbowling(int turn) {

    int computerturn;
    int possibleturns[9] = {0, 1, 2, 3, 4, 5, 6};

    possibleturns[8], possibleturns[9] = turn;


    srand(time(0));
    computerturn = possibleturns[rand()%10];

    return computerturn;

}

您的代碼可能會出現未定義的行為,即越界數組訪問:

int possibleturns[9] = {0, 1, 2, 3, 4, 5, 6};
// ...
computerturn = possibleturns[rand()%10];

rand()%10是 0-9(含)之間的 integer。 您的數組有 9 個元素,索引為 0-8。 如果隨機數結果為 9 mod 10,則會出現未定義的行為。

附加條款:

  • 您不需要在每個rand之前調用srand 在啟動時只調用一次(參見srand() — 為什么只調用一次?
  • possibleturns[8], possibleturns[9] = turn; 不做你期望它做的事。 它只分配給索引 9。你想要possibleturns[8] = possibleturns[9] = turn; 分配給兩者
  • 同樣的問題也出現在makecomputerturnbatting

@mwahi,您似乎在計算機上使用了一些外部庫。 您需要在 output 程序中包含一些 DLL。 如果您使用 Visual Studio 編譯它,則必須包含一些 DLL。 Microsoft 的此頁面在這里解釋了如何將 DLL 直接鏈接到您的 output 程序: https://learn.microsoft.com/en-us/cpp/build/linking-an-executable-to-a-dll?view=msvc- 160

對 DLL 的依賴是預期的,搜索 DLL 的名稱和“可再發行”。 rest 不是預期的,但沒有足夠的信息來診斷問題。

另外,要回答關於“它只打開 2 秒然后關閉”的問題,可能是因為控制台程序顯示了一些內容並關閉了。 告訴你的朋友,從控制台間接運行它,而不是從本地文件夾打開它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM