6

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?

2
  • 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
    Commented Feb 5, 2016 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.
    – aledruetta
    Commented Feb 5, 2016 at 17:10

1 Answer 1

13

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

5
  • 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
    – aledruetta
    Commented Feb 5, 2016 at 18:27
  • My function isn't main... Is this a problem?
    – aledruetta
    Commented Feb 5, 2016 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 ; } }
    – aledruetta
    Commented 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. Commented 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. Commented Jun 3, 2017 at 1:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .