Error handling strategies in a shared library - C

前端 未结 4 1168
野的像风
野的像风 2021-02-14 10:40

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

4条回答
  •  爱一瞬间的悲伤
    2021-02-14 11:07

    For log messages, you should allow the client to supply a callback function to the library so then the client can decide what to do with them e.g. send to syslog or display in a window on the screen.

    For returning errors, you have three basic strategies:

    1. return an error code and have a function that translates it into a message
    2. pass a pointer parameter that points to an object that will hold error message information
    3. Have some global library object that contains error information from the last operation.

    Whatever you do, you don't want to just log the error message because the client might want to do something with it. e.g. present a dialog box.

    I'd probably go with 2 mostly.

提交回复
热议问题