简体   繁体   English

静态库,链接和依赖

[英]Static libraries, linking and dependencies

I have a static library that I want to distribute that has includes Foo.c/h and someone picks it up and includes my static library in their application. 我有一个我要分发的静态库,其中包含Foo.c / h,有人选择它并在其应用程序中包含我的静态库。

Say they also have Foo.c/h in their application. 假设他们的应用程序中也有Foo.c / h。 Will they have linking errors? 他们会有链接错误吗?

The name of a source file is not significant in the linking process. 源文件的名称在链接过程中并不重要。

If the file has the same contents, then you'll have a problem, assuming that the .c file contains exported symbols (eg non-static or non-template functions, or extern variables). 如果文件具有相同的内容,那么假设.c文件包含导出的符号(例如非静态或非模板函数或外部变量),则会出现问题。

It depends. 这取决于。 If the foo.c and foo.h define the same functions and/or variables, then yes, there will be multiple definition errors. 如果foo.c和foo.h定义了相同的函数和/或变量,那么肯定会有多个定义错误。 If they just have the same file names, but contain different code, there will be no problem. 如果它们只有相同的文件名,但包含不同的代码,则没有问题。

Linker an File Name 链接文件名
The linker itself has the least problems, but some IDE's will complain if the files are added - even if they are in different folders. 链接器本身问题最少,但是如果添加了文件,某些IDE会抱怨 - 即使它们位于不同的文件夹中。 But that's usually not much of a problem. 但这通常不是什么大问题。

Do not distribute a static library (without source) 不要分发静态库(没有源代码)

First, you shouldn't redistribute a static library alone. 首先,您不应该单独重新分发静态库。 The code - and the compatibility - depends on the compiler, and many of the common settings, like which run time library (static/dynamic, debug/release, happily the single threaded's are gone from VC), some C++ settings such as exception handling, virtual member function pointer representation and others. 代码 - 兼容性 - 取决于编译器,以及许多常见设置,例如哪些运行时库(静态/动态,调试/发布,很高兴单线程从VC中消失),一些C ++设置,例如异常处理,虚拟成员函数指针表示和其他。 You end up building hundreds of variants to support just the mainstream compilers, and you still end up with someone who needs it just a bit different. 您最终需要构建数百种变体以支持主流编译器,并且您最终仍然会遇到需要它的人。

Always include source, so the user can rebuild the static library on his box. 始终包含源,因此用户可以在其盒子上重建静态库。 Alternatively use a dynamic library - but that has its own gotchas. 或者使用动态库 - 但它有自己的陷阱。

namespace 命名空间
As mentioned, use a namespace to avoid collisions. 如上所述,使用命名空间来避免冲突。 Don't go overboard, if you have source included the user can always remaname it. 不要过分,如果你有源包括用户可以随时重新命名它。 But keep in mind that using namespace declarations should not occur in a header, so code needs to be adjusted slightly. 但请记住, using namespace声明不应出现在标头中,因此需要稍微调整代码。

if you are giving out your static lib you dont need to include foo.c as this will be part of the lib and yes if they have there own foo.c it will confuse the compiler and linker and might cause problems if you both have like functions or it will include the wrong header. 如果你给出你的静态库你不需要包含foo.c,因为这将是lib的一部分,是的,如果他们有自己的foo.c它会混淆编译器和链接器,如果你们两个都喜欢它可能会导致问题函数或它将包含错误的标题。

Your best way to solve this is to use a namespace for your code thus keeping it unique. 解决此问题的最佳方法是为代码使用命名空间,从而保持其唯一性。

namespace myfooname
{

void foo()
{
//stuff
}


}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM