I'm looking for some help in how to setup something. I have some programs where I call a function defined in a separate cpp file (call it the library function) and that function will generate messages (say a string) that the program needs to collect. This library function is in a separate file because I want to use it in more than one program.
The way I have it setup is that each program has a HandleMessage() function that the library function calls when it needs to send a message to the program. So in the library cpp files I need to #include something from the program.
This doesn't seem like the best way to have this setup. Is there a better way? I'm concerned about creating circular dependencies and this setup doesn't seem right.
You can pass a function to another function. What you need to provide is the function signature, but then you don't need to know where this function come from. See:
If EXE calls the DLL's function; I should expect that the DLL returns a value directly to the EXE.
The DLL can also call the EXE: the EXE must simply export the proper function(s) so that the DLL can load them (the same way an EXE loads a DLL's functions, via LoadLibrary).
There is very little difference between an EXE and a DLL.
However, this is more typically done by the EXE passing the DLL the address of a "callback" function that the DLL can use.
So in the library cpp files I need to #include something from the program.
Not necessarily. The library function needs to know the function signature of the callback.
You can place the function prototype for HandleMessage() in the library's header file even though that function is not in the library. Each program that wants to use the library would include the library's header file. No need for the library to include anything from the calling program.