gcc is not using /tmp for temporary files - linux

I am using Ubuntu Linux.
gcc is not using /tmp for temporary files (.i, .o, .s).
I exported environment variable TMPDIR like this:
export TMPDIR=/tmp
however gcc still tries to create tmp files in ./
so, if there is no W permission, gcc fails.
So I want to fix the gcc temporary file path to /tmp/
however, gcc is ignoring the TMPDIR.
What would be the problem here?

Adding the:
export TMPDIR=$HOME/temp;
line to ~/.profile file and then running:
. ~/.profile
command followed by a system restart did the trick for me in case anyone comes across this.

Related

Path, /usr/bin/ and /usr/local/bin/

I installed watchr on OS X (10.8.3) using gem install watchr. And it's installed in /usr/bin/watchr
$ which watchr
/usr/bin/watchr
However, when I tried to call it $ watchr -v, the system couldn't find it.
$ watchr -v
-bash: /usr/local/bin/watchr: No such file or directory
I think this is related to how the path is set up on my machine. My questions:
What is the right way to fix it?
In general, what programs should go to /usr/bin/ vs. /usr/local/bin/?
When I do e.g. $ /usr/bin/watchr -e 'watch(./hello.txt) ...', are we looking at the hello.txt in the current directory or in /usr/bin/ i.e. the same directory as watchr?
The path to your command was cached with a bad value. Try to update the cached directory that bash has stored for the path.
hash -d watchr
I found the answer over here which ctags shows /usr/local/bin/ctags but when I run ctags it runs /usr/bin/ctags. How is this possible?
Is /usr/local/bin/watchr a broken symlink? That would make which watchr not include it but watchr would print this error:
-bash: /usr/local/bin/watchr: No such file or directory
I don't know why the gem that comes with OS X installs programs in /usr/bin/, but generally /usr/bin/ is meant for preinstalled programs, and package managers use something like /opt/local/bin/ or /usr/local/bin/.
I also have /usr/local/bin/ before other folders on the path, and I put most programs that I install or compile manually to /usr/local/bin/. I used to have a separate ~/bin/ folder, but it's easy to find non-Homebrew programs with something like find /usr/local/bin ! -lname '../Cellar/*'.
Related questions about /usr/local/bin/ in general:
https://unix.stackexchange.com/questions/8656/usr-bin-vs-usr-local-bin-on-linux
https://unix.stackexchange.com/questions/4186/what-is-usr-local-bin-came-across-it-in-an-script-installation-for-applescript
create a file called .profile in your home directory and add the following line.
export PATH=“/usr/local/bin:/usr/local/sbin:/usr/bin:$PATH”

How to set the environmental variable LD_LIBRARY_PATH in linux

I have first executed the command: export LD_LIBRARY_PATH=/usr/local/lib
Then I have opened .bash_profile file: vi ~/.bash_profile.
In this file, I put:
LD_LIBRARY_PATH=/usr/local/lib
export LD_LIBRARY_PATH
Then if the terminal is closed and restarted, typing echo $LD_LIBRARY_PATH displays no result.
How to set the path permanently?
You should add more details about your distribution, for example under Ubuntu the right way to do this is to add a custom .conf file to /etc/ld.so.conf.d, for example
sudo gedit /etc/ld.so.conf.d/randomLibs.conf
inside the file you are supposed to write the complete path to the directory that contains all the libraries that you wish to add to the system, for example
/home/linux/myLocalLibs
remember to add only the path to the dir, not the full path for the file, all the libs inside that path will be automatically indexed.
Save and run sudo ldconfig to update the system with this libs.
Keep the previous path, don't overwrite it:
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/your/custom/path/
You can add it to your ~/.bashrc:
echo 'export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/your/custom/path/' >> ~/.bashrc
Add
LD_LIBRARY_PATH="/path/you/want1:/path/you/want/2"
to /etc/environment
See the Ubuntu Documentation.
CORRECTION: I should take my own advice and actually read the documentation. It says that this does not apply to LD_LIBRARY_PATH: Since Ubuntu 9.04 Jaunty Jackalope, LD_LIBRARY_PATH cannot be set in $HOME/.profile, /etc/profile, nor /etc/environment files. You must use /etc/ld.so.conf.d/.conf configuration files.* So user1824407's answer is spot on.
Alternatively you can execute program with specified library dir:
/lib/ld-linux.so.2 --library-path PATH EXECUTABLE
Read more here.
The file .bash_profile is only executed by login shells. You may need to put it in ~/.bashrc, or simply logout and login again.
For some reason no one has mentioned the fact that the bashrc needs to be re-sourced after editing. You can either log out and log back in (like mentioned above) but you can also use the commands: source ~/.bashrc or . ~/.bashrc.
Put export LD_LIBRARY_PATH=/usr/local/lib in ~/.bashrc [preferably towards end of script to avoid any overrides in between, Default ~/.bashrc comes with many if-else statements]
Post that whenever you open a new terminal/konsole, LD_LIBRARY_PATH will be reflected
Go to the home folder and edit .profile
Place the following line at the end
export LD_LIBRARY_PATH=<your path>
Save and Exit.
Execute this command
sudo ldconfig
You could try adding a custom script, say myenv_vars.sh in /etc/profile.d.
cd /etc/profile.d
sudo touch myenv_vars.sh
sudo gedit myenv_vars.sh
Add this to the empty file, and save it.
export LD_LIBRARY_PATH=/usr/local/lib
Logout and login, LD_LIBRARY_PATH will have been set permanently.
I do the following in Mint 15 through 17, also works on ubuntu server 12.04 and above:
sudo vi /etc/bash.bashrc
scroll to the bottom, and add:
export LD_LIBRARY_PATH=.
All users have the environment variable added.
In Ubuntu 20.04 Linux this is just not obvious and straight forward as it should be.
I will attempt to make it simple for anyone who is pulling out their hair just like I was with my Ubuntu 20.04.3 Linux.
Start by identifying the path where your library files' folder is located. In my case, the *.so files that I was working with were located in a folder called libs and this folder's path in my Ubuntu box is /usr/lib
So now I want to add the path /usr/lib to LD_LIBRARY_PATH such that when I run echo $LD_LIBRARY_PATH in my Ubuntu terminal I will be able to see the path /usr/lib echoed as shown below;
joseph$ echo $LD_LIBRARY_PATH
:/usr/lib
Here are the steps I used
Open terminal in Ubuntu 20.04 Linux box
Change path to /etc/ld.so.conf.d/ by running cd /etc/ld.so.conf.d/
Create a file with a *.conf extension at the end with a text editor
like e.g. vim or gedit in my case I created it as follows sudo gedit my_project_libs.conf
Inside the .conf file that I created named my_project_libs.conf
I added the path to my libs by adding this line export
LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/lib
Thereafter, I then run gedit ~/.bash_profile to open the
~/.bash_profile file so that I can add inside it this line export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/lib which includes the path to the folder with my libraries /usr/lib that I want
included in LD_LIBRARY_PATH
I also ran gedit ~/.bashrc to open the
~/.bashrc file so that I can add inside it this line export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/lib which includes the path to the folder with my libraries /usr/lib that I want
included in LD_LIBRARY_PATH
When you are done adding the line in step 5, save and close.
In your terminal, type the following sudo ldconfig and press
enter on your keyboard.
Close all your open terminals that you were using then open a new terminal session and run echo $LD_LIBRARY_PATH If you see the path you added is echoed back, you did it right.
In my case, this is what I see :/usr/lib when I run echo $LD_LIBRARY_PATH in my newly opened Ubuntu terminal session
joseph$ echo $LD_LIBRARY_PATH
:/usr/lib
That's how I got it to work for me in my Ubuntu 20.04.3 Linux box.
Everyone seems to be missing the forest for the trees.
The real answer is that '~/.bash_profile' is by default only sourced for LOGIN SHELLS.
The bash config file you are probably looking for if you are starting and closing terminals from your desktop GUI is '~/.bashrc', which is the file sourced by default when starting interactive, non-login shells.
https://apple.stackexchange.com/questions/51036/what-is-the-difference-between-bash-profile-and-bashrc

Update include path in linux

I have few header files in /my/path/to/file folder. I know how to include these files in new C program but everytime I need to type full path to header file before including it. Can I set some path variable in linux such that it automatically looks for header files ?
You could create a makefile. A minimal example would be:
INC_PATH=/my/path/to/file
CFLAGS=-I$(INC_PATH)
all:
gcc $(CFLAGS) -o prog src1.c src2.c
From here you could improve this makefile in many ways. The most important, probably, would be to state compilation dependencies (so only modified files are recompiled).
As a reference, here you have a link to the GNU make documentation.
If you do not want to use makefiles, you can always set an environment variable to make it easier to type the compilation command:
export MY_INC_PATH=/my/path/to/file
Then you could compile your program like:
gcc -I${MY_INC_PATH} -o prog src1.c src2.c ...
You may want to define MY_INC_PATH variable in the file .bashrc, or probably better, create a file in a handy place containing the variable definition. Then, you could use source to set that variable in the current shell:
source env.sh
I think, however, that using a makefile is a much preferable approach.
there is a similar question and likely better solved (if you are interested in a permanent solution): https://stackoverflow.com/a/558819/1408096
Try setting C_INCLUDE_PATH (for C header files) or CPLUS_INCLUDE_PATH (for C++ header files).
Kudos:jcrossley3
I'm not in Linux right now and I can't be bothered to reboot to check if everything's right, but have you tried making symbolic links? For example, if you are on Ubuntu:
$ cd /usr/include
$ sudo ln -s /my/path/to/file mystuff
So then when you want to include stuf, you can use:
#include <mystuff/SpamFlavours.h>

How can I prevent finding executable on $PATH?

I am using a system with an incomplete installation of GNAT, the GNU Ada compiler. A script (in the gdb testsuite) is finding /usr/bin/gnatmake and assumes that it can run Ada compiles. These fail because a the linker can't find libgnat.so.
I don't have root access, so I can't install libgnat.so or remove /usr/bin/gnatmake.
Is there any way to prevent a script from finding gnatmake in /usr/bin? I clearly cannot remove /usr/bin from the path.
Can you install a private, working version of gnatmake?
If you can, then you can create a symlink to the working version of gnatmake in your $HOME/bin directory:
ln -s /path/to/real/gnatmake ~/bin/gnatmake
Then insert your own $HOME/bin directory into your $PATH:
export PATH="$HOME/bin:$PATH"
Now the shell will find your version of gnatmake before the one in /usr/bin.
Try sudoing the script as yourself (sudo -u you ./script). In case you're not allow to sudo, you can also try exec VAR=val ./script. A third way would be to add another directory to $PATH with 'fake' empty scripts to shadow the ADA files.

Doubt regarding executable files in linux

I have a program written in C, which is named computeWeight.c and to compile it i use the following code
chaitu#ubuntu:~$ gcc -Wall -o computeWeight computeWeight.c
//to execute it:
chaitu#ubuntu:~$ ./computeWeight
Do i have any mechansim where i can directly use as mentioned below,
chaitu#ubuntu:~$ computeWeight
Should i be changing any permissions on the executable to get this?
You need to add "." to your path. Some people regard this as dangerous, though. See for instance http://www.arsc.edu/support/policy/dotinpath.html .
The $PATH variable define the places where linux would look for executables (try typing echo $PATH in a terminal). You need to put that file in one of those places. One way is to add a bin folder in your home directory, put the executable file there, and add this line (which adds the bin directory in your home folder to the search path) to your .cshrc file so that it'd be executed for every shell:
set PATH = ($PATH $HOME/bin)
With that said I don't think typing ./ is that bad.
export PATH=$PATH:.

Resources