I am writing a cross platform shared library (.so
in linux and .dll
in windows) using C. Currently when there is a error, library functions returns the
Best practice (IMHO) is for a library to not print anything to stderr (or stdout), because they may not even be present. In addition to the GUI situation, you also have the use case of a server application that doesn't have a "console", and may want to be logging errors using a function like syslog().
Some approaches for handling error information without printing it directly:
return a numeric error code, and provide a function for turning it into a string
return a struct/object error code, which contains additional information
provide a function on a "session" object that returns info about the last error
allow the caller to register a callback that's invoked in the event of an error
The one exception to the "don't write to stderr from a library" rule that I'm reasonably comfortable with is if a library has a "debug mode" parameter that enables logging of detailed info to stderr.