8

I am building the google-gflags commandline flags library for C++ on Mac OS X (10.7.1). The build process is as such:

$ ./configure --prefix=output
$ make
$ make install 

I'd like to change the install name of the generated shared library at build time and not use install_name_tool afterwards.

By default, the install name of the generated shared library, libgflags.dylib, is the output path:

$ otool -L ./output/libgflags.dylib
$ ./output/libgflags.dylib:
    /tmp/gflags-1.5/output/lib/libgflags.0.dylib (compatibility version 2.0.0, current version 2.0.0)
    /usr/lib/libstdc++.6.dylib (compatibility version 7.0.0, current version 52.0.0)
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 159.0.0) 

The man page for ld(1) has a -install_name option which can be used to change the install name of a dynamic library at link-time.

For example, with a dummy program:

$ g++ -dynamiclib temp.cc -install_name /tmp/temp.dylib -o temp.dylib
$ otool -L temp.dylib 
temp.dylib:
    /tmp/temp.dylib (compatibility version 0.0.0, current version 0.0.0)
    /usr/lib/libstdc++.6.dylib (compatibility version 7.0.0, current version 52.0.0)
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 159.0.0)

But, I am unable to use this command line option with the ./configure script. I've tried manually setting the CFLAGS variable, but that results in a error:

$ CFLAGS="-install_name /tmp/desired/location/libgflags.dylib" ./configure
checking for a BSD-compatible install... /opt/local/bin/ginstall -c
checking whether build environment is sane... yes
checking for gawk... no
checking for mawk... no
checking for nawk... no
checking for awk... awk
checking whether make sets $(MAKE)... yes
checking for gcc... gcc
checking whether the C compiler works... no
configure: error: in `/Users/vibhav/Code/install_name_test/gflags-1.5':
configure: error: C compiler cannot create executables

So, is it possible for me to change the install name of .dylib generated by configure and make without using install_name_tool?

v8891
  • 133
  • 1
  • 5
  • I do not understand what you are trying to do. Why can't you simply run `./configure --prefix /tmp/desired/location`? – adl Aug 24 '11 at 16:24
  • That's a valid question. I cannot to set `--prefix` to that location for various reasons, including that my build location is different from my application's "install" location. I simply want to pass the appropriate linker flag while using `configure`. – v8891 Aug 24 '11 at 17:00
  • Your scenario is still not clear to me. Could you explain how the standard `./configure --prefix /somewhere && make && make install` is wrong for your setup? Why would your build location matter? (You should use an absolute prefix, of course.) Are you trying to link to `libglflags` without installing it? (In that case you should use `libtool` to link your application with `libflag.la` and let libtool do its magic to link with a non-installed library.) Do you want to install the library to a temporary location before it is copied to its final location? (It's a job for the DESTDIR variable.) – adl Aug 25 '11 at 00:58
  • 2
    The Apple folks recommend using ***`-headerpad_max_install_names`*** to compile and link. Then, when you ***`make install`***, you use ***`install_name_tool`*** to update the record so its points to the fully qualified pathname (i.e., `/usr/local/lib/libfoo.dylib`). Nick X basically answered the same, but he did not mention the Apple's recommendation or ***`-headerpad_max_install_names`***. – jww Mar 02 '16 at 04:27

2 Answers2

8

Generally, passing linker arguments through g++ must be prefaced with -Wl and spaces must be replaced with commas. So, if you want to pass "-install_name /tmp/temp.dylib" to the linker you will need to call this:

g++ -Wl,-install_name,/tmp/temp.dylib ...
Ben
  • 221
  • 1
  • 3
  • 5
5

one possible approach would be editing the config.status manually. but before I try to do that, install_name_tool -id saved my life.

Nikhil
  • 16,194
  • 20
  • 64
  • 81
Nick X
  • 176
  • 1
  • 7