Creating makefile for C code, running without ./ - c

I need to create a makefile that will compile my simpleprogram.c to sp and it can be called like unix commands like ls,ps etc, without writing explicitly ./sp. I looked upon the web and cannot find a solution, or searching it in a wrong way. I cannot search like "executable without ./" , because I do not know what is this called => "./"

Put the binary in a directory that's in your PATH.
http://www.cs.purdue.edu/homes/cs348/unix_path.html

Just copy your program to your systems bin (executable binaries) directory.
Most commonly its /usr/bin for programs which can be used by all user.
If the app is only for admins, you should use /usr/sbin/ directory.
Remember to set the "executable" flag with chmod: chmod +x your_app

The proper solution to this (assuming you don't want sp to be run from outside of your makefile) is to call your program using the full path name instead of ./ (which is relative, and can change during multi-directory makes). In your makefile do something like:
SP_DIR := $(shell pwd)/spdir
rule : somedependency
$(SP_DIR)/sp
Where $(shell pwd) will expand to the directory the makefile is being run from. If your sp directory is in a parent directory of this, it is possible to use .. in the path as well: eg.
SP_DIR := $(shell pwd)/../../spdir
If you do want to run sp from outside of the makefile, then you need to either copy sp to a directory specified in your PATH variable (do echo $PATH to see these), or modify your .bashrc or equivalent file to make PATH include the directory that sp is built in.
John

You can just do:
export PATH=$PATH:.
But this is not a good idea, in general.

Related

Compiling C file from vim in a "path agnostic" way

Suppose I launch vim from a large project root folder and want to compile a specific example (I ll use zephyr RTOS for example). This root folder is located under /home/<user>/zephyr/
Let's say I run vim samples/basic/blinky/src/main.c.
Now if I want to compile it, I would go, from another terminal to samples/basic/blinky/build/ and run make
If I want to build it without leaving vim, I could run :make -C samples/basic/blinky/build/
I would like to automate this process, pressing any key, let's say f5.
So if I have, for example, two vertical splits, v1 and v2.
In v1 I have samples/basic/blinky/src/main.c and in v2 I have samples/drivers/rtc/src/main.c.
Pressing f5 from v1 would lead to run the equivalent of :make -C samples/basic/blinky/build/, and from v2 would lead to run the equivalent of :make -C samples/drivers/rtc/build/
The common pattern is that the build folder is located in ../build/ from the current c file directory.
I don't want to "permanently" use :cd or :lcd to change working directory, even for the current split/window because:
My ctags tags file is located in the root folder, so I want to be able to jump to any function that samples/basic/blinky/src/main.c uses but are not necessarily defined in the same file.
If I want to open a new file, I want to access it using its path from the root folder and not the current file path
My current solution is to have a function in my ~/.vimrc which temporally changes the working directory to the current file equivalent build folder, so that I can run :make and then changes back the working directory to the root folder.
It looks like this:
nnoremap <F5> :call MakeTst()<CR>
function! MakeTst(...)
:cd %:p:h
:cd ../build/
:make
:cd /home/<user>/zephyr/
endfunction
While this works, the downside is that the root folder is hardcoded inside the ~/.vimrc.
How can I achieve the same result without hardcoding the root folder path?
You gave the answer yourself: in your function. As long as your file
structure keeps that pattern, you can use filename modifiers to make it
generic:
:nnoremap <F5> :make -C %:p:h/../build
This will always build in the directory build at the same level of the
directory where the current file sits. Just like your example:
a/b/c/src/file.c
a/b/c/build
It breaks in a case like this:
a/b/c/src/include/features.h
As it would try to build in:
a/b/c/src/build
There is a workaround though. If your build is always at the same
level of src, then you can perform a text substitution with the :s
modifier:
:nnoremap <F5> :make -C %:p:s!.*\zssrc.*!build!
This simple pattern .*\zssrc.* searches for the last src in your
path and replaces it (and anything after it) with build. It does
nothing if there is no src in the path.

Execute compiled executable C file without ./ [duplicate]

When running scripts in bash, I have to write ./ in the beginning:
$ ./manage.py syncdb
If I don't, I get an error message:
$ manage.py syncdb
-bash: manage.py: command not found
What is the reason for this? I thought . is an alias for current folder, and therefore these two calls should be equivalent.
I also don't understand why I don't need ./ when running applications, such as:
user:/home/user$ cd /usr/bin
user:/usr/bin$ git
(which runs without ./)
Because on Unix, usually, the current directory is not in $PATH.
When you type a command the shell looks up a list of directories, as specified by the PATH variable. The current directory is not in that list.
The reason for not having the current directory on that list is security.
Let's say you're root and go into another user's directory and type sl instead of ls. If the current directory is in PATH, the shell will try to execute the sl program in that directory (since there is no other sl program). That sl program might be malicious.
It works with ./ because POSIX specifies that a command name that contain a / will be used as a filename directly, suppressing a search in $PATH. You could have used full path for the exact same effect, but ./ is shorter and easier to write.
EDIT
That sl part was just an example. The directories in PATH are searched sequentially and when a match is made that program is executed. So, depending on how PATH looks, typing a normal command may or may not be enough to run the program in the current directory.
When bash interprets the command line, it looks for commands in locations described in the environment variable $PATH. To see it type:
echo $PATH
You will have some paths separated by colons. As you will see the current path . is usually not in $PATH. So Bash cannot find your command if it is in the current directory. You can change it by having:
PATH=$PATH:.
This line adds the current directory in $PATH so you can do:
manage.py syncdb
It is not recommended as it has security issue, plus you can have weird behaviours, as . varies upon the directory you are in :)
Avoid:
PATH=.:$PATH
As you can “mask” some standard command and open the door to security breach :)
Just my two cents.
Your script, when in your home directory will not be found when the shell looks at the $PATH environment variable to find your script.
The ./ says 'look in the current directory for my script rather than looking at all the directories specified in $PATH'.
When you include the '.' you are essentially giving the "full path" to the executable bash script, so your shell does not need to check your PATH variable. Without the '.' your shell will look in your PATH variable (which you can see by running echo $PATH to see if the command you typed lives in any of the folders on your PATH. If it doesn't (as is the case with manage.py) it says it can't find the file. It is considered bad practice to include the current directory on your PATH, which is explained reasonably well here: http://www.faqs.org/faqs/unix-faq/faq/part2/section-13.html
On *nix, unlike Windows, the current directory is usually not in your $PATH variable. So the current directory is not searched when executing commands. You don't need ./ for running applications because these applications are in your $PATH; most likely they are in /bin or /usr/bin.
This question already has some awesome answers, but I wanted to add that, if your executable is on the PATH, and you get very different outputs when you run
./executable
to the ones you get if you run
executable
(let's say you run into error messages with the one and not the other), then the problem could be that you have two different versions of the executable on your machine: one on the path, and the other not.
Check this by running
which executable
and
whereis executable
It fixed my issues...I had three versions of the executable, only one of which was compiled correctly for the environment.
Rationale for the / POSIX PATH rule
The rule was mentioned at: Why do you need ./ (dot-slash) before executable or script name to run it in bash? but I would like to explain why I think that is a good design in more detail.
First, an explicit full version of the rule is:
if the path contains / (e.g. ./someprog, /bin/someprog, ./bin/someprog): CWD is used and PATH isn't
if the path does not contain / (e.g. someprog): PATH is used and CWD isn't
Now, suppose that running:
someprog
would search:
relative to CWD first
relative to PATH after
Then, if you wanted to run /bin/someprog from your distro, and you did:
someprog
it would sometimes work, but others it would fail, because you might be in a directory that contains another unrelated someprog program.
Therefore, you would soon learn that this is not reliable, and you would end up always using absolute paths when you want to use PATH, therefore defeating the purpose of PATH.
This is also why having relative paths in your PATH is a really bad idea. I'm looking at you, node_modules/bin.
Conversely, suppose that running:
./someprog
Would search:
relative to PATH first
relative to CWD after
Then, if you just downloaded a script someprog from a git repository and wanted to run it from CWD, you would never be sure that this is the actual program that would run, because maybe your distro has a:
/bin/someprog
which is in you PATH from some package you installed after drinking too much after Christmas last year.
Therefore, once again, you would be forced to always run local scripts relative to CWD with full paths to know what you are running:
"$(pwd)/someprog"
which would be extremely annoying as well.
Another rule that you might be tempted to come up with would be:
relative paths use only PATH, absolute paths only CWD
but once again this forces users to always use absolute paths for non-PATH scripts with "$(pwd)/someprog".
The / path search rule offers a simple to remember solution to the about problem:
slash: don't use PATH
no slash: only use PATH
which makes it super easy to always know what you are running, by relying on the fact that files in the current directory can be expressed either as ./somefile or somefile, and so it gives special meaning to one of them.
Sometimes, is slightly annoying that you cannot search for some/prog relative to PATH, but I don't see a saner solution to this.
When the script is not in the Path its required to do so. For more info read http://www.tldp.org/LDP/Bash-Beginners-Guide/html/sect_02_01.html
All has great answer on the question, and yes this is only applicable when running it on the current directory not unless you include the absolute path. See my samples below.
Also, the (dot-slash) made sense to me when I've the command on the child folder tmp2 (/tmp/tmp2) and it uses (double dot-slash).
SAMPLE:
[fifiip-172-31-17-12 tmp]$ ./StackO.sh
Hello Stack Overflow
[fifi#ip-172-31-17-12 tmp]$ /tmp/StackO.sh
Hello Stack Overflow
[fifi#ip-172-31-17-12 tmp]$ mkdir tmp2
[fifi#ip-172-31-17-12 tmp]$ cd tmp2/
[fifi#ip-172-31-17-12 tmp2]$ ../StackO.sh
Hello Stack Overflow

When your executable behaves differently when invoked with ./ [duplicate]

When running scripts in bash, I have to write ./ in the beginning:
$ ./manage.py syncdb
If I don't, I get an error message:
$ manage.py syncdb
-bash: manage.py: command not found
What is the reason for this? I thought . is an alias for current folder, and therefore these two calls should be equivalent.
I also don't understand why I don't need ./ when running applications, such as:
user:/home/user$ cd /usr/bin
user:/usr/bin$ git
(which runs without ./)
Because on Unix, usually, the current directory is not in $PATH.
When you type a command the shell looks up a list of directories, as specified by the PATH variable. The current directory is not in that list.
The reason for not having the current directory on that list is security.
Let's say you're root and go into another user's directory and type sl instead of ls. If the current directory is in PATH, the shell will try to execute the sl program in that directory (since there is no other sl program). That sl program might be malicious.
It works with ./ because POSIX specifies that a command name that contain a / will be used as a filename directly, suppressing a search in $PATH. You could have used full path for the exact same effect, but ./ is shorter and easier to write.
EDIT
That sl part was just an example. The directories in PATH are searched sequentially and when a match is made that program is executed. So, depending on how PATH looks, typing a normal command may or may not be enough to run the program in the current directory.
When bash interprets the command line, it looks for commands in locations described in the environment variable $PATH. To see it type:
echo $PATH
You will have some paths separated by colons. As you will see the current path . is usually not in $PATH. So Bash cannot find your command if it is in the current directory. You can change it by having:
PATH=$PATH:.
This line adds the current directory in $PATH so you can do:
manage.py syncdb
It is not recommended as it has security issue, plus you can have weird behaviours, as . varies upon the directory you are in :)
Avoid:
PATH=.:$PATH
As you can “mask” some standard command and open the door to security breach :)
Just my two cents.
Your script, when in your home directory will not be found when the shell looks at the $PATH environment variable to find your script.
The ./ says 'look in the current directory for my script rather than looking at all the directories specified in $PATH'.
When you include the '.' you are essentially giving the "full path" to the executable bash script, so your shell does not need to check your PATH variable. Without the '.' your shell will look in your PATH variable (which you can see by running echo $PATH to see if the command you typed lives in any of the folders on your PATH. If it doesn't (as is the case with manage.py) it says it can't find the file. It is considered bad practice to include the current directory on your PATH, which is explained reasonably well here: http://www.faqs.org/faqs/unix-faq/faq/part2/section-13.html
On *nix, unlike Windows, the current directory is usually not in your $PATH variable. So the current directory is not searched when executing commands. You don't need ./ for running applications because these applications are in your $PATH; most likely they are in /bin or /usr/bin.
This question already has some awesome answers, but I wanted to add that, if your executable is on the PATH, and you get very different outputs when you run
./executable
to the ones you get if you run
executable
(let's say you run into error messages with the one and not the other), then the problem could be that you have two different versions of the executable on your machine: one on the path, and the other not.
Check this by running
which executable
and
whereis executable
It fixed my issues...I had three versions of the executable, only one of which was compiled correctly for the environment.
Rationale for the / POSIX PATH rule
The rule was mentioned at: Why do you need ./ (dot-slash) before executable or script name to run it in bash? but I would like to explain why I think that is a good design in more detail.
First, an explicit full version of the rule is:
if the path contains / (e.g. ./someprog, /bin/someprog, ./bin/someprog): CWD is used and PATH isn't
if the path does not contain / (e.g. someprog): PATH is used and CWD isn't
Now, suppose that running:
someprog
would search:
relative to CWD first
relative to PATH after
Then, if you wanted to run /bin/someprog from your distro, and you did:
someprog
it would sometimes work, but others it would fail, because you might be in a directory that contains another unrelated someprog program.
Therefore, you would soon learn that this is not reliable, and you would end up always using absolute paths when you want to use PATH, therefore defeating the purpose of PATH.
This is also why having relative paths in your PATH is a really bad idea. I'm looking at you, node_modules/bin.
Conversely, suppose that running:
./someprog
Would search:
relative to PATH first
relative to CWD after
Then, if you just downloaded a script someprog from a git repository and wanted to run it from CWD, you would never be sure that this is the actual program that would run, because maybe your distro has a:
/bin/someprog
which is in you PATH from some package you installed after drinking too much after Christmas last year.
Therefore, once again, you would be forced to always run local scripts relative to CWD with full paths to know what you are running:
"$(pwd)/someprog"
which would be extremely annoying as well.
Another rule that you might be tempted to come up with would be:
relative paths use only PATH, absolute paths only CWD
but once again this forces users to always use absolute paths for non-PATH scripts with "$(pwd)/someprog".
The / path search rule offers a simple to remember solution to the about problem:
slash: don't use PATH
no slash: only use PATH
which makes it super easy to always know what you are running, by relying on the fact that files in the current directory can be expressed either as ./somefile or somefile, and so it gives special meaning to one of them.
Sometimes, is slightly annoying that you cannot search for some/prog relative to PATH, but I don't see a saner solution to this.
When the script is not in the Path its required to do so. For more info read http://www.tldp.org/LDP/Bash-Beginners-Guide/html/sect_02_01.html
All has great answer on the question, and yes this is only applicable when running it on the current directory not unless you include the absolute path. See my samples below.
Also, the (dot-slash) made sense to me when I've the command on the child folder tmp2 (/tmp/tmp2) and it uses (double dot-slash).
SAMPLE:
[fifiip-172-31-17-12 tmp]$ ./StackO.sh
Hello Stack Overflow
[fifi#ip-172-31-17-12 tmp]$ /tmp/StackO.sh
Hello Stack Overflow
[fifi#ip-172-31-17-12 tmp]$ mkdir tmp2
[fifi#ip-172-31-17-12 tmp]$ cd tmp2/
[fifi#ip-172-31-17-12 tmp2]$ ../StackO.sh
Hello Stack Overflow

Making fopen() open files from a certain directory

I have a function with something like
FILE *file1 = fopen("testing.txt", "r");
I can't modify this line. However, if I make a file named "testing.txt" in, say /tmp, would I be able to make the function load the file from /tmp instead of it's own directory. (Maybe by modifying the PATH variable?)
If the program doesn't change its own working directory, you could cd into /tmp and simply run the program from there.
$ cd /tmp
$ /absolute/path/to/my_program
That opens a file from your current working directory.
You can change the current working directory using chdir.
See this.
This is using C code.
You can also use cd.
For example, go to the terminal:
$ cd /tmp
$ cd /path_to_your_program
Also, cd .. will make you go to the directory above, and cd will make you go to the home directory.
Also, if you do not have the program in the directory in which you have to compile it, you can use cp which copies file.
$ cp /path_to_copy_from /path_to_copy_to
Then you can go to that directory, and run it from there.
I would recommend you to take a basic linux tutorial like this.

Executing a compiled C program

I compiled a silly little "hello world" C program called main.c:
gcc main.c
As expected, a file called a.out appeared, which they say is an executable. From that same directory, if I type
a.out
and hit enter, it says "command not found". But if I type
./a.out
It says "hello world", as desired. I've never seen an executable that requires a './' in front of it to run. Why now?
All executables that aren't in your PATH require an explicit path from root / or the local directory ./ to run. A quick search turns up other threads with essentially the same question:
Why do you need ./ (dot-slash) before script name to run it in bash?
This also has the added benefit of helping with your auto completion in your shell (assuming it supports it). If you type just aTabTab then it will list every executable in your path that starts with "a". However, if you type ./aTab it will probably just auto-complete as a.out since it will only look at executable files in the current directory starting with "a". So, looking at it that way, the "./" actually saves you typing a few keys!
It is standard practice in Unix and Linux not to have the current working directory in the path. If you want to have MSDOS/Windows behavior, alter your PATH variable to include . as the first directory.
It's because the system is looking for a.out or any other exec. file in some special paths. And the current dir in not in that list by default (usually).
look at the list of such paths:
$ env|grep PATH
you can add such current dir to PATH env. variable:
$ export PATH=$PATH:.
But you better avoid doing that and run ./a.out.
Such tech. provides us understanding that we are running specified file from current dir,
not the other file with the same name from another (potentially) dir. So, we know what we run exactly.
When you type something like a.out into a Linux terminal, you're implying that you want to run a command called a.out. By default, the terminal does not look in the current directory for these commands, it looks in PATH - a set of directories for executable programs. It is usually these directories:
/bin
/usr/bin
/usr/local/bin
among others (you can check them all by running echo $PATH)
You have to specifiy the directory directory of your program for it to run, if it is not in one of the directories of PATH. For example:
./a.out works because . refers to the directory you're in
../a.out could work if a.out is in a parent directory (.. refers to the parent)
directory
projectdir/a.out also works, if your program is in the sub-directory, projectdir
That's because a.out is not in your $PATH.
The command you provide is searched in the $PATH (environment variable in linux) by the shell.
$PATH basically is the list of directories. When you provide the executable name, shell searches it in the directories provides by $PATH.
Since a.out is not in your $PATH, you've to explicitly provide the path to a.out.

Resources