Home > Mobile >  How do I run a c executable in a unix environment?
How do I run a c executable in a unix environment?

Time:02-18

If I have a c program file called hello_world.c, I would create an executable out of it using the command gcc -std=c11 -o hello_world.exe hello_world.c on Windows Operating system and then just call the executable like hello_world and windows will execute it, now replicating the same on Unix based operating systems has become quite a challenge because yes I use the command gcc -std=c11 -o hello_world.exe hello_world.c, the terminal does not throw an error but when I try to call the program like hello_world, I get the error that the command hello_world was not found, How do I work around this please?

CodePudding user response:

In a Unix environment, you can compile your C file as:

gcc hello_world.c -o hello_world

where hello_world is the name of compiled output

After compiling you can use the following command to run your program:

./hello_world

CodePudding user response:

I get the error that the command hello_world was not found

You made two mistakes:

  1. If you type some_command in the command prompt in Windows, Windows first searches the command some_command (e.g. some_command.bat or some_command.exe) in the current directory. If it is not found there, Windows searches the directories in the PATH for the command.

    This is not the case in Unix and Linux. Instead, only the directories in the PATH are searched. There are Linux distributions where the directory "." is in the PATH. In this case, Linux will search for some_command in the current directory. However, in Ubuntu 20 (as an example), "." is by default not part of the PATH so Linux will not search for some_command in the current directory.

    You explicitly have to specify the current directory in the command line: ./some_command.

  2. Unlike Windows, Unix and Linux do not know file extensions but the dot (.) is just a "normal" letter.

    An attribute (actually: three attributes) is used to mark a file as being "executable". (You can compare this to the "file is read-only" attribute that can be set for Files on Windows.)

    You can add a suffix (e.g. .exe, .bat, .sh ... or even .txt) to an executable file; but in this case, the dot and the suffix are a part of the file name: If you name your file hello_world.exe, the command is not hello_world but it is hello_world.exe (because the dot (.) is just a "normal" letter for Unix and Linux).

    Personally, I like adding the suffix .x to C-compiled executables and .sh to shell scripts (in Windows you would say: .bat files). However, this is just my personal taste.

CodePudding user response:

In a unix environment, compile your C program with below command:

gcc hello_word.c -o hello_world

In addition, the suffix of executable file does not have any special meaning. Before you run it with command ./hello_world, maybe you needs to add executable permission with command chmod x hello_world.

  • Related