简体   繁体   English

为 XCode iOS 构建 C++ 静态库的问题

[英]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.我在 Objective-c 中有一个大型 iOS 项目,它在 6 个子文件夹中也有大量 C++ 类。 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.如果我在 iOS 项目中拥有所有 C++ 源代码并从源文件编译,那么一切都可以编译并且工作得很好。 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.我想将所有这些 C++ 代码移动到一个静态库中,所以我为我的类创建了一个新项目和一个包装器,并根据 SO 上的大量指令将其编译为一个静态库。 It compiles without any errors and created the .a output file with the appropriate header.它编译时没有任何错误,并创建了带有适当标头的 .a 输出文件。

When I include this Static Library into my project I get 11 errors related to:当我将此静态库包含到我的项目中时,我收到 11 个与以下相关的错误:

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 命令时,我确认我的静态库是架构: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.我没有在这个项目中使用模拟器,因为它需要一个实时摄像头,所以我只需要一个架构,即 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.如果我从应用程序的 Linux 版本的相同代码创建一个静态库,它工作得很好,所以我知道基本代码、头文件和文件结构都很好。 That Static Library says it is architecture x86_64 according to lipo -info which make sense.该静态库根据 lipo -info 说它是架构 x86_64,这是有道理的。

$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.我看到的另一件事是,由 XCode 作为静态库构建的 .a 文件是 732KB,但 Eclipse 为 Linux 从完全相同的源文件构建的文件是 2.2MB。 Why would they be so different?为什么他们会如此不同? All of the source code that is supposed to be included is 1.6MB.应该包含的所有源代码都是 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.我觉得问题不在于静态库是错误的架构,而是它不包括项目子文件夹中的所有 C++ 文件。 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.我看到的另一个问题是,如果我错误地将静态库 XCode 项目配置为编译为可执行文件,我会得到与将静态库包含到设备项目中时得到的完全相同的错误集,但我不太清楚除了不编译所有文件之外,还有什么可以做的。 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.请记住,当这个 C++ 源代码作为主项目的一部分被添加和编译时,它一切正常。

Does anyone know how to ensure that XCode will include all of the files in the subfolder and classes that are associated with them?有谁知道如何确保 XCode 将包含子文件夹中的所有文件以及与它们关联的类? 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?是否有一个特殊的链接器设置需要在 XCode 中使用才能让它链接作为静态库项目一部分的所有 C++ 文件?

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.由于有多个级别的文件,这很麻烦,但最后一个模糊的深夜线索是 XCode 中的文件夹是蓝色而不是黄色。 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.我以前也遇到过同样的事情,应该已经认识到,当 XCode 中的文件夹为蓝色时,它没有导入您的文件,即使它们显示在里面并且似乎正在构建。 C++ Static Libraries in XCode do work and its simple once you have all the files included in the Static Library project.一旦您将所有文件都包含在静态库项目中,XCode 中的 C++ 静态库就可以工作并且很简单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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