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:
If you type
some_commandin the command prompt in Windows, Windows first searches the commandsome_command(e.g.some_command.batorsome_command.exe) in the current directory. If it is not found there, Windows searches the directories in thePATHfor the command.This is not the case in Unix and Linux. Instead, only the directories in the
PATHare searched. There are Linux distributions where the directory "." is in thePATH. In this case, Linux will search forsome_commandin the current directory. However, in Ubuntu 20 (as an example), "." is by default not part of thePATHso Linux will not search forsome_commandin the current directory.You explicitly have to specify the current directory in the command line:
./some_command.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 filehello_world.exe, the command is nothello_worldbut it ishello_world.exe(because the dot (.) is just a "normal" letter for Unix and Linux).Personally, I like adding the suffix
.xto C-compiled executables and.shto shell scripts (in Windows you would say:.batfiles). 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.
