Why adding static libcurl.a to xcode 7.2 example p

2020-06-27 11:53发布

问题:

I had been trying to eliminate any dependency on libcurl.4.dylib by adding libcurl.a to my Xcode 7.2 project. I had built a brand-new libcurl and placed it in /usr/local/lib (with the header in /usr/local/include/curl):

export MACOSX_DEPLOYMENT_TARGET="10.8"
./configure --disable-shared --with-darwinssl
make clean
make
make install

But the .app was STILL linked with a dependency on libcurl.4.dylib. Finding out why took several days.

Although I am adding libcurl.a to my Xcode "Link Binary with Libraries" Build Phase, Xcode's generated link command of -lcurl is telling the linker to, by default, link with a DYNAMIC library. The linker will NOT accept -static as a prefix to -l, because the CLANG driver doesn't support turning on/off -static/-dynamic on a per-file basis (if the linker does see -static, it expects that EVERYTHING is static — which Apple STRONGLY DISCOURAGES).

Once I changed (in a MANUAL shell link line) the Xcode-generated -lcurl to be explicitly "/usr/local/lib/libcurl.a", the linked .app no longer showed a dependency on libcurl.4.dylib.

What sucks is that it appears there is no easy method to do this through Xcode — unless I make a copy of libcurl.a with a DIFFERENT NAME.

Maybe others know the "magic" params / build options for Xcode 7.2, or have more insight, but I wanted to leave this here in case anyone else ran into this issue with libcurl -- or any other static (.a) library where there happened to be a dynamic (.dylib) of the same name in the same location.

Stephen

EDIT: This is the command line(s) I used to build the universal static library:

export MACOSX_DEPLOYMENT_TARGET="10.10"
./configure --prefix=$HOME CFLAGS="-arch i386 x86_64" LDFLAGS="-arch i386 x86_64" --disable-shared --with-darwinssl --without-zlib  --without-libidn --disable-ldap
make clean
make
sudo make install

回答1:

I tried the approach suggested by SMGreenfield and it did not work for me. One of the reasons: the library cannot be used in the universal environment because file curlbuild.h is different for each architecture. Using this as a source https://curl.haxx.se/mail/lib-2015-08/0146.html, I did the following. Have not been able to do a lot of testing yet, but the library compiled without error "curl_rule_01 declared as an array with negative size".

  1. Build 64-bit static libcurl:

Download CURL source file to: /Users/yourname/Libs/.

Create folder /Users/yourname/Libs/curl_64.

Type in Terminal:

$ cd /Users/yourname/Libs/curl-7.52.1
$ export MACOSX_DEPLOYMENT_TARGET="10.7"
$ export CFLAGS="-arch x86_64"
$ export LDFLAGS="-arch x86_64"
$ ./configure --prefix=/Users/yourname/Libs/curl_64 --disable-shared -–enable-static --without-libidn –-without-zlib -–disable-ldap
$ make clean
$ make
$ make install
  1. Build 32-bit static libcurl:

Create folder /Users/yourname/Libs/curl_32.

Type in Terminal:

$ cd /Users/yourname/Libs/curl-7.52.1
$ export MACOSX_DEPLOYMENT_TARGET="10.7"
$ export CFLAGS="-arch i386"
$ export LDFLAGS="-arch i386"
$ ./configure --prefix=/Users/yourname/Libs/curl_32 --disable-shared –-enable-static --without-libidn –-without-zlib -–disable-ldap
$ make clean
$ make
$ make install
  1. Create universal libcurl:

Create folder /Users/yourname/Libs/curl_universal/lib.

Lipo two static libraries:

lipo -create /Users/yourname/Libs/curl_32/lib/libcurl.a /Users/yourname/Libs/curl_64/lib/libcurl.a –output /Users/yourname/Libs/curl_universal/libcurl.a

Copy all subfolders from /Users/yourname/Libs/curl_64 to /Users/yourname/Libs/curl_universal except for subfolder lib!

In folder /Users/yourname/Libs/curl_universal/include/curl copy file curlbuild.h and save as curlbuild64.h. Copy this file from folder /Users/yourname/Libs/curl_32/include/curl to folder /Users/yourname/Libs/curl_universal/include/curl and save it as curlbuild32.h. Now we should have three files: curlbuild.h, curlbuild64.h, and curlbuild32.h.

  1. Open file curlbuild.h and edit as follows: