让 XCode 在 XCode 4.3(.1) 中包含、编译和链接现有 (C++) 代码库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10128646/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 13:39:18  来源:igfitidea点击:

Getting XCode to include, compile and link existing (C++) codebase in XCode 4.3(.1)

iphonec++iosxcode

提问by olafure

I'm trying to 'import' my existing C++ codebase into my XCode iOS project.

我正在尝试将我现有的 C++ 代码库“导入”到我的 XCode iOS 项目中。

Problem is, xCode won't compile (and therefore not link) my code. This is what I've done so far:

问题是,xCode 不会编译(因此不会链接)我的代码。这是我到目前为止所做的:

  • File > Add files to "Project". I don't tick 'Copy files' since it's a shared codebase for multiple platforms and multiple copies of source code is not an option.
  • I've added appropriate path to the "Header Search Path" project settings. Including header files works, I can #include "stuff.h" from my Objective-C++ (.mm) file.
  • 文件 > 将文件添加到“项目”。我不勾选“复制文件”,因为它是多个平台的共享代码库,并且源代码的多个副本不是一个选项。
  • 我已经为“标题搜索路径”项目设置添加了适当的路径。包含头文件有效,我可以从我的 Objective-C++ (.mm) 文件中 #include "stuff.h"。

However, I can't find any way of actually telling XCode to compile the .cpp files?

但是,我找不到任何实际告诉 XCode 编译 .cpp 文件的方法?

A few points:

几点:

  • I'm not looking for a way to pre compile it into a library and linking that way
  • The project is a standard iOS Objective-C project. The C++ code is called from it (that's not a problem)
  • 我不是在寻找一种将它预编译到库中并以这种方式链接的方法
  • 该项目是一个标准的 iOS Objective-C 项目。从中调用 C++ 代码(这不是问题)

Update

更新

I've added the codebase directoryto the target's "Compile Sources". I actually have to click "Add other..." in the dialog that pops up, since I can't select the directory reference to my codebase.

我已将代码库目录添加到目标的“编译源”中。我实际上必须在弹出的对话框中单击“添加其他...”,因为我无法选择对我的代码库的目录引用。

When I add it, XCode asks me if I want to "Create external build system project". I leave it ticked and the two fields with the default values "/usr/bin/make" and "$(ACTION)". However, my codebase doesn't get compiled during the build. Do I have to write my own Makefile ?

当我添加它时,XCode 会问我是否要“创建外部构建系统项目”。我把它打勾,两个字段的默认值是“/usr/bin/make”和“$(ACTION)”。但是,我的代码库在构建过程中没有被编译。我必须编写自己的 Makefile 吗?

If I don't select "Create external build system project" then a directory reference is created under the target, but not compiled during the build and a warning is emitted:

如果我不选择“创建外部构建系统项目”,则会在目标下创建目录引用,但不会在构建期间编译并发出警告:

warning: no rule to process file '$(PROJECT_DIR)/../codebase'
of type folder for architecture i386

回答by sch

Even if you don't check Copy Filesyou have to check Add to Targetswhen adding the files to the project.

即使您不检查复制文件,您也必须在将文件添加到项目时检查添加到目标

enter image description here

在此处输入图片说明

If you forgot to do that, you can select the target, go to the Build Phasestab and add the files to Compile Sources.

如果您忘记这样做,您可以选择目标,转到Build Phases选项卡并将文件添加到Compile Sources

enter image description here

在此处输入图片说明

回答by Halsafar

If you go to your Targets -> Build Phases -> Compile Sources -> "+" -> "Add Others" -> (select your external source folder) -> Select: Create Group for any added folders -> Unchecked: Copy items into destination.

如果你去你的目标 -> 构建阶段 -> 编译源 -> "+" -> "添加其他" ->(选择你的外部源文件夹)-> 选择:为任何添加的文件夹创建组 -> 未选中:复制项目进入目的地。

Now your folder will be in XCode, mimic the structure. If you add source files to that folder in XCode the source files will be added to your external source directory just fine. So the ease of using multiple IDEs and having to manage source linkage is solved.

现在你的文件夹将在 XCode 中,模仿结构。如果您在 XCode 中将源文件添加到该文件夹​​,源文件将被添加到您的外部源目录中就好了。因此,解决了使用多个 IDE 和必须管理源链接的简便性。

The catch. You still have to manually add files into the "Compile Sources" area. This is rather simple. Just click and drag your source files from the group directory in XCode right into the Compile Sources window. So there is an extra step when you add a source file now. Forgetting to add them to the Compile Sources window will yield unresolved symbols compile errors.

抓住了。您仍然必须手动将文件添加到“编译源”区域。这比较简单。只需单击并将您的源文件从 XCode 的组目录中拖到“编译源”窗口中即可。所以现在添加源文件时有一个额外的步骤。忘记将它们添加到 Compile Sources 窗口将产生未解决的符号编译错误。