acib708
acib708

Reputation: 1583

GLib usage with XCode

I've been trying to use GLib in a project. I work with XCode. I downloaded Glib via Homebrew:

brew install glib

I then run

brew test glib

Which outputs

/usr/bin/clang -o test test.c -I/usr/local/Cellar/glib/2.36.3/include/glib-2.0 -I/usr/local/Cellar/glib/2.36.3/lib/glib-2.0/include -I/usr/local/opt/gettext/include -L/usr/local/Cellar/glib/2.36.3/lib -L/usr/local/opt/gettext/lib -lglib-2.0 -lintl -Os -w -pipe -march=native -Qunused-arguments -mmacosx-version-min=10.9 -isysroot /Applications/Xcode5-DP6.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk

So, I copied all the -I arguments, and pasted them into XCode -> Project Settings -> Build Settings -> Other C Flags.

In a .c file inside my XCode project I have

    GList  *processList_p = NULL;      /* Pointer to the process list */

If I compile everything works, but then if I call a function from GLib, such as

//Create the new list element, append it to the list and return the pointer.
return g_list_append(processList, newProcess);

I get the error

Undefined symbols for architecture x86_64: "_g_list_append", referenced from: _CreateProcess in Scheduler.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)

I really have no idea what's going on, any help would be greatly appreciated.

Upvotes: 1

Views: 2087

Answers (4)

KunMing Xie
KunMing Xie

Reputation: 1667

  1. install pkg-config if not exists

    brew install pkg-config

  2. list cflags

    pkg-config --cflags --libs glib-2.0

  3. manual choose pkg if not exists

    PKG_CONFIG_PATH=/path/to/pkgconfig/ pkg-config --cflags --libs glib-2.0

Upvotes: 0

raf
raf

Reputation: 535

see Setting C++ compile flags in xcode

in short:
add result of
pkg-config --cflags --libs glib-2.0
to Build Settings -> Linking -> Other Linker Flags NOT Build Settings -> Custom Compiler Flags -> Other C Flags

for some reason using 'pkg-config --cflags --libs glib-2.0' directly does not work yielding:

clang: error: no such file or directory: 'pkg-config --cflags --libs glib-2.0'

Upvotes: 4

mitnk
mitnk

Reputation: 3307

As @Dash83 said, it's a linker issue. You need to let compiler to know the locations.

Outputs on my machine:

$ pkg-config --cflags glib-2.0

-I/usr/local/Cellar/glib/2.46.2/include/glib-2.0 -I/usr/local/Cellar/glib/2.46.2/lib/glib-2.0/include -I/usr/local/opt/gettext/include

$ pkg-config --libs glib-2.0

-L/usr/local/Cellar/glib/2.46.2/lib -L/usr/local/opt/gettext/lib -lglib-2.0 -lintl

Pass them to clang:

$ clang your_program.c -o your_program `pkg-config --cflags glib-2.0` `pkg-config --libs glib-2.0`

It compiles on my side.

Upvotes: 2

Dash83
Dash83

Reputation: 1468

That is a linker problem my friend. I work with glib on vim on the console and came across your question looking for a quick start guide to use glib in xcode, so I can't really tell you how to fix it on xcode, but I can guarantee you that's a problem with the linker options/flags.

Upvotes: 0

Related Questions