繁体   English   中英

如何转换我的 Windows C 程序以在 Linux 系统上运行?

[英]How can I convert my Windows C program to run on a Linux system?

我想将以下程序转换为在 linux 系统上运行以完成我的学校作业。 我正在努力奋斗,因为 linux 计算机实验室因新冠疫情而关闭。 因此,我在 windows 计算机上编写了作业。 我今天可以访问,但由于多个错误而无法运行。 如何转换我的代码以使其与 linux 兼容?

#include <stdio.h>
#include <string.h>






char ch;

#define NUMROWS 6
#define NUMCOLS 6
int i, j;

char str1[42] = {'S', '#', '#', '#', '#', '#', '\n', '.', '.', '.', '.', '.', '#', '\n', '#', '.', '#', '#', '#', '#', '\n', '#', '.', '#', '#', '#', '#', '\n', '.', '.', '.', '#', '.', 'G', '\n', '#', '#', '.','.','.','#','\n'};


main()
{


    
    
    char response[10];
    char response2[10];
    char yes;
    char no;
    char ready;
    char notready;
    
    printf("Do you want to play the maze game? yes|no : \n");
    scanf("%s", &response);
    
    if (!strcmp(response, "yes"))
    {
    printf("\n");
    printf("Game will begin!");
    printf("\n");
    printf("\n");
    
    printf("Maze Board Example: \n");
    printf("S##### \n");
    printf(".....# \n");
    printf("#.#### \n");
    printf("#.#### \n");
    printf("...#.G \n");
    printf("##...# \n");
    printf("\n");
        
    printf("Intstructions:");
    printf("Are you ready to play? ready|no \n");
    scanf("%s", &response);
    
    if (!strcmp(response, "ready"))
    {
        printf("playing...");
        

        system("cls");



        while(1)
        {
            mazeGo();
        }
    }
    

    }
    
    
    
    
    
    if (!strcmp(response, "no"))
    {
        printf("\n");
        printf("Game will exit");

    }
    
    return 0;
    
    
    
    
}

    
    
void mazeGo()
{


    str1[0]= '+';
    printf("%s", str1);
    sleep(1);
    system("cls");
    str1[7] = '+';
    printf("%s", str1);
    sleep(1);
    system("cls");
    str1[8] = '+';
    printf("%s", str1);
    sleep(1);
    system("cls");
    str1[9] = '+';
    printf("%s", str1);
    sleep(1);
    system("cls");
    str1[10] = '+';
    printf("%s", str1);
    sleep(1);
    system("cls");
    str1[11] = '+';
    printf("%s", str1);
    sleep(1);
    system("cls");
    str1[15] = '+';
    printf("%s", str1);
    sleep(1);
    system("cls");
    str1[22] = '+';
    printf("%s", str1);
    sleep(1);
    system("cls");
    str1[28] = '+';
    printf("%s", str1);
    sleep(1);
    system("cls");
    str1[29] = '+';
    printf("%s", str1);
    sleep(1);
    system("cls");
    str1[30] = '+';
    printf("%s", str1);
    sleep(1);
    system("cls");
    str1[37] = '+';
    printf("%s", str1);
    sleep(1);
    system("cls");
    str1[38] = '+';
    printf("%s", str1);
    sleep(1);
    system("cls");
    str1[39] = '+';
    printf("%s", str1);
    sleep(1);
    system("cls");
    str1[32] = '+';
    printf("%s", str1);
    sleep(1);
    system("cls");
    str1[33] = '+';
    printf("%s", str1);
    sleep(1);
    system("cls");
    
    printf("\n You win!");
    
    exit(0);

}





我无法想象您在 Windows 上没有收到任何警告...

...
printf("Do you want to play the maze game? yes|no : \n");
scanf("%s", &response);
...

=> 格式“%s”需要“char ”类型的参数,但参数 2 的类型为“char ( )[10]

应该是scanf("%s", response); 甚至更好: scanf("%9s", response);

这足以调用未定义的行为并让程序顺利运行,随时崩溃或提供不一致的 output ......并且程序中重复错误......

system("cls");

=> 警告:function 'system' 的隐式声明

当然不是错误,但最佳实践建议在使用函数之前声明函数。 您应该使用#include <stdlib>

void mazeGo()

=> 警告:'mazeGo' 的类型冲突

该标识符首先在mazeGo(); 并被隐含地声明为int mazeGo() 你应该有一个void mazeGo(); main 之前的声明

和一个数字或未使用的变量。 这又不是错误,但在代码中留下未使用的变量可能会隐藏拼写错误并使未来的维护更加困难。

除此之外, system("cls")只会在 Windows 上清除屏幕。 在 Linux 上, system启动的 shell 将抱怨未知命令,但不应中止程序。

TL/DR:

  1. 警告不容忽视
  2. 如果程序在 Windows 上是正确的,它将在 Linux 上运行(屏幕清除除外)

首先,您应该将 void 的返回类型添加到您的主 function。

void main()...

然后您尝试执行“cls”命令,该命令在 linux 中将不起作用,因为它是 windows 特定命令。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM