Ad

Can C Code Be Compiled Into A Static Library For Any Architecture?

I've found a C library I'd like to use in my iOS project.

The library comes with a makefile that will compile the sources into in an .a archive. When I run lipo archs on the resulting .a, arm64 which is necessary for iOS isn't included.

If I manually throw all of the C sources and headers into my iOS project, everything will compile and link. This however is a clunky solution because it adds hundreds of files to my main project and the library the doesn't seem to be working correctly anyways.

If I try to make a new iOS Static Library project in Xcode and throw all of the C sources and headers in there, I get build errors for every resulting .o file resembling: Building for iOS, but the linked item '<item>.o' was built for macOS.

Am I trying to do something that isn't possible? What inherently determines the compatible architectures that C sources can be built for?

Ad

Answer

All of this is possible, however it is somewhat hard without seeing your code.

Here is a little starter:

clang -c library.c -o library.x86_64.o -arch x86_64 // Compile your code to x86_64
clang -c library.c -o library.arm64.o -arch arm64 // Compile your code to arm64
libtool -static -o library.x86_64.a library.x86_64.o // Create a static intel lib
libtool -static -o library.arm64.a library.arm64.o // Create a static arm64 lib
libtool -static -o library.a library.x86_64.a library.arm64.a // Create a universal static lib

Without knowing your makefile or your Xcode project, there is not much I can say without doing a very big tutorial

Ad
source: stackoverflow.com
Ad