如何在 C++ xcode 项目中使用动态共享库?

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

How do I use a dynamic shared library in a C++ xcode project?

c++xcodedylib

提问by Alexander Rafferty

I have created a new project in XCode (using the template "STL C++ library"), and have made a little test function void hello();that prints to the console.

我在 XCode 中创建了一个新项目(使用模板“STL C++ 库”),并制作了一个void hello();打印到控制台的小测试函数。

It compiles fine, and I am able to export a dylib file.

它编译得很好,我可以导出一个 dylib 文件。

What I can't figure out is how to create a project that then links to this file and can call the hello()method.

我无法弄清楚的是如何创建一个项目,然后链接到该文件并可以调用该hello()方法。

Any advice would be greatly appreciated.

任何建议将不胜感激。

Alex.

亚历克斯。

采纳答案by Mark Szymczyk

Create another Xcode project, such as an application project. In your example you could create a command-line tool project that uses C++.

创建另一个 Xcode 项目,例如应用程序项目。在您的示例中,您可以创建一个使用 C++ 的命令行工具项目。

After creating the project add the dylib to the target's Link Binary with Libraries build phase. In Xcode 4, select the project from the project navigator to open the project editor. Select the 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 add your library.

创建项目后,将 dylib 添加到目标的 Link Binary with Libraries 构建阶段。在 Xcode 4 中,从项目导航器中选择项目以打开项目编辑器。从项目编辑器左侧的目标列表中选择目标。单击编辑器顶部的 Build Phases 按钮。单击 Link Binary with Libraries 构建阶段旁边的显示三角形。单击 + 按钮添加您的库。

When you finish adding the dylib to your project, include the header file for your library in your code, and you should be able to call the hello() function. You may also need to add a search path to your library to the Library Search Paths build setting to use your library.

将 dylib 添加到项目后,将库的头文件包含在代码中,您应该能够调用 hello() 函数。您可能还需要将库的搜索路径添加到库搜索路径构建设置以使用您的库。