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!

Grace
  • 1,265
  • 21
  • 47
  • @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? – Grace Aug 21 '15 at 09:10
  • 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? – JBL Aug 21 '15 at 09:17
  • @JBL: yeah that is what I am trying to do – Grace Aug 21 '15 at 09:18
  • Consider using `clang++` instead of `g++`, `g++` is just a clang wrapper. – Thomas Aug 24 '15 at 20:28
  • 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) – VB_overflow Aug 28 '15 at 20:21

2 Answers2

4

Have you considered the -arch compiler flag ?

g++ -c file1.cpp file2.cpp -std=c++11 -arch x86_64 -arch i386 -arch armv7 #...
Ad N
  • 7,930
  • 6
  • 36
  • 80
  • 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' – Grace Aug 21 '15 at 09:31
  • 1
    @Grace: It means the SDK is not compatible with `-arch x86_64` or `-arch i386` (check with `-v`). This answer is correct. – Thomas Aug 24 '15 at 20:25
  • 1
    @Thomas yeah it may be correct but I still couldn't find a way to make it work with me – Grace Aug 28 '15 at 10:25
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.

benjist
  • 2,740
  • 3
  • 31
  • 58
  • 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. – Grace Aug 31 '15 at 07:02
  • 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. – benjist Aug 31 '15 at 08:35