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

时间:2022-06-10 11:29:21

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 a which compiles into library a.dylib and header file a.h, the project is located in it's own directory.

我有一个名为a的C项目,它编译成库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.dylib into other frameworks but what do I do with a.h?

我可以将a.dylib拖到其他框架中但是我怎么处理a.h?

3 个解决方案

#1


22  

I figured it out.

我想到了。

I point to location of project a deployment directory (headers) to Search Path in project settings either:

我指出将项目部署目录(标题)项目的位置设置为项目设置中的搜索路径:

  • as Header Search Paths, if used as <a/a.h>
  • 作为标题搜索路径,如果用作
  • or into User Header Search Paths, if used as "a/a.h"
  • 或者用户头标题搜索路径,如果用作“a / a.h”

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

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

#2


3  

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.

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

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

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

# include "test.h"

#3


2  

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语句中添加路径的位置。您是否有理由不想将标题复制到项目文件中?

#1


22  

I figured it out.

我想到了。

I point to location of project a deployment directory (headers) to Search Path in project settings either:

我指出将项目部署目录(标题)项目的位置设置为项目设置中的搜索路径:

  • as Header Search Paths, if used as <a/a.h>
  • 作为标题搜索路径,如果用作
  • or into User Header Search Paths, if used as "a/a.h"
  • 或者用户头标题搜索路径,如果用作“a / a.h”

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

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

#2


3  

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.

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

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

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

# include "test.h"

#3


2  

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语句中添加路径的位置。您是否有理由不想将标题复制到项目文件中?