繁体   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