• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Problems with a target that builds a static lib into a dylib
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Problems with a target that builds a static lib into a dylib


  • Subject: Re: Problems with a target that builds a static lib into a dylib
  • From: Pascal Bourguignon <email@hidden>
  • Date: Sat, 12 Nov 2016 12:23:22 +0100


On 11 Nov 2016, at 22:52, Jens Alfke <email@hidden> wrote:

I’m having problems with an Xcode target that simply takes a static library (.a file) built by another target, and relinks it into a .dylib. Sometimes the dylib comes out empty — a tiny file with no symbols. I’ve gotten to where this only happens sporadically, but it’s still a pain.

Context: I’ve got a library that’s built as part of my (fairly complex) project. Some targets want to link with it as a static library, and others want it as a dylib, so I need two targets that build the library as either type. To avoid having to duplicate a bunch of info like the list of source files and the compiler settings, I made the dylib-generating target dependent on the static-lib target, and it just links with the static lib. Schematically, the two targets are like:
StaticFoo: x.c, y.c, z.m ⟶ foo.a
DynamicFoo: foo.a ⟶ foo.dylib    [dependency on StaticFoo]

When I initially set this up, foo.dylib didn't even get created at all. I figured out that I had to add a single source file to the target, even if all it has is just a no-op static function. I assume this is an edge case in the Xcode build system, where if there are no source files at all it doesn’t think there’s anything to do so it doesn’t run the linker.

Now it mostly works, but I find that after switching schemes in Xcode the build will fail because foo.dylib gets rebuilt and comes out empty. foo.a is fine, but foo.dylib is a tiny stub file with no symbols in it. This then of course causes link errors in the targets that link with foo.dylib.

Any idea what’s wrong in the DynamicFoo target? I’ve added the “-all_load” linker flag, thinking that maybe the entire .a file was being dead-stripped, but that didn’t change anything.


It’s not entirely the fault of Xcode, but also of clang and its linker. 

Also, you will see that playing with -all_load and other options you’ll find it very difficult to specify what symbols should be exported from the shared library vs. the static library, notably if the public extern symbols come from the static library.

My advice here would be to compile the files again for the dynamic library.

x.c y.c z.m -> foo.a
x.c y.c z.m -> foo.dylib


-- 
__Pascal J. Bourguignon__



 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

References: 
 >Problems with a target that builds a static lib into a dylib (From: Jens Alfke <email@hidden>)

  • Prev by Date: Re: C++ and Fortran - Help linking files
  • Next by Date: Any way to extract os_log messages from a consumer's iOS device?
  • Previous by thread: Re: Problems with a target that builds a static lib into a dylib
  • Next by thread: Any way to extract os_log messages from a consumer's iOS device?
  • Index(es):
    • Date
    • Thread