-1

I'm new here so sorry if this is a dumb/bad question but I just cannot figure out how to do it for the life of me.

So how would one go about linking a c program to a command. For example lets say I had helloworld.c and I entered make helloword and then wanted to run the command helloworld to run helloworld anywhere on the system. How would I go about this (not using bash aliases).

jww
  • 97,681
  • 90
  • 411
  • 885
  • 5
    This is more a question on the linux environment. There are paths (set up for the current user, and all the users on the system. These are searched for a name ('helloworld') if `helloworld` is placed in a directory which is searched, it will be available to everybody. If these are amended to include a new directory, then helloworld can be added there. – mksteve Feb 18 '18 at 08:24

3 Answers3

0

Compiles the program and link it as an executable to a.out

gcc helloworld.c

Execute the file

./a.out
user9335240
  • 1,739
  • 1
  • 7
  • 14
0

Assuming the helloworld executable is in newDir directory in the home folder.

Then the absolute path for helloworld is /home/user_name/newDir/helloworld

Now add the below line your ~/.bashrc file.

export PATH=$PATH:/home/user_name/newDir/

Now open any new terminal (any directory), you will be able to run the command helloworld

Adding the path of the executable in the PATH environment variable will allow the user to run the executable available in the mentioned directory any where in the system. Check the value of your $PATH with echo $PATH. It probably contains the system directories /usr/bin and /bin where most system executables sit. But don't mess these directories by adding your own programs there (leave them to be handled by your package manager).

You might want to avoid having a too long PATH environment variable; then, assuming you do have a $HOME/bin/ directory mentioned in that $PATH, you could just add a symbolic link there, e.g.

ln -sv /home/user_name/newDir/helloworld $HOME/bin/helloworld
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
Santosh A
  • 5,173
  • 27
  • 37
-1

Well you would be running it using the ./(outputfilename) So to assign a name we use the comment gcc -o desiredOPname filename.c And then run it as ./desiredOPname