Search code examples
autotoolslibtool

Is there a reasonable way to pass linker flags though configure and libtool?


Context: I am trying to cross build a library (curl) which uses autotools and libtool using clang, which requires the -target flag for pretty much everything. I set the environment vars, including CC, CFLAGS, LDFLAGS etc., run ./confgigure. So far so good.

Then I do make and the linking fails, because libtool removes (among others) the -target option. libtool even has a FAQ entry about this, which states that to remedy this, I should use flags such as -Xcompiler. Sure enough, when I run the libtool command with the necessary options prefixed by -XCClinker, it works. Except when I put that into LDFLAGS, the configure script fails instead, since it compiles its tests without libtool, and clang then fails due to malformed command line.

I ended up just patching libtool to leave unknown flags alone, but is there a sane way to pass these flags so that they work in both configure and vanilla libtool?

The only similar-ish question I found on SO is this one, but that's about stripping more flags for linking, not fewer.


Solution

  • I set the environment vars, including CC, CFLAGS, LDFLAGS etc., run ./confgigure. So far so good.

    Not necessarily. One can pass those and a few other environment variables in configure's environment. If you do, it will remember that you did, and it will use them and provide for make to use them too. But you should do that only when and to the extent necessary. SOP is to just ./configure; make, possibly followed by sudo make install.

    I ended up just patching libtool to leave unknown flags alone, but is there a sane way to pass these flags so that they work in both configure and vanilla libtool?

    When you do need LDFLAGS etc, they don't necessarily have to be specified to configure. They can also be specified directly to make, and if you need to avoid configure using them during its tests then that's exactly what you should do. For example,

    ./configure
    make LDFLAGS="-XCClinker -target ..."
    

    That's a little inconvenient because you need to specify the flags every time you run make, but if you're just trying to build the software, not develop it, then it's not too big a deal.

    If you were trying to develop the software then you might make the build system test for Mac or directly for the need for the extra flags, and provide them itself. That would yield the smoothest experience. But I would hardly expect anyone who just wanted to perform one-off builds to set that up in someone else's package.