简体   繁体   中英

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

I would like to convert the following program to run on a linux system for my school assignment. I am struggling very hard because the linux computer labs are shutdown due to covid. So, I coded my homework on my windows computer. I had access today and it did not run due to multiple errors. What can I do to convert my code to make it linux compatible?

#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);

}





I cannot imagine you got no warnings on Windows...

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

=> format '%s' expects argument of type 'char ', but argument 2 has type 'char ( )[10]

It should be scanf("%s", response); or even better: scanf("%9s", response);

That is enough to invoke Undefined Behaviour and have the program run smoothly, crash at any moment or provide non consistent output... And the error is repeated in the program...

system("cls");

=> warning: implicit declaration of function 'system'

Not an error of course, but best practices recommend to declare functions before using them. You should use #include <stdlib>

void mazeGo()

=> warning: conflicting types for 'mazeGo'

The identifier was first encountered at mazeGo(); and was implitely declared as int mazeGo() . You should have a void mazeGo(); declaration before main

And a number or unused variables. Here again it is not an error but leaving unused variables in a code could hide typos and make future maintenance harder.

Apart from that, system("cls") will clear the screen only on Windows. On Linux the shell started by system will complain for an unknown command but it should not abort the program.

TL/DR:

  1. Warnings are not to be ignored
  2. If the program had been correct on Windows it would have run on Linux (except for the screen clearing)

Firstly you should add the return type of void to your main function.

void main()...

then you're trying to execute the "cls" command, which in linux will not work, because it's a windows specific command.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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