简体   繁体   English

如何从零开始在Xcode 4上构建C ++项目-链接现有和新的外部库

[英]How to build a C++ project on Xcode 4 from scratch - linking existing and new external libraries

I've been trying to build some C++ console-based programs in Xcode. 我一直在尝试在Xcode中构建一些基于C ++控制台的程序。 I managed to open a new project (Command-Line Tool -> C++) and to edit main.cpp, but I still can't figure out two things: 我设法打开一个新项目(命令行工具-> C ++)并编辑main.cpp,但我仍然无法弄清两件事:

  • how do I create new .cpp and .h files to build new libraries and especially how do I link them to main.cpp (I actually managed to create .h and .cpp files, but importing them in main.cpp with an #include doesn't seem to work) 我如何创建新的.cpp和.h文件以构建新库,尤其是如何将它们链接到main.cpp(我实际上设法创建了.h和.cpp文件,但使用#include将其导入main.cpp中似乎不起作用)
  • how do I link external existing libraries? 如何链接现有的外部库? I have this Stanford C++ Libraries (from CS106B on iTunes U) which are a set of .h headers and a .o archive. 我有这个斯坦福C ++库(来自iTunes U上的CS106B),它是一组.h标头和一个.o存档。

Thanks 谢谢

If you're going to use your project to write C++ libraries, the first thing you must do is add a target for each library you're going to write. 如果要使用项目编写C ++库,则必须做的第一件事就是为要编写的每个库添加一个目标。 Open the project editor by selecting your project from the project navigator on the left side of the project window. 通过从项目窗口左侧的项目导航器中选择项目,打开项目编辑器。 At the bottom of the project editor is an Add Target button. 在项目编辑器的底部是“添加目标”按钮。 Click the button. 点击按钮。 Select Framework and Library under Mac OS X. Select either C/C++ Library or STL C++ Library from the list of library targets. 在Mac OS X下选择“框架和库”。从库目标列表中选择C / C ++库或STL C ++库。 Click the Next button. 单击下一步按钮。 Enter the library name in the Product Name text field. 在“产品名称”文本字段中输入库名称。 Use the Type menu to create a dynamic or static library. 使用“类型”菜单创建动态或静态库。 Click the Finish button. 单击完成按钮。

After creating the library target you can add new C++ files to it. 创建库目标后,可以向其中添加新的C ++文件。 When creating a new C++ file, make sure you add the file to the library target, not the tool target. 创建新的C ++文件时,请确保将文件添加到库目标,而不是工具目标。 Select the library target checkbox in the Save panel. 在“保存”面板中选中库目标复选框。

At this point it's time to link libraries to the tool target. 现在是时候将库链接到工具目标了。 Select the tool target from the target list on the left side of the project editor. 从项目编辑器左侧的目标列表中选择工具目标。 Click the Build Phases button at the top of the editor. 单击编辑器顶部的“构建阶段”按钮。 Click the disclosure triangle next to the Link Binary with Libraries build phase. 单击“与库链接二进制文件”构建阶段旁边的显示三角形。 Click the + button to link to an existing library. 单击+按钮链接到现有库。

You may want Xcode to build your library targets automatically when you build the tool target. 您可能希望Xcode在构建工具目标时自动构建库目标。 To get Xcode to do this, click the disclosure triangle next to the Target Dependencies build phase. 要使Xcode做到这一点,请单击Target Dependencies构建阶段旁边的显示三角形。 Click the + button to add a dependency. 单击+按钮添加依赖项。 Choose one of your library targets in the sheet that opens. 在打开的工作表中选择一个库目标。

You're going to have to provide more information to get anyone to answer your question regarding getting main.cpp to include the new files you create. 您将必须提供更多信息,以使任何人回答有关使main.cpp包含您创建的新文件的问题。 At a minimum you have to provide code on how you are including the header files in main.cpp and what you mean when you say #include doesn't seem to work. 至少您必须提供有关如何在main.cpp中包括头文件以及您说#include似乎无效的含义的代码。 The following include should work: 以下包括应该工作:

#include "NewFile.h"

Where NewFile.h is the new header file you created. 其中,NewFile.h是您创建的新头文件。

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

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