#include <libnotify/notify.h>
#include <glib.h>
#include <unistd.h>
int main(int argc, char** argv)
{
if(argc == 3)
{
NotifyNotification *n;
notify_init("Test");
n = notify_notification_new (argv[1],argv[2], NULL, NULL);
notify_notification_set_timeout (n, 3000); //3 seconds
if (!notify_notification_show (n, NULL)) {
g_error("Failed to send notification.\n");
return 1;
}
g_object_unref(G_OBJECT(n));
}else{
g_print("Too few arguments (%d), 2 needed.\n", argc-1);
}
return 0;
}编译代码给出了“对”错误的“未定义引用”:
shadyabhi@shadyabhi-desktop:~/c$ gcc -Wall -o test libnotify.c `pkg-config --libs --cflags glib-2.0 gtk+-2.0`
/tmp/ccA2Q6xX.o: In function `main':
libnotify.c:(.text+0x20): undefined reference to `notify_init'
libnotify.c:(.text+0x4b): undefined reference to `notify_notification_new'
libnotify.c:(.text+0x60): undefined reference to `notify_notification_set_timeout'
libnotify.c:(.text+0x71): undefined reference to `notify_notification_show'
collect2: ld returned 1 exit status
shadyabhi@shadyabhi-desktop:~/c$我从这个博客中提取了代码。
发布于 2010-02-24 22:05:54
听起来你好像忘了传递-lnotify来链接到libnotify了。
发布于 2016-08-24 10:25:23
我还不能发表评论,所以我把这作为一个答复。
在对接受的问题的评论中,Abhijeet Rastogi询问如何知道gcc的论点应该是什么,虽然Ignacio Vazquez-Abrams正确地提到了pkg-config,但是还有更多:
这个神奇的"-lnotify“是gcc的链接器上的"-l”标志,上面附加了你想链接到的库。查看/usr/lib时,有一个名为libnotify.so的文件,该文件与"-lnotify“链接到程序中。因此,要链接到库,在/usr/lib中搜索相应的库文件,注意文件名,删除"lib-“和文件扩展名,并将其添加到"-l"-flag中。请注意,链接顺序很重要,因此您必须在其受抚养人之前包含依赖项。
现在,如果库中有一个.pc文件,您可以使用如下一行
gcc `pkg-config --cflags --libs libnotify` main.c ...来建立这个程序。在我的系统中,对pkg-config的调用扩展到
-pthread -I/usr/include/gdk-pixbuf-2.0 -I/usr/include/libpng16 -I/usr/include/glib-2.0 -I/usr/lib/glib-2.0/include -lnotify -lgdk_pixbuf-2.0 -lgio-2.0 -lgobject-2.0 -lglib-2.0因此,不需要显式地处理lib中山的glib和gtk依赖项。
https://stackoverflow.com/questions/2329905
复制相似问题