11

For example, if I need Gtk+ include paths. How to use pkg-config gtk+-2.0 --cflags in Xcode project settings?

tvanfosson
  • 524,688
  • 99
  • 697
  • 795
shoumikhin
  • 1,327
  • 15
  • 23

5 Answers5

3

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. 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.

Lytol
  • 970
  • 6
  • 15
  • I've tried this too, and it's not ideal, but it's the best solution I've found so far. Ideally, there should be a Gtk framework for Xcode. There is one here, but the page also warns that it's not stable enough for end-user use: https://sourceforge.net/apps/trac/gtk-osx/wiki/Build#Framework – ptomato Nov 24 '10 at 09:47
  • After doing this, I still got Gtk/gtk.h no such file or directory in my xcode. Am I missing anything here? – Winston Chen Jan 08 '12 at 08:45
3
  1. Create an aggregate target
  2. In Build Phases, add a 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

  4. In Build Phases in your app target, add the aggregate target as a dependency
  5. Exclude MyApp.xcconfig in your version control

One drawback is that until you build the aggregate target directly or indirectly at least once, the autocomplete will not work properly.

Koen.
  • 25,449
  • 7
  • 83
  • 78
keithyip
  • 985
  • 7
  • 21
0
  • Use a makefile that uses pkg-config and any other shell tools to build a 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.

  • Optional: encapsulate the makefile build into a "Run Script" build step in XCode.

0

In addition to @keithyip's great answer, if linker flags are also needed, use 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

Yair
  • 58
  • 6
0

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. Just be sure to not add it into your source repository.

kainjow
  • 3,955
  • 1
  • 20
  • 17