2

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 even tried installing the cfitsio from the /usr/local directory.
  • I installed from /usr/local/cfitsio, but again I had to use -I and -L command with these new locations.

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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
BhishanPoudel
  • 15,974
  • 21
  • 108
  • 169
  • Are you using `gcc` from XCode or some other version? The environment variable on macOS is DYLD_LIBRARY_PATH. Can you install sum links in `/usr/local/lib` pointing to the installed libraries? Can you do something similar for the headers in `/usr/local/include`, except it would probably be a link to the directory where you installed cfitsio? – Jonathan Leffler Nov 17 '16 at 15:12
  • You could use make or cmake or pkg-config [if you install cfitsio with brew](https://heasarc.gsfc.nasa.gov/fitsio/fitsio_macosx.html). You need to understand that your project need to be configure to compile. So yes -I and -L and -l are required all time. Don't use environment variable to do that. – Stargateur Nov 17 '16 at 15:14
  • @Stargateur, I did not use homebrew to install cfitsio, just use make install from their official source package. – BhishanPoudel Nov 17 '16 at 15:19
  • @BhishanPoudel It's better to use the package manager of your OS. Unless it's doesn't have what you want. I'm not a user of Macos but it's seems that there is a package for cfitsio. Brew will install the lib in PATH that gcc will search by default. So you could avoid -L. – Stargateur Nov 17 '16 at 15:23
  • @Stargateur It is a great suggestions, but I am looking for another way around. – BhishanPoudel Nov 17 '16 at 15:30
  • `DYLD_LIBRARY_PATH` is less effective since Mac OS X El Capitan 10.11 and the introduction of SIP (System Integrity Protection) — see [Can El Capitan run software compiled for Yosemite …](http://stackoverflow.com/questions/33074492/). I believe it will be simplest if you create symlinks in `/usr/local/lib` that point to where you installed the CFITSIO libraries, and you create appropriate links so that the headers are also available via `/usr/local/include`. The alternatives are far harder, and more likely to require resetting after updates. – Jonathan Leffler Nov 17 '16 at 15:47
  • You've not answered whether you're using XCode or not. If you are using XCode, the `gcc` that you're using is really `clang` in disguise. The main compiler components are also installed in some out of the way location; there are simple stubs in `/usr/bin` that invoke the correct 'hidden' programs after you've installed the command-line tools. That means it is hard to tweak the 'gcc' configuration. If the GCC is not from XCode, you may be able to tweak its configuration more easily. Nevertheless, you'll almost certainly need the shared libraries accessible via a name in `/usr/local/lib`. – Jonathan Leffler Nov 17 '16 at 15:54
  • @JonathanLeffler, I tried to create soft links, but there is no .so file in anywhere in cfitsio/lib or cfitsio/* or cfitsio/zlib/* , I was wondering what file to link? – BhishanPoudel Nov 17 '16 at 15:54
  • Well, of course there aren't any `.so` files; what do you think macOS is — Linux? The extension for dynamic libraries is `.dylib` (or sometimes `.bundle`) and not `.so`. – Jonathan Leffler Nov 17 '16 at 15:56
  • @JonathanLeffler, I am using default terminal ( form spotlight type terminal opens terminal in Mac) . I am unaware how to check whether it it Xcode or not, sorry for that. – BhishanPoudel Nov 17 '16 at 15:58
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/128368/discussion-between-bhishan-poudel-and-jonathan-leffler). – BhishanPoudel Nov 17 '16 at 15:59
  • Run `gcc --version`. If it is Apple's XCode, you'll get something like: `Apple LLVM version 8.0.0 (clang-800.0.42.1) Target: x86_64-apple-darwin16.1.0` (and some other information); if it is GNU's GCC, you'll get something like: `gcc (GCC) 6.2.0` and some other information. – Jonathan Leffler Nov 17 '16 at 16:09

1 Answers1

3

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
BhishanPoudel
  • 15,974
  • 21
  • 108
  • 169