简体   繁体   中英

Problems Building C++ Static Library for XCode iOS

I have a large iOS project in Objective-c that also has a large number of C++ classes in a 6 subfolders. If I have all of the C++ source code in the iOS project and compile from the source files, everything compiles and works just fine. All good so far.

I wanted to move all of this C++ code to a static library, so I created a new project and a wrapper for my classes and compiled it as a Static Library based on the numerous sets of instructions on SO. It compiles without any errors and created the .a output file with the appropriate header.

When I include this Static Library into my project I get 11 errors related to:

Undefined symbols for architecture arm64:
  "Configuration::Configuration()", referenced from:
  ___cxx_global_var_init in libMInterfaceLib.a(M- 
   3fdac31d875d4f01157909522738d4c95b3c465d55314d375878e8f2faec0803.o)

 "Type::GetSizes(int, int, int&, int&, int&)", referenced from:
    CM::BitSizes() in libMInterfaceLib.a(M- 
  3fdac31d875d4f01157909522738d4c95b3c465d55314d375878e8f2faec0803.o)

When I use the lipo info command I get confirmation that my Static Library is architecture: arm64

 $lipo -info libMInterfaceLib.a
 Non-fat file: libMInterfaceLib.a is architecture: arm64

Which makes sense because I'm building the Static Library for a device and including it in a device build. I'm not using a Simulator for this project because it needs a live camera so I only need one Architecture which is arm64.

If I create a Static Library from the same code for the Linux version of the application it works just fine, so I know the basic code, headers and file structure is good. That Static Library says it is architecture x86_64 according to lipo -info which make sense.

$lipo -info libMInterfaceLib-OSx.a
Non-fat file: libMInterfaceLib-OSx.a is architecture: x86_64

The other thing I see is that the .a file built by XCode as a Static Library is 732KB, but the file built by eclipse for Linux from exactly the same source files is 2.2MB. Why would they be so different? All of the source code that is supposed to be included is 1.6MB.

I get the feeling that the problem is not that the Static Library is the wrong Architecture, but that it is not including all of the C++ files that are in the subfolders of the project. The other issue I see is that if I misconfigure the Static Library XCode Project to compile as an executable, I get exactly the same set of errors as I get when I include the Static Library into the device project, but I don't quite know what to make of that other than its not compiling all of the files. Its also super fast to compile which leads me to believe again that its not building everything into the Static Library that it should. Remember, when this C++ source code is added and compiled as part of the main project it all works fine.

Does anyone know how to ensure that XCode will include all of the files in the subfolder and classes that are associated with them? Is there a special linker setting that I need to be using in XCode to get it to link all of the C++ files that are part of the Static Library project?

Anyone know where I'm going wrong here?

Any help and guidance is greatly appreciated.

OK, So after a bunch of messing around I realized that just including the folders into the Static Library project didnt work. I had to create individual Groups for each folder and then import all of the files into those Groups as files in the folders. As there were multiple levels of files this was a pain in the arse but the final blurry eyed late night clue was that the folders in XCode were Blue and not Yellow. That was the only indication that they werent all included. There we no compilation errors in the Static Library project, it was only when trying to use that Static Library in another project that any errors showed up. I've been caught by the same thing before and should have recognized that when folders are Blue in XCode, it hasnt imported your files even though they show up inside and appear to build. C++ Static Libraries in XCode do work and its simple once you have all the files included in the Static Library project.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM