Creating a portable library to run on both linux and windows

前端 未结 5 1728
野趣味
野趣味 2020-12-23 18:20
gcc (GCC) 4.7.2

Hello,

I am creating a shared library that will compile on linux and a dll that will compile on windows using the same sour

5条回答
  •  青春惊慌失措
    2020-12-23 18:30

    extern "C" basically means that you are telling the compiler not to mangle your function name. Mangling is the process of "encoding" function names for later execution and is quite different in C and C++ as C++ can have different functions having the same name (via overloading etc...).

    In C++ source, what is the effect of extern "C"?

    Once compiled these functions can be called from anywhere but you might want to be sure what kind of library you are creating (static or dynamic) before you start.

    Also I recommend you not using DEFINES like you do in the same file for portability purposes because of the maintenance or readability problems you might encounter later in the development. I would create a basic file defining an interface which is fully portable to WIN and UNIX then create two other libraries implementing the interface but for different platforms.

    For example you can have: AbstractInterface.h, WinInterface.h, UnixInterface.h

    Then only compile the ones you need depending on the platform.

提交回复
热议问题