objective-c Xcode - 如何将 c 库和头文件包含到可可项目中?

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

Xcode - how to include c library and header file to cocoa project?

objective-ccocoaxcodexcodebuild

提问by stefanB

How do I add c library to Xcode Cocoa project?

如何将 c 库添加到 Xcode Cocoa 项目?

Or what is the best option, I don't want to copy them into Cocoa project directory.

或者什么是最好的选择,我不想将它们复制到 Cocoa 项目目录中。

I have a C project called awhich compiles into library a.dyliband header file a.h, the project is located in it's own directory.

我有一个 C 项目a,它编译成库a.dylib和头文件a.h,该项目位于它自己的目录中。

I want to use this library from my objective-c application in Xcode.

我想在 Xcode 中的 Objective-c 应用程序中使用这个库。

How do I add the header file and library to my Xcode project?

如何将头文件和库添加到我的 Xcode 项目中?

I can drag the a.dylibinto other frameworksbut what do I do with a.h?

我可以拖a.dylib进去,other frameworks但我该怎么办a.h

回答by stefanB

I figured it out.

我想到了。

I point to location of project adeployment directory (headers) to Search Pathin project settingseither:

我点了项目的位置a部署目录(头)到搜索路径项目设置之一:

  • as Header Search Paths, if used as <a/a.h>
  • or into User Header Search Paths, if used as "a/a.h"
  • 作为Header Search Paths,如果用作<a/a.h>
  • 或 into User Header Search Paths,如果用作"a/a.h"

As for library I just drag it to Xcode project and set it to refer to library instead of copy.

至于库,我只是将它拖到 Xcode 项目并将其设置为引用库而不是复制。

回答by Siddhartha Saif

Here are the steps before adding a header file test.h in your project. Here is the files location root -> Library -> include -> test.h

以下是在项目中添加头文件 test.h 之前的步骤。这是文件位置 root -> Library -> include -> test.h

  1. click on build settings

  2. Find User Header Search path. add your header file location here. add following value to Debug, Release and Any Architecture field. $(SRCROOT)/Library/include. Your project Root is the folder that contains your project, it conatins .xcodeproj file.

  1. 点击构建设置

  2. 查找用户标题搜索路径。在此处添加您的头文件位置。将以下值添加到 Debug、Release 和 Any Architecture 字段。$(SRCROOT)/图书馆/包括。您的项目根目录是包含您的项目的文件夹,它包含 .xcodeproj 文件。

After adding path you will be able to add header in like this

添加路径后,您将能够像这样添加标题

# include "test.h"

回答by Ben Gottlieb

You can drag them both the .a and .h files to Xcode, but make sure to not check the "Copy items to project folder". As for referencing the header file, you'll need to put it in a place where you can add a path to it in your #include/#import statements. Is there a reason you don't want to copy the header to your project file?

您可以将它们的 .a 和 .h 文件拖到 Xcode,但请确保不要选中“将项目复制到项目文件夹”。至于引用头文件,您需要将它放在可以在#include/#import 语句中添加路径的地方。您是否有理由不想将标题复制到您的项目文件中?