简体   繁体   中英

Run C program on unix not in command line

It might be my code, but I can compile and run this program via command line. In windows the exe file is also runnable. However, I cannot run the compiled code outside of the terminal on any unix systems (ubuntu or osx) I am pretty new to C so any help would be appreciated. Thank you!

Clarification:

In Ubuntu I run

gcc game.c -o game
./game

And then it runs perfectly. But if I go through the GUI to the game file and double click it, it doesn't do anything. I'm used to on Windows it would bring up a command and run the program as if you ran it from cmd .

Code (it is a simple number guessing game):

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int guessed = 0;
int guesses = 0;
int total_guesses = 0;
int games_played = 0;
int range_top = 10;
int generate_random_number()
{
    srand(time(NULL));
    return (rand() % range_top);
}
void take_guess(int num)
{
    int guess;
    printf("what is your guess:  ");
    scanf(" %i",&guess);
    if(guess == num)
    {
        guessed = 1;
    }
    else if(guess>num)
    {
        printf("Your guess was too high,\n");
    }
    else 
    {
        printf("Your guess was too low.\n");
    }

}
void print_stats()
{
        printf("\n\n\nGames Played: %i\nTotal Guesses: %i\nAverage Guesses Per Game: %f\n\n\n\n",games_played,total_guesses,(double)total_guesses/games_played);
        int i = 5;
        while(i>0)
        {
                printf("exiting in %is\n",i);
                i--;
                sleep(1);
        }
}
int main(void)
{
    printf("This is a game in C\n");
    printf("Would you like to play? (y/n): ");
    char play;
    scanf("%c",&play);
    while(play == 'y')
    {
        printf("I am thinking of a number between 0 and %i\n",range_top);
        int num = generate_random_number();
        while(guessed ==0)
        {
            take_guess(num);
            guesses++;
        }
        games_played++;
        total_guesses+=guesses;
        printf("It took you %i guesses to win.\n",guesses);
        printf("Would you like to play again? (y/n): ");
        scanf(" %c",&play);
        guessed = 0;
        guesses = 0;
    }
    print_stats();
}

But if I go through the GUI to the game file and double click it, it doesn't do anything

How do you know that it doesn't run? I bet it does run, but as it is not run in context of any terminal (command line window), you just can't see its output. That's how Linux (and Unix in general) work.

Windows differentiates GUI and commandline applications, and in the latter case automatically brings a console window. Unfortunately (fortunately?), that's not the case in Unix.

If you want to achieve Windows behavior, you could:

  1. Create a launcher for your application . That will allow you to specify custom icon etc.

  2. Create a shell script that will invoke teminal and your application inside of it:

game.sh :

#!/bin/bash
gnome-terminal -e "./game"

Please note that not everyone will have gnome-temrinal installed, so you may have to adjust your script to support more terminal emulators ( xterm , rxvt , maybe konsole for KDE users).

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