开发者

Shared libraries and language standards

AFAIK, neither C99 nor C++ standards are aware of shared libraries. Why is that?

EDIT: 开发者_如何学运维What is a good practice of writing shared libs without duplicating Linux/Windows versions?

Thanks.


Because a shared library is a feature of the OS.

Nothing to do with any particular language.


I can think of a few reasons why this makes sense in 1999 (as in the case of C99) or even 2011:

  • There are still systems where C and C++ are used that don't have shared libraries. (Think embedded.) If the standard mandated something, life is needlessly difficult for compiler/library implementers who are targetting platforms where the question of shared libraries is irrelevant.

  • Different operating systems make different design choices with regard to shared libraries. If the standard mandated something, it would limit those choices. There is also a lot of icky legacy here to consider as shared library implementations have evolved over time.

As is the case with many things in C and C++, other, more platform-specific standards do a fine enough job here where the language itself has left things unspecified. According to manpages I just looked up, POSIX.1-2001 specifies dlopen et al. If you are targetting Windows you know where to find LoadLibrary/GetProcAddress. __declspec requirements for Windows can also be wrapped in a macro. If you care about both Windows and POSIXy systems it's not too hard to write a layer that does the appropriate thing. I'm sure there are lots already written and available.


"How to write shared libraries" by Ulrich Drepper,
read the first page about them and their history.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜