I have a number of legacy codes that need to be compiled with specific (and often conflicting) libraries. To be specific I have a program which can only be compiled with g77 and another program which can only be compiled with gfortran.

Let's call the first program makee and the second program UVES_popler.

When compiling (and running) they both need to be linked to the version of pgplot that is compiled with the respective compiler. So, compiling makee with g77 needs to run with pgplot that has been compiled by g77 as well. And the same, respectively for UVES_popler and gfortran.

Let's assume that I can compile pgplot with both g77 and gfortran -- what is the best practice for organizing my bashrc? Should I create a bash function for each program and have the proper links and LD_LIBRARY_PATHs? Something like:

runmakee() {
  export LD_LIBRARY_PATH=/path/to/g77-pgplot/

And (probably relatedly) is there a way of setting flags in Makefiles so that the proper libraries are called when compiling these programs respectively?


Your bash functions should work, but the "usual" way of doing that is to write a wrapper script for each executable, and set anything that needs to be set in there. (You can change the executable name to foo.bin for instance, and call the wrapper script foo to make it easy to call.)

For ELF targets (not sure about other object formats), you can also set the -rpath linker option to hard-code a runtime library search path in your executables directly. With gcc (for C code), for the final link stage, it would look like:

gcc ... -Wl,-rpath,/your/hardcoded/path ...

I assume the Fortran compilers have similar options, or that you can change the linker options directly yourself (in which case the option is -rpath /your/path).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.