I'm a C++ beginner coder. I'm trying to understand how to send notifications using libnotify-bin
library. I've read some guys saying don't to use system calls. Any clue?
1 Answer
Okay so this is how I did it. First install libnotify-dev
:
sudo apt-get install libnotify-dev
this will install the lib on your system and put the headers of the lib to /usr/include/libnotify/
You can take a look at the header files to find out how to use the lib. I did the following:
#include <libnotify/notify.h>
#include <iostream>
int main(int argc, char * argv[] )
{
notify_init("Sample");
NotifyNotification* n = notify_notification_new ("Hello world",
"some message text... bla bla",
0);
notify_notification_set_timeout(n, 10000); // 10 seconds
if (!notify_notification_show(n, 0))
{
std::cerr << "show has failed" << std::endl;
return -1;
}
return 0;
}
In order to build this type:
g++ hello_world.cc -o hello_world `pkg-config --cflags --libs libnotify`
And here is the result:
-
When I test your code, it works fine! Although, when I put it in my function I receive an error message from the compiler:
main.cpp:(.text+0x4d7): undefined reference to notify_init main.cpp:(.text+0x4ee): undefined reference to notify_notification_new main.cpp:(.text+0x503): undefined reference to notify_notification_set_timeout main.cpp:(.text+0x514): undefined reference to notify_notification_show
Feb 5, 2016 at 18:27 -
-
void notifyUser( const char *app_name, const char *summary, const char *body, const char *icon ) { notify_init( app_name ) ; NotifyNotification *notifyN = notify_notification_new( summary, body, icon ) ; notify_notification_set_timeout( notifyN, 10000 ) ; if ( !notify_notification_show( notifyN, 0 ) ) { std::cerr << "show has failed" << std::endl ; } }
Feb 5, 2016 at 18:34 -
@AleD no the function name doesn't matter you can name it whatever you want. This is probably the linking order problem. Feb 5, 2016 at 18:48
-
I got the same undefined reference error after adjusting the filename, whereupon I had moved it to the end of the command line. The order is important. The source file which references libnotify should come before the pkg-config part. The pkg-config adds the -l option to g++, which is order dependent. Jun 3, 2017 at 1:14
libnotify-bin
ends up using system calls, you cannot avoid them. System calls are how most things get done. Which guys? What, exactly did they say? I've heard "some guys" saying the Earth is flat.