make arm architecture c library in mac

2019-07-25 11:05发布

问题:

I'm trying to make my own c library in Mac and include it to my iphone program. The c code is simple , like this:

math.h:

int myPow2(int);

math.c:

#include "math.h"

int myPow2(int num) { 
    return num*num; 
} 

I search how to make the c library file ( .a or .lib ..etc) seems need to use gcc compiler (Is there other methods?) so I use this command:

gcc -c math.c -o math.o
ar rcs libmath.a math.o

And include it in iPhone Project. Now it has the problem when build xcode iphone project.

"file was built for unsupported file format which is not the architecture being linked"

I found some pages discuss about the problem, but no detail how to make the i386/arm architecture library. And I finally use this command to do it:

gcc -arch i386 -c math.c -o math.o
/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/arm-apple-darwin10-gcc-4.2.1 -c math.c -o math.o 

I dont know if this method is correct? Or there has another method to do it?

回答1:

What Bavarious said.

Add a new target to your project. When XCode asks you what template to use, chose "Static Library".

Add the files for the library to that target. Add the static library target as a dependency of your main executable target. Add the library that gets built to your main executable target. Voila!



标签: iphone c linker