XCode 中的 C/C++ 库和 STL C++ 库有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12946106/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's the difference between C/C++ Library and STL C++ Library in XCode?
提问by Tony
I'm trying to create a C++ library in Xcode and I'm not sure whether to choose the C/C++ Library or the STL C++ Library option? I noticed that the STL C++ Library
option doesn't let you create a static library and force you to create a dynamic library. However, the C/C++ Library
option also lets you create a dynamic library in addition to creating a static library.
我正在尝试在 Xcode 中创建 C++ 库,但不确定是选择 C/C++ 库还是 STL C++ 库选项?我注意到该STL C++ Library
选项不允许您创建静态库并强制您创建动态库。但是,C/C++ Library
除了创建静态库之外,该选项还允许您创建动态库。
What's the difference between these two options and when should I use each? I read the descriptions below the options but unfortunately they are not terribly helpful.
这两个选项之间有什么区别,我应该什么时候使用它们?我阅读了选项下方的说明,但不幸的是,它们并没有太大帮助。
On another note, why is static library file different from dynamic library file at all? It seems that the difference is primarily in how the library is found (packaged with your app vs. relying on presence on the target machine), not in the functioning or code of the library itself. It would be great if someone can clarify this.
另一方面,为什么静态库文件与动态库文件完全不同?似乎区别主要在于库的发现方式(与您的应用程序一起打包,而不是依赖于目标机器上的存在),而不是库本身的功能或代码。如果有人能澄清这一点,那就太好了。
采纳答案by user1820003
A statically linked library cannot be loaded at run-time but must be incorporated into your binary file when you link your executable. This means that all entry points to the code in the statically linked library are well defined and their addresses will not change relative to the start of your executable code (thus "static").
无法在运行时加载静态链接库,但必须在链接可执行文件时将其合并到二进制文件中。这意味着静态链接库中代码的所有入口点都是明确定义的,并且它们的地址相对于可执行代码的开头(因此是“静态”)不会改变。
With dynamically loaded libraries, there is no way of knowing where the code will be, so when the library is loaded at run-time, a certain amount of performance overhead is necessary to "bind" the loaded code. Essentially, the linking is deferred to run-time, which is why it is also sometimes known as "late binding".
使用动态加载的库,无法知道代码将在哪里,因此在运行时加载库时,需要一定量的性能开销来“绑定”加载的代码。本质上,链接被推迟到运行时,这就是为什么它有时也被称为“后期绑定”。
Which you choose to implement depends on your usage requirements. If you want a self-contained executable that the user can simply drag and drop into his applications folder without having to worry about dependencies, statically linking your libraries may be the way to go.
您选择实施哪个取决于您的使用要求。如果您想要一个自包含的可执行文件,用户可以简单地将其拖放到他的应用程序文件夹中而不必担心依赖项,那么静态链接您的库可能是可行的方法。
But for larger scaled projects, in apps that provide a ton of functionality, it may be prohibitive and unnecessary to load all the functionality at once. Providing a set of dynamically loadable libraries can both save memory and shorten start-up time. Then, as the user accesses features, the relevant code is loaded, and possibly features that haven't been used in a while can be unloaded.
但是对于更大规模的项目,在提供大量功能的应用程序中,一次加载所有功能可能会令人望而却步,也没有必要。提供一组可动态加载的库既可以节省内存又可以缩短启动时间。然后,当用户访问功能时,会加载相关代码,并且可能会卸载一段时间未使用的功能。
Additionally, if you make changes to the code, you might be able to simply redistribute the one or two libraries rather than having to re-compile and re-link and re-distribute the entire executable. And need I mention the prospect of plugins?
此外,如果您对代码进行了更改,您或许可以简单地重新分发一两个库,而不必重新编译、重新链接和重新分发整个可执行文件。我需要提到插件的前景吗?
The differences between the two templates above are subtle. Both compile C according to the GNU99 standard. But the C/C++ Library template sets up xcode to compile C++ according to the C++/GNU++0x "standard". C++/GNU++0x was later officially published as C++/GNU++11 in 2011. Both templates default to using libc++, but the STL C++ Template allows you to select to link against the older libstdc++ instead. Why would you do this? If your code links against libc++ but you are also linking against other libraries that reference libstdc++, and you run across conflicting symbols, you might be able to resolve this by linking against libstdc++ instead. The STL C++ Library template also allows you to request that the compiler stick to the C++11 standard, excluding the GNU++11 extensions, or go back to C++/GNU++98 (if you need to compile legacy code, for example).
上述两个模板之间的差异是微妙的。两者都根据 GNU99 标准编译 C。但是 C/C++ 库模板根据 C++/GNU++0x“标准”设置 xcode 来编译 C++。C++/GNU++0x 后来在 2011 年正式发布为 C++/GNU++11。两个模板都默认使用 libc++,但 STL C++ 模板允许您选择链接到旧的 libstdc++。你为什么要这样做?如果您的代码链接到 libc++,但您也链接到引用 libstdc++ 的其他库,并且遇到冲突的符号,您或许可以通过链接 libstdc++ 来解决此问题。STL C++ 库模板还允许您要求编译器坚持 C++11 标准,不包括 GNU++11 扩展,或返回到 C++/GNU++98(如果您需要编译遗留代码,