使用现有代码创建 XCode 项目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5942059/
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-09-09 03:56:16  来源:igfitidea点击:

creating an XCode project with existing code

xcodexcode-template

提问by Mr Aleph

I'm in the process of porting a Linux application to Mac. I have different files with the source code that can get compiled and linked using the standard Makefile.

我正在将 Linux 应用程序移植到 Mac。我有不同的源代码文件,可以使用标准 Makefile 编译和链接。

I'm going to be porting that code to Mac and continue writing code in C (sorry, no obj-c). Is there a way to create a project on XCode, add the existing code so I can use XCode and the IDE, compile and debug the code and generate Mac Makefiles?

我打算将该代码移植到 Mac 并继续用 C 编写代码(抱歉,没有 obj-c)。有没有办法在 XCode 上创建项目,添加现有代码以便我可以使用 XCode 和 IDE,编译和调试代码并生成 Mac Makefile?

Thanks for the help

谢谢您的帮助

回答by justin

New Project -> Other -> External Build System

新建项目 -> 其他 -> 外部构建系统

(in new project)

(在新项目中)

Expand "Targets"

展开“目标”

select the target the template created

选择模板创建的目标

press return

按回车

edit the target settings:

编辑目标设置:

  • by default, it uses /usr/bin/makefor invocation. if you want to use some other build system, then you'll have more to configure.
  • for a Makefile, you will need to set the 'Directory' field of the build settings window to the directory which contains the appropriate makefile to build.
  • 默认情况下,它/usr/bin/make用于调用。如果您想使用其他构建系统,那么您将需要配置更多。
  • 对于 Makefile,您需要将构建设置窗口的“目录”字段设置为包含要构建的相应 makefile 的目录。

note that you'll lose some integration when using a makefile.

请注意,使用 makefile 时会丢失一些集成。

you can regain some of that by adding the sources to the project (drag and drop), and not associating them with a target.

您可以通过将源添加到项目(拖放)而不将它们与目标相关联来重新获得其中的一些。

to improve navigation and code completion, you may want to create a second (dummy) target (such as a static library) so the ide parses your programs. you would then add the sources to the dummy static library, and set the makefile as a 'dependency' of the static library (so it gets built, and so it gets built first).

为了改进导航和代码完成,您可能需要创建第二个(虚拟)目标(例如静态库),以便 ide 解析您的程序。然后将源添加到虚拟静态库中,并将 makefile 设置为静态库的“依赖项”(因此它被构建,所以它首先被构建)。

回答by Jamie

The Apple Developer docs have a section on porting makefile based projects into XCode.

Apple Developer 文档有一个部分是关于将基于 makefile 的项目移植到 XCode 中的。

"Porting UNIX/Linux Applications to OS X"

“将 UNIX/Linux 应用程序移植到 OS X”

This subsection is most relevant: "Building makefile projects with XCode"

本小节最相关:“使用 XCode 构建 makefile 项目”

https://developer.apple.com/library/content/documentation/Porting/Conceptual/PortingUnix/preparing/preparing.html#//apple_ref/doc/uid/TP40002849-BBCJABGC

https://developer.apple.com/library/content/documentation/Porting/Conceptual/PortingUnix/preparing/preparing.html#//apple_ref/doc/uid/TP40002849-BBCJABGC

回答by the_mandrill

You can build using your existing makefiles and create a wrapper project with a custom target with a 'Run Script' build phase that just calls down to your makefile. This means that you'll also be able to use the debugger, but you probably won't get the full benefit of the editor with autocompletion etc.

您可以使用现有的 makefile 进行构建,并使用“运行脚本”构建阶段的自定义目标创建包装器项目,该阶段仅调用您的 makefile。这意味着您还可以使用调试器,但您可能无法通过自动完成等功能获得编辑器的全部好处。

回答by historystamp

To import C code into Xcode:

将 C 代码导入 Xcode:

  1. start xcode
  2. new project
  3. pick something like Standard Tool. Should read c command line tool.
  4. drag you file or files onto main project window. This is the first window that comes up when you create a new project. My project name is xaBitHoist. I dropped the files on xaBitHoist > src.
  1. 启动 xcode
  2. 新项目
  3. 选择标准工具之类的东西。应该阅读 c 命令行工具。
  4. 将您的一个或多个文件拖到主项目窗口中。这是创建新项目时出现的第一个窗口。我的项目名称是 xaBitHoist。我将文件放在 xaBitHoist > src 上。

just build. Seems to default to main.c

只是建立。似乎默认为 main.c

to change start program or add argues Project > New Project Executable.

更改启动程序或添加争论项目> 新项目可执行文件。

the online help for xcode is good.

xcode 的在线帮助很好。

Good luck.

祝你好运。

Robert

罗伯特