C++ 学习在 xcode 中编译
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2725850/
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
Learning to compile in xcode
提问by Nathan
I have started using c++ extensively in school and now my programs are getting to the point where have more than 1 file (i.e. header, driver, implementation file). I don't know enough about software development to understand or get a grasp of how to setup the build process by looking at apple's guides. Could someone walk me through how to compile a simple c++ project with a header and two c++ files in xcode? Do I first need to make a makefile??
我已经开始在学校广泛使用 c++,现在我的程序已经到了有超过 1 个文件(即头文件、驱动程序、实现文件)的地步。我对软件开发的了解不够,无法通过查看苹果的指南来理解或掌握如何设置构建过程。有人可以指导我如何在 xcode 中编译一个带有头文件和两个 C++ 文件的简单 C++ 项目吗?我首先需要制作一个makefile吗?
回答by Kristopher Johnson
In Xcode:
在Xcode中:
- Select File -> New Project...
- Select Mac OS X -> Application -> Command Line Tool (I'm assuming that you are making a simple C++ app that uses stdin/stdout, not a Cocoa-based GUI)
- For "Type", choose "C++ stdc++"
- Click "Choose..." and give the project a name and location
- A new project window will appear. Open the "Source" group (folder) on the right hand side. There will be a
main.cpp
file. You can use that file if desired, or select it and hit the Delete key to get rid of it. - Select Project -> Add to Project...
- Select the files (headers and .cpp files) you want to add to the project.
- Select Build -> Build and Run to run your project
- 选择文件 -> 新建项目...
- 选择 Mac OS X -> 应用程序 -> 命令行工具(我假设您正在制作一个使用 stdin/stdout 的简单 C++ 应用程序,而不是基于 Cocoa 的 GUI)
- 对于“类型”,选择“C++ stdc++”
- 单击“选择...”并为项目指定名称和位置
- 将出现一个新项目窗口。打开右侧的“源”组(文件夹)。会有一个
main.cpp
文件。如果需要,您可以使用该文件,或者选择它并按 Delete 键删除它。 - 选择项目 -> 添加到项目...
- 选择要添加到项目中的文件(头文件和 .cpp 文件)。
- 选择 Build -> Build and Run 来运行你的项目
You will want to learn more about Xcode if you are going to use it as your main IDE. There's a lot of documentation for Xcode, but most of it is designed for people creating Cocoa apps. For developing simple C++ console apps, you can probably start with the Xcode Workspace Guide.
如果您打算将 Xcode 用作主要 IDE,您将需要了解有关 Xcode 的更多信息。Xcode 有很多文档,但其中大部分是为创建 Cocoa 应用程序的人设计的。对于开发简单的 C++ 控制台应用程序,您可以从Xcode Workspace Guide 开始。
回答by Tomas
You commented about targets, so I thought I might explain them for you:
你评论了目标,所以我想我可以为你解释一下:
You don't need to worry about it for now, if you select the C++ Command Line Tool project template there is already a target and some build configurations made for you which should already work.
您现在不必担心,如果您选择 C++ 命令行工具项目模板,那么已经有一个目标和一些为您制作的构建配置,它们应该已经可以工作了。
Rather then have multiple makefiles Xcode prefers to keep its compiling information within the project file, targets are for building different products, if you click the drop down arrow on the standard target (Should be the name of your project) you can see 'build phases', essentially steps in building that require specific input, using the "Compile with Source" and "Copy Headers" phases you can have two targets which use separate main.cpp files and make separate applications, yet still share some source files within the project.
而不是有多个生成文件 Xcode 更喜欢将其编译信息保留在项目文件中,目标用于构建不同的产品,如果您单击标准目标上的下拉箭头(应该是您的项目名称),您可以看到“构建阶段” ',基本上是需要特定输入的构建步骤,使用“使用源代码编译”和“复制头文件”阶段,您可以有两个目标,它们使用单独的 main.cpp 文件并制作单独的应用程序,但仍然在项目中共享一些源文件.
The active target is the one Xcode is going to work with when you hit the 'build' button.
活动目标是当您点击“构建”按钮时 Xcode 将使用的目标。
The build configuration changes the values passed to the GCC compiler (The backend responsible for compiling and linking all your source files, the messages from which you can see in the build window) and a few processes xcode handles itself. These values anything from custom macros to level of optimisation performed. Traditionally one has a Development configuration which has low optimisations and fills in variables symbols for debugging purposes, and then a Deployment configuration with high optimisation and no support for debugging and other developer features. If you want to muck around with these options you can change them for your entire project by right clicking your project file, selecting get info and opening the 'build' tab, you can change them for each target by right clicking the target, selecting get info and opening the 'build' tab and selecting what configuration to change using the drop down menu on the left.
构建配置更改传递给 GCC 编译器的值(负责编译和链接所有源文件的后端,您可以在构建窗口中看到的消息)和一些 xcode 处理自身的进程。这些值从自定义宏到执行的优化级别。传统上,一种开发配置具有低优化并填充变量符号以用于调试目的,然后具有高度优化且不支持调试和其他开发人员功能的部署配置。如果您想使用这些选项,您可以通过右键单击您的项目文件,选择获取信息并打开“构建”选项卡来为整个项目更改它们,您可以通过右键单击目标,选择获取来为每个目标更改它们信息并打开“
If you click any of the options xcode normally does a pretty decent job of explaining what it does - if you don't understand (There are a few weird ones) then its probably best to leave them at whatever value they are.
如果您单击任何选项,xcode 通常会很好地解释它的作用 - 如果您不明白(有一些奇怪的),那么最好将它们保留为任何值。
回答by Mac
If you've set up a C++ project in xcode, you should be right to simply click "build" - no makefile twiddling required...
如果您已经在 xcode 中设置了一个 C++ 项目,那么只需单击“构建”就应该是正确的 - 不需要makefile twiddling ...