简体   繁体   English

如何在Windows和Linux下链接到OS C库?

[英]How does linking to OS C libraries under Windows and Linux work?

I understand Linux ships with ac library, which implements the ISO C functions and system call functions, and that this library is there to be linked against when developing C. However, different c compilers do not necessarily produce linkable code (eg one might pad datastructures used in function arguments differently from another). 我理解Linux附带了ac库,它实现了ISO C函数和系统调用函数,并且这个库在开发C时可以链接。但是,不同的c编译器不一定产生可链接的代码(例如,可能填充数据结构)在函数参数中使用的与另一个不同)。 How is the built-in c library meant to be linked to when I could use any compiler to compile my C? 当我可以使用任何编译器编译我的C时,内置的c库是如何链接的? Is the story any different for static versus dynamic linking? 静态与动态链接的故事有何不同?

Under Windows on the other hand, each compiler provides its own standard library, which solves part of the problem, but system calls are still in a single set of DLLs. 另一方面,在Windows下,每个编译器都提供自己的标准库,它解决了部分问题,但系统调用仍然在一组DLL中。 How are C applications linked to these DLLs successfully? C应用程序如何成功链接到这些DLL? How about different languages? 不同语言怎么样? (The same DLLs can be used by pre-.Net Visual Basic, etc.) (相同的DLL可以由pre.Net Visual Basic等使用)

Each platform has some "calling conventions" that each C implementation must adhere to in order to be able to talk to the operating system correctly. 每个平台都有一些“调用约定”,每个C实现必须遵守这些约定才能正确地与操作系统通信。 For Windows, for example, all OS-based functions have to be called using stdcall convention, as opposed to the default C convention of cdecl . 例如,对于Windows,必须使用stdcall约定调用所有基于OS的函数,而不是cdecl的默认C约定。

In Linux, since the standard C library (and kernel) is compiled using GCC, any other compilers for Linux must make sure their calling conventions are compatible to the one used by GCC. 在Linux中,由于标准C库(和内核)是使用GCC编译的,因此Linux的任何其他编译器必须确保其调用约定与GCC使用的约定兼容。

Compilers do come with their implementations of the standard library. 编译器确实带有标准库的实现。 It's just that under Linux it's assumed that any compiler will follow the same conventions the version of GCC that compiled the library had. 只是在Linux下,假设任何编译器都遵循与编译库的GCC版本相同的约定。

As of interoperability, it can be easier than you think. 在互操作性方面,它可能比您想象的要容易。 There are established calling conventions that will allow compilers to produce a valid call to a function, even if the function wasn't compiled with the same software. 已建立的调用约定将允许编译器生成对函数的有效调用,即使该函数未使用相同的软件编译。

As of structures and padding, you'll notice that most frameworks work with opaque types, that is, pointers to structures. 从结构和填充开始,您会注意到大多数框架都使用不透明类型,即指向结构的指针。 Often, the structure's layout isn't even available to clients. 通常,结构的布局甚至不能供客户使用。 As such, they never works with the actual data, only pointers to the data, which clears the padding issue. 因此,它们永远不会使用实际数据,只能指向数据,这会清除填充问题。

Standards. 标准。 You'll note that stdlib stuff operates on primitive values and arrays - and the standard for that stuff is pretty explicit on how things are to be done. 你会注意到stdlib的东西在原始值和数组上运行 - 而且这些东西的标准非常清楚如何完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C / C ++如何选择在Linux下使用gpio? - How does C/C++ select work with gpio under Linux? 在Linux上具有静态C运行时的共享库和可执行链接。 他们每个人都有像Windows一样的单独堆吗? - Shared Libraries and Executable linking with static C run time on Linux. Does each of them have separate heap like Windows? 如何编译 C Linux 库以用于 Windows? - How to compile C Linux libraries for use in Windows? Linux 原生 C 库:如何配置 Visual Studio Code 以在 Windows 上使用它们? - Linux-native C libraries: How to configure Visual Studio Code to work with them on Windows? 在 C/C++ 程序中,系统(Windows、Linux 和 Mac OS X)如何调用 main() 函数? - In a C/C++ program, how does the system (Windows, Linux, and Mac OS X) call the main() function? Python 是否依赖于 C 编译器,例如 ZEDC9F0A5A3D57797BF3647 中的 GCC? Python 依赖于哪些操作系统库? - Does Python has dependency on C compilers like GCC in Linux? What OS libraries does Python depends on? 在OS X和Linux下使用automake构建C - Building C with automake under OS X and Linux 如何在 Linux 上安装 C 库的手册页? - How does one install man pages for C libraries on Linux? 在Windows中链接库 - Linking libraries in Windows C / C ++动态链接如何在不同平台上工作? - C/C++ How Does Dynamic Linking Work On Different Platforms?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM