Sunny
Sunny

Reputation: 10075

Glib linking error for g_socket_new

I am trying to learn glib and gObject for a networking project.

Here is the command used to compile (after using pkg-config to get output):

gcc socket1.c -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -lgobject-2.0 -lglib-2.0

socket1.c, after stripping, to identify the problem is:

 #include <glib.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
 #include <fcntl.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <gio/gio.h>

 static  GSocket *mySocket;
 int main(int argc, char **argv) {

   GError *err1 = NULL;

   mySocket = g_socket_new ( G_SOCKET_FAMILY_IPV4,
                 G_SOCKET_TYPE_STREAM,
                 G_SOCKET_PROTOCOL_TCP,
                 &err1);

 }

The error is:

  /tmp/ccKIEXOi.o: In function `main':
  socket1.c:(.text+0x3d): undefined reference to `g_socket_new'
  collect2: error: ld returned 1 exit status

I have tried to use readelf -Ws and nm to see if I could see g_new_socket in any of the .so files linked. I did not see any. Is there a separate library I need to link to? Where/which is it?

Upvotes: 1

Views: 1070

Answers (1)

Philip Withnall
Philip Withnall

Reputation: 5703

You need to link to libgio-2.0.so, which is the third part of GLib (GLib, GObject, GIO). You can get its compiler and linker flags from pkg-config using pkg-config --cflags --libs gio-2.0.

You can tell GSocket is in GIO by looking at the documentation — it’s in the GIO manual: https://developer.gnome.org/gio/stable/GSocket.html.

Upvotes: 7

Related Questions