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.
另外,为什么静态库文件与动态库文件完全不同?似乎差异主要在于如何找到库(与您的应用程序一起打包而不是依赖于目标计算机上的状态),而不是在库本身的功能或代码中。如果有人能澄清这一点,那就太棒了。
1 个解决方案
#1
17
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(如果您需要编译遗留代码,例)。
#1
17
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(如果您需要编译遗留代码,例)。