3

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?

  • 3
    SInce 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. – waltinator Feb 5 '16 at 15:35
  • I'm sorry, @waltinator, I don't find those "some guys". They argued that isn't a good idea to use system call instead libraries. They didn't explained because. I think may be because security issues. – AleD Feb 5 '16 at 17:10
7

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:

screenshot

  • 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 – AleD Feb 5 '16 at 18:27
  • My function isn't main... Is this a problem? – AleD Feb 5 '16 at 18:32
  • 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 ; } } – AleD Feb 5 '16 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. – Dimitri Podborski Feb 5 '16 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. – Daniel Stevens Jun 3 '17 at 1:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.