简体   繁体   English

GTK + gcc:链接时未定义对所有gtk函数的引用

[英]GTK+ gcc: undefined reference to all gtk functions when linking

I'm coding an interface with GTK+ for chat using XMPP protocol. 我正在使用GTK +为使用XMPP协议的聊天接口编写代码。 My code worked fine when I compiled it like : 像这样编译时,我的代码工作正常:

gcc login_gtk.c `pkg-config --cflags --libs gtk+-3.0`

But since I add my file to my project, and I'm using a Makefile, it doesn't compile anymore. 但是由于我将文件添加到项目中,并且使用的是Makefile,因此该文件不再编译。 I guess the problem comes from my Makefile. 我猜问题出在我的Makefile中。

Compiler errors : 编译器错误:

gcc -o -W -Wall -Wextra -Werror -g -I./Includes/. -pthread -I/usr/include/gtk-3.0 -I/usr/include/atk-1.0 -I/usr/include/at-spi2-atk/2.0 -I/usr/include/pango-1.0 -I/usr/include/gio-unix-2.0/ -I/usr/include/cairo -I/usr/include/gdk-pixbuf-2.0 -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -I/usr/include/freetype2 -I/usr/include/pixman-1 -I/usr/include/libpng12  -lgtk-3 -lgdk-3 -latk-1.0 -lgio-2.0 -lpangocairo-1.0 -lgdk_pixbuf-2.0 -lcairo-gobject -lpango-1.0 -lcairo -lgobject-2.0 -lglib-2.0     -c -o Sources/login_gtk.o Sources/login_gtk.c
gcc -o -W -Wall -Wextra -Werror -g -I./Includes/. -pthread -I/usr/include/gtk-3.0 -I/usr/include/atk-1.0 -I/usr/include/at-spi2-atk/2.0 -I/usr/include/pango-1.0 -I/usr/include/gio-unix-2.0/ -I/usr/include/cairo -I/usr/include/gdk-pixbuf-2.0 -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -I/usr/include/freetype2 -I/usr/include/pixman-1 -I/usr/include/libpng12  -lgtk-3 -lgdk-3 -latk-1.0 -lgio-2.0 -lpangocairo-1.0 -lgdk_pixbuf-2.0 -lcairo-gobject -lpango-1.0 -lcairo -lgobject-2.0 -lglib-2.0     -c -o Sources/circular_buffer.o Sources/circular_buffer.c
gcc -o -W -Wall -Wextra -Werror -g -I./Includes/. -pthread -I/usr/include/gtk-3.0 -I/usr/include/atk-1.0 -I/usr/include/at-spi2-atk/2.0 -I/usr/include/pango-1.0 -I/usr/include/gio-unix-2.0/ -I/usr/include/cairo -I/usr/include/gdk-pixbuf-2.0 -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -I/usr/include/freetype2 -I/usr/include/pixman-1 -I/usr/include/libpng12  -lgtk-3 -lgdk-3 -latk-1.0 -lgio-2.0 -lpangocairo-1.0 -lgdk_pixbuf-2.0 -lcairo-gobject -lpango-1.0 -lcairo -lgobject-2.0 -lglib-2.0     -c -o Sources/welcome_server.o Sources/welcome_server.c
gcc -o -W -Wall -Wextra -Werror -g -I./Includes/. -pthread -I/usr/include/gtk-3.0 -I/usr/include/atk-1.0 -I/usr/include/at-spi2-atk/2.0 -I/usr/include/pango-1.0 -I/usr/include/gio-unix-2.0/ -I/usr/include/cairo -I/usr/include/gdk-pixbuf-2.0 -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -I/usr/include/freetype2 -I/usr/include/pixman-1 -I/usr/include/libpng12  -lgtk-3 -lgdk-3 -latk-1.0 -lgio-2.0 -lpangocairo-1.0 -lgdk_pixbuf-2.0 -lcairo-gobject -lpango-1.0 -lcairo -lgobject-2.0 -lglib-2.0     -c -o Sources/main_loop.o Sources/main_loop.c
gcc -o -W -Wall -Wextra -Werror -g -I./Includes/. -pthread -I/usr/include/gtk-3.0 -I/usr/include/atk-1.0 -I/usr/include/at-spi2-atk/2.0 -I/usr/include/pango-1.0 -I/usr/include/gio-unix-2.0/ -I/usr/include/cairo -I/usr/include/gdk-pixbuf-2.0 -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -I/usr/include/freetype2 -I/usr/include/pixman-1 -I/usr/include/libpng12  -lgtk-3 -lgdk-3 -latk-1.0 -lgio-2.0 -lpangocairo-1.0 -lgdk_pixbuf-2.0 -lcairo-gobject -lpango-1.0 -lcairo -lgobject-2.0 -lglib-2.0     -c -o Sources/client_init.o Sources/client_init.c
gcc -o -W -Wall -Wextra -Werror -g -I./Includes/. -pthread -I/usr/include/gtk-3.0 -I/usr/include/atk-1.0 -I/usr/include/at-spi2-atk/2.0 -I/usr/include/pango-1.0 -I/usr/include/gio-unix-2.0/ -I/usr/include/cairo -I/usr/include/gdk-pixbuf-2.0 -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -I/usr/include/freetype2 -I/usr/include/pixman-1 -I/usr/include/libpng12  -lgtk-3 -lgdk-3 -latk-1.0 -lgio-2.0 -lpangocairo-1.0 -lgdk_pixbuf-2.0 -lcairo-gobject -lpango-1.0 -lcairo -lgobject-2.0 -lglib-2.0     -c -o Sources/main.o Sources/main.c
gcc -o x2p ./Sources/login_gtk.o ./Sources/circular_buffer.o ./Sources/welcome_server.o ./Sources/main_loop.o ./Sources/client_init.o ./Sources/main.o -lexpat
./Sources/login_gtk.o: In function `login_form':
./Sources/login_gtk.c:10: undefined reference to `gtk_entry_new'
./Sources/login_gtk.c:11: undefined reference to `gtk_entry_get_type'
./Sources/login_gtk.c:11: undefined reference to `g_type_check_instance_cast'
./Sources/login_gtk.c:11: undefined reference to `gtk_entry_set_text'
./Sources/login_gtk.c:12: undefined reference to `gtk_box_get_type'
./Sources/login_gtk.c:12: undefined reference to `g_type_check_instance_cast'
./Sources/login_gtk.c:12: undefined reference to `gtk_box_pack_start'
./Sources/login_gtk.c:13: undefined reference to `gtk_entry_get_type'
./Sources/login_gtk.c:13: undefined reference to `g_type_check_instance_cast'

Makefile: 生成文件:

NAME            =       x2p

SRCS            =                                   \
                    ./Sources/login_gtk.c           \
                    ./Sources/circular_buffer.c     \
                    ./Sources/welcome_server.c      \
                    ./Sources/main_loop.c           \
                    ./Sources/client_init.c         \
                    ./Sources/main.c                \                                                                                                      
OBJS            =       $(SRCS:.c=.o)

CC              =       gcc -o

CFLAGS          =       -W -Wall -Wextra -Werror -g -I./Includes/. $(shell pkg-config --cflags --libs gtk+-3.0)

LDFLAGS         +=      -lexpat

RM              =       rm -f

CAR             =       *\# *~ *core*

all             :       $(NAME)

$(NAME)         :       $(OBJS)
                        $(CC) $(NAME) $(OBJS) $(LDFLAGS)
clean           :
                        $(RM) $(OBJS) $(CAR)

fclean          :       clean
                        $(RM) $(NAME)

re              :       fclean all

And part of login_gtk.c: 以及login_gtk.c的一部分:

#include        <gtk/gtk.h>


void            location_form(GtkWidget *pVBox)
{
  GtkWidget     *pList;

  pList = gtk_combo_box_text_new();
  gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(pList), "LOCATION");
  gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(pList), "WORK");
  gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(pList), "HOME");
  gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(pList), "SCHOOL");
  gtk_combo_box_set_active(GTK_COMBO_BOX(pList), 0);
  gtk_box_pack_start (GTK_BOX(pVBox), pList, FALSE, TRUE, 0);
}

The order is wrong : this is an incredibly common mistake I continue to meet. 顺序错误 :这是我继续遇到的一个非常常见的错误。 Previous versions of gcc were less picky on the argument order and this spread the wrong idea pkgconfig --cflags --libs works in every position. 以前版本的gcc在参数顺序上不太挑剔,这散布了错误的思想pkgconfig --cflags --libs可以在每个位置使用。

Check out this email for info on proper flags ordering. 请查看此电子邮件,以获取有关正确标记顺序的信息。 In short: 简而言之:

gcc $(pkg-config --cflags ...) myapp.c $(pkg-config --libs ...)

You haven't included the libraries in your Makefile. 您尚未将库包含在Makefile中。

You must add -L<path to your lib directory and -lgtk+-3.0 to your linker line. 您必须将-L<path to your lib directory添加-L<path to your lib directory并将-lgtk+-3.0到链接器行。

CLFAGS missing here ... CLFAGS在这里失踪...

$(NAME)         :       $(OBJS)
                    $(CC) $(NAME) $(OBJS) $(LDFLAGS)

or here: 或在这里:

LDFLAGS         +=      -lexpat

I finally reach to compile by doing this thing the Makefile : 我最终通过执行Makefile来进行编译:

NAME            =       x2p

SRCS            =                                       \
                        ./Sources/circular_buffer.c     \
                        ./Sources/welcome_server.c      \
                        ./Sources/main_loop.c           \
                        ./Sources/client_init.c         \
                        ./Sources/main.c                \                                            

OBJS            =       $(SRCS:.c=.o)

LLFLAGS         =       $(shell pkg-config --cflags --libs gtk+-3.0)

CC              =       gcc

O               =       -o

CFLAGS          =       -W -Wall -Wextra -Werror -g -I./Includes/.


LDFLAGS         +=      -lexpat

RM              =       rm -f

CAR             =       *\# *~ *core*

all             :       $(NAME)

$(NAME)         :       $(OBJS)
                        $(CC) -I./Includes/. ./Sources/login_gtk.c $(LLFLAGS) $(O) $(NAME) $(OBJS) $(LDFLAGS)
clean           :
                        $(RM) $(OBJS) $(CAR)

fclean          :       clean
                        $(RM) $(NAME)

re              :       fclean all

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

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