Handling messages from an external file/library

I've written a library (functions declared in a .h file and defined in a .cpp file) and I'd like to call its functions from a main program (main.cpp). A function in the library will run and more than once it needs to notify the main program about things so that the main program can figure out what do -- for instance, it could prompt the user for what to do or handle an error.

I think this is what any non-trivial program does but I don't know how to do it or even what terms to Google for.

Can anyone provide some help?
Last edited on
I'm afraid you're going to have to be a bit more specific.

Perhaps you should explain the actual specific issue/desire, and then one us can help form a design.

If you have multiple programs communicating with each other, this is called "inter-process communication" (IPC). But in your case, I don't see a need for IPC; it sounds like you just need your main function to have a loop that calls a function from your header. But your title doesn't really match your description.

e.g.
stuff.h header:
1
2
3
4
5
6
#ifndef STUFF_H
#define STUFF_H

bool do_stuff();

#endif 


stuff.cpp
1
2
3
4
5
6
7
8
9
10
11
12
#include "stuff.h"
#include <iostream>

bool do_stuff()
{
    using namespace std;
    cout << "Doing stuffs\n";
    cout << "Would you like to do more stuff? (y/n) ";
    char yesno;
    cin >> yesno;
    return (yesno == 'y');
}


main.cpp
1
2
3
4
5
6
#include "stuff.h"

int main()
{
    while (do_stuff()) { };
}


______________________________

In general, the two ways of a library to send information back to the user of the library is either with exceptions or return codes/error codes.
Last edited on
Thank you for the reply. I'll be more specific. This is only one program so no IPC.

I'm writing something that goes through a directory and lists all the files along with their modified dates and size. So the library function is called and when it finds a file it gets the date and size, then needs to send this information back to the main.cpp so the program can determine what to do.

What I've done that seems to work OK is this: there is a function in my main.cpp named doMessage(message_t msg) - where message_t is a struct I've defined to hold the file information. The library calls this function on each file it finds.

So the main program has to implement a doMessage() function. Is this the correct way of doing it?
Is this the correct way of doing it?

No. You should use a callback function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// Library header ////////////////////////////////////////////////

// my_library.h
#include <string>

struct FileInfo
{
    std::string name;
    size_t size;
};

void lib_func(int n, void(*f)(const FileInfo& fi));


// Library impl ////////////////////////////////////////////////

//#include "my_library.h"

void lib_func(int n, void(*f)(const FileInfo& fi))
{
    const char *a[] {"one", "two", "three"}; // fake file data
    FileInfo fi;
    for (int i = 0; i < n; ++i)
    {
        fi.name = a[i];
        fi.size = i;
        f(fi);
    }
}


// Client code ////////////////////////////////////////////////

#include <iostream>
//#include "my_library.h"

void my_callback(const FileInfo& fi)
{
    std::cout << fi.name << ": " << fi.size << '\n';
}

int main()
{
    lib_func(3, my_callback);
}

Last edited on
Topic archived. No new replies allowed.