简体   繁体   English

如何使用 pkg-config 设置 Xcode 中的包含路径?

[英]How to use pkg-config for setting include paths in Xcode?

For example, if I need Gtk+ include paths.例如,如果我需要 Gtk+ 包含路径。 How to use pkg-config gtk+-2.0 --cflags in Xcode project settings?如何在 Xcode 项目设置中使用pkg-config gtk+-2.0 --cflags

One option, but it's not very portable to other developers on the project -- you can just run pkg-config gtk+-2.0 --cflags in your Terminal and paste it into Build Settings -> Other C Flags . 一个选项,但它对项目中的其他开发人员来说不太方便 - 您可以在终端中运行pkg-config gtk+-2.0 --cflags并将其粘贴到Build Settings -> Other C Flags I'd be interested to hear how others deal with this in a more portable way though. 我很想知道其他人如何以更便携的方式处理这个问题。 Ideally, it would be nice to have pkg-config run at compile-time to make building more system-independent. 理想情况下,在编译时运行pkg-config以使构建更加独立于系统会更好。

  1. Create an aggregate target 创建聚合目标
  2. In Build Phases, add a Run Script 在Build Phases中,添加一个Run Script

     #!/bin/bash OTHER_CPLUSPLUSFLAGS="$(pkg-config gtk+-2.0 --cflags)" echo -e "OTHER_CPLUSPLUSFLAGS = \\$(inherited) ${OTHER_CPLUSPLUSFLAGS}" > MyApp.xcconfig 
  3. In Info in your project, set MyApp.xcconfig as your target configuration file 在项目的Info中,将MyApp.xcconfig设置为目标配置文件

  4. In Build Phases in your app target, add the aggregate target as a dependency 在应用目标的构建阶段中,将聚合目标添加为依赖关系
  5. Exclude MyApp.xcconfig in your version control 在版本控制中排除MyApp.xcconfig

One drawback is that until you build the aggregate target directly or indirectly at least once, the autocomplete will not work properly. 一个缺点是,在直接或间接构建聚合目标至少一次之前,自动完成将无法正常工作。

  • Use a makefile that uses pkg-config and any other shell tools to build a lib. 使用使用pkg-config和任何其他shell工具的makefile来构建lib。
  • Create a small, dependency-free API file for the lib that will compile in XCode, and include it and the built lib in the XCode build. 为将在XCode中编译的lib创建一个小的,无依赖性的API文件,并在XCode构建中包含它和构建的lib。

  • Optional: encapsulate the makefile build into a "Run Script" build step in XCode. 可选:将makefile构建封装到XCode中的“运行脚本”构建步骤中。

In addition to @keithyip 's great answer, if linker flags are also needed, use OTHER_LDFLAGS :除了@keithyip的出色答案之外,如果还需要 linker 标志,请使用OTHER_LDFLAGS

#!/bin/sh

OTHER_CPLUSPLUSFLAGS=$(pkg-config --cflags gtk+-2.0)
OTHER_LDFLAGS=$(pkg-config --libs gtk+-2.0)

echo "OTHER_CPLUSPLUSFLAGS = \$(inherited) ${OTHER_CPLUSPLUSFLAGS}" > MyApp.xcconfig
echo "OTHER_LDFLAGS = \$(inherited) ${OTHER_LDFLAGS}" >> MyApp.xcconfig

More portable would be to write a script that writes the output of pkg-config into an .xcconfig file and then include that in your project. 更便携的是编写一个脚本,将pkg-config的输出写入.xcconfig文件,然后将其包含在项目中。 Just be sure to not add it into your source repository. 请务必不要将其添加到源存储库中。

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

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