Compile c++ files for all iOS architectures
Asked Answered
S

2

7

I have some cpp files that I would like to compile it in order to run on simulator and iPhone. What I am trying to do is:

g++ -c file1.cpp file2.cpp -std=c++11
ar rcs libabc.a *.o

And this compiles fine but only for x86_64 architecture..Obviously...

Is there any easy way I can edit these 2 line of command in order to have a library compiled for all architectures (x86_64 i386 armv7 armv7s arm64)? Or should I build some huge scripts to have that library? If so? Is there any ready scripts for that?

I have also tried to run it using -arch:

g++ -c file1.cpp file2.cpp -std=c++11 -arch armv7 -arch x86_64

but these are some errors I'm getting

//----------------- Error 1 -------------------------//

enter image description here

//----------------- Error 2 -------------------------//

enter image description here

//----------------- Error 3 -------------------------//

enter image description here

Thanks!

Swim answered 21/8, 2015 at 8:36 Comment(5)
@JBL tried to run the script but no luck. What is the ./configure is supposed to mean in the script?? and on which level I have to put the script? on the same level of my .h and .cpp files?Swim
I was a bit mistaken, this is not really appropriate for your problem. Are you trying to compile these files as a library for use in your iOS app?Dendro
@JBL: yeah that is what I am trying to doSwim
Consider using clang++ instead of g++, g++ is just a clang wrapper.Pennant
Why not using XCode to compile those cpp files to a library ? You could create a XCode project including those, then either compile for the architecture you want using XCode IDE or use xcodebuild to compile the project from commandline (there are options to choose architecture)Gushy
S
0

Better build for each arch individually and then glue thrm together. It helps in figuring out different or wrongly included platform specific headers and else.

But from my experience, all this isn't worth it if you can avoid building via console. Create an Xcode project and use a fake framework, if it suits you. Or build the static libraries individually and later glue them together yourself.

Statistician answered 30/8, 2015 at 20:59 Comment(2)
Hi @benjist, even If I compile for each one individually that doesn't work also. I have already libraries compiled from Xcode, but I am in a need to make it also working via console.Swim
Well then you have to fiddle and find the correct include paths for each supported architecture and you must use the correct compiler. This is your actual problem. The compiler you use can't build for the chosen architecture and the architecture specific includes must refer to architecture specific paths inside Xcodes toolchain. All these compilers and includes are inside Xcode when you install also the command line tools. Or you set all compilers up yourself - something you really don't want to do.Statistician
E
4

Have you considered the -arch compiler flag ?

g++ -c file1.cpp file2.cpp -std=c++11 -arch x86_64 -arch i386 -arch armv7 #...
Eldwon answered 21/8, 2015 at 9:29 Comment(3)
Yeah it gives me some strange errors like: /usr/include/sys/cdefs.h:680:2: error: Unsupported architecture or /usr/include/sys/_types/_ssize_t.h:30:9: error: unknown type name '__darwin_ssize_t'Swim
@Grace: It means the SDK is not compatible with -arch x86_64 or -arch i386 (check with -v). This answer is correct.Pennant
@Pennant yeah it may be correct but I still couldn't find a way to make it work with meSwim
S
0

Better build for each arch individually and then glue thrm together. It helps in figuring out different or wrongly included platform specific headers and else.

But from my experience, all this isn't worth it if you can avoid building via console. Create an Xcode project and use a fake framework, if it suits you. Or build the static libraries individually and later glue them together yourself.

Statistician answered 30/8, 2015 at 20:59 Comment(2)
Hi @benjist, even If I compile for each one individually that doesn't work also. I have already libraries compiled from Xcode, but I am in a need to make it also working via console.Swim
Well then you have to fiddle and find the correct include paths for each supported architecture and you must use the correct compiler. This is your actual problem. The compiler you use can't build for the chosen architecture and the architecture specific includes must refer to architecture specific paths inside Xcodes toolchain. All these compilers and includes are inside Xcode when you install also the command line tools. Or you set all compilers up yourself - something you really don't want to do.Statistician

© 2022 - 2024 — McMap. All rights reserved.