warrenm / GLTFKit2

A glTF 2.0 asset loader and exporter for Objective-C and Swift.
MIT License
159 stars 29 forks source link

Library not found when statically linking to Draco #34

Closed ClayQAQ closed 1 year ago

ClayQAQ commented 1 year ago

I have compiled draco by using “$ cmake ../ -G Xcode” . And I also got “libdraco.a” from Debug folder,and put libdraco.a into the Xcode But xcode show "Library not found for -ldraco", Would you mind helping me?

warrenm commented 1 year ago

You have at least two options for linking to a static draco library.

If you want to use linker flags (-ldraco) to link, you also need to specify the directory where the .a file is located in the target's "Library Search Paths"; Xcode doesn't automatically search the project directory recursively.

On the other hand, you can add the library to the target's Link Binary with Libraries build phase; this should automatically note the path to the library without needing to specify it elsewhere.

ClayQAQ commented 1 year ago

Thank you for your reply. But the way you said doesn't work. In fact,I've done both of these options. I wrote the path of "libdraco.a" on the "Library Search Paths", and also add the library to the target's Link Binary with Libraries build phase. I wonder if I compiled draco library incorrectly. No draco header file found after compilation. Here is my Xcode's configuration:

1 2

And here is the draco library I builded:

3
warrenm commented 1 year ago

The search path should be a directory, not the path of the library file itself.

ClayQAQ commented 1 year ago

Understood, you're right. I have already run the Draco library, but the "libdraco.a" library is 21.3MB in size. Is it too large? We only need the decoding functionality. When compiling Draco, can we make it as small as possible? How did you do it?

warrenm commented 1 year ago

I think you can reduce it to a few MB per architecture if you compile it in release mode optimized for size. If you want a prebuilt package and you’re comfortable with adding an SPM dependency, I made DracoSwift for exactly this purpose.

ClayQAQ commented 1 year ago

Thanks I'll take a look

ClayQAQ commented 1 year ago

I recompile libdraco.a to 2.7MB in release mode. Can I extract only the decoding module and further compress the library's size?

warrenm commented 1 year ago

You can do whatever you want if you’re compiling from source. Keep in mind that since you’re statically linking, though, that your final binary size won’t increase by the size of the compiled library; it’ll only increase by the size of the functions that are actually used by the app.

ClayQAQ commented 1 year ago

You mean The Xcode compiler will optimize and discard the portions of static libraries that we are not using, isn’t it? So I don't have to mind the size of the static library embedded in xcode.

warrenm commented 1 year ago

It's a bit of a subtle art that I don't fully understand, but if you think of a static library as an archive of object files, under certain optimization settings, a linker can recognize which sections of a static library are never referenced and omit them from the final binary. You could probably produce a smaller static library to begin with by manually including only the functionality required by your app, as DracoSwift is intended to be a fully general distribution of Draco.

ClayQAQ commented 1 year ago

Thanks, but manually selecting static libraries is a bit of a hassle.

warrenm commented 1 year ago

Okay, well, at this point you know your options, and you can decide how to proceed.