BhishanPoudel
BhishanPoudel

Reputation: 17154

How can I add a default Include and Library path for GCC in macOS for Bash?

This should not be a difficult task. The tried pages are following:

How can I add the include and library path for the cfitsio library for the GCC compiler?

Attempt

I downloaded and installed cfitsio in the path ~/Applications. (not /Applications, BTW).

Then installation commands are:

sudo ./configure
sudo make
sudo make install

Now, let's say I have a program, example.c.

Compile: gcc -Wall -O3 example.c -lm -lcfitsio

It does not work.

However,

gcc -Wall -O3 -o example example.c -I /Users/poudel/Applications/cfitsio/include -L /Users/poudel/Applications/cfitsio/lib -lm -lcfitsio

Works

Now I don't want to use flags -I and -L all the time. How can I do so?

I updated my ~/.bash_profile with the following lines:

export PATH=$PATH:~/Applications/cfitsio/bin
export LD_LIBRARY_PATH="~/Applications/cfitsio:$LD_LIBRARY_PATH"
export LD_LIBRARY_PATH="~/Applications/cfitsio/lib:$LD_LIBRARY_PATH"
export LD_LIBRARY_PATH="~/Applications/cfitsio/lib/pkgconfig:$LD_LIBRARY_PATH"
export LD_LIBRARY_PATH="~/Applications/cfitsio/zlib:$LD_LIBRARY_PATH"

To check the paths included after running source ~/.bash_profile, I used:

echo $LD_LIBRARY_PATH

This shows the added paths correctly.

I have added the paths, but this does not work:

gcc -Wall -O3 -o example example.c -lm -lcfitsio

And if I give the -I and -L flags with their paths, it works.

Can we do something that the above command work without using -I and -L commands all the time?

Note:

I tried to use DYLD instead and added these lines in bash_profile:

export PATH="$(pwd):~/Applications/cfitsio/bin:$PATH"
export PATH="$(pwd):~/Applications/cfitsio/include:$PATH"  # fitsio.h is here
export DYLD_LIBRARY_PATH="~/Applications/cfitsio/lib:$DYLD_LIBRARY_PATH"
export DYLD_LIBRARY_PATH="~/Applications/cfitsio/zlib:$DYLD_LIBRARY_PATH"

However, if I run these commands, they return empty outputs, I could not set the dyld library path to these paths.

echo $LD_LIBRARY_PATH
echo $DYLD_LIBRARY_PATH

Upvotes: 3

Views: 15127

Answers (1)

BhishanPoudel
BhishanPoudel

Reputation: 17154

Following the suggestions of Jonathan Leffler, I got a workaround.

I created soft links, and it worked.

sudo ln -s ~/Applications/cfitsio/lib/libcfitsio.a /usr/local/lib/libcfitsio.a

sudo ln -s ~/Applications/cfitsio/include/*.h /usr/local/include/

In fact, I copied all the header files from

~/Applications/cfitsio/include

to

/usr/local/include/

and it worked.

I assume soft links also should work.

Upvotes: 4

Related Questions