简体   繁体   English

导出符号意味着什么?

[英]What does exporting a symbol mean?

I have been looking for this term "exporting a symbol". 我一直在寻找这个术语“输出符号”。 What does exporting a symbol mean in C/C++ or with respect to the libraries (shared/static)? 导出符号意味着在C / C ++中或相对于库(共享/静态)? From where do we export the symbols and why? 我们从哪里导出符号以及为什么? What is the relation of exporting a symbol with the name mangling by the compiler? 导出符号与编译器名称修改的关系是什么?

Exporting a symbol means "advertising" its existence in your object file/library and where it is, so that it could be imported (=linked to) by other modules. 导出符号意味着在其目标文件/库中“广告”它的存在,以及它可以被其他模块导入(=链接到)。

Link can be done statically or dynamically, but either way the linker has to know what the symbol is, and where it is, and the exported symbol and the imported symbol must match for it to happen. 链接可以静态或动态完成,但无论哪种方式,链接器都必须知道符号是什么,以及它在哪里,导出的符号和导入的符号必须匹配才能发生。 Name mangling is related to that (C++ name mangling includes symbol's type definition in the symbol name, and the mangling of the exported and imported symbol must match for the linker to link the import-export correctly). 名称修改与此相关(C ++名称修改包含符号名称中的符号类型定义,导出和导入符号的修改必须匹配链接器以正确链接导入 - 导出)。


Example: 例:

Suppose you have a library "STANDARDC" (random name) and your program SOMEPROG. 假设您有一个库“STANDARDC”(随机名称)和您的程序SOMEPROG。 Program SOMEPROG needs to print to console, so it will call printf . 程序SOMEPROG需要打印到控制台,因此它将调用printf But you don't actually implement printf in your program SOMEPROG, you just use it (=import it), while the implementation is elsewhere. 但是你实际上并没有在你的程序SOMEPROG中实现printf ,你只需要使用它(= import it),而实现在其他地方。

The library STANDARDC has a list of symbols it exports which includes all the functions that are implemented in that library and can be called from outside (=exported functions). 库STANDARDC有一个它导出的符号列表,其中包括在该库中实现的所有函数,可以从外部调用(=导出函数)。 printf is one of such functions, so it will appear in the exported list. printf是这样的函数之一,因此它将出现在导出的列表中。

The compiler goes through your SOMEPROG.C and sees that you reference printf , but there's no implementation for it. 编译器通过你的SOMEPROG.C看到你引用了printf ,但它没有实现。 The compiler adds the printf to the list of the imported symbols for the resulting SOMEPROG.obj, for the linker to link the actual implementation in. 编译器将printf添加到生成的SOMEPROG.obj的导入符号列表中,以便链接器链接实际的实现。

The linker takes your SOMEPROG.obj file and the STANDARDC .lib file, and sees what functions are used in the SOMEPROG.obj. 链接器获取您的SOMEPROG.obj文件和STANDARDC .lib文件,并查看SOMEPROG.obj中使用的函数。 The linker finds that printf is not implemented, it is imported, so the linker looks through all the .lib files it has and finds matching printf in the exported list of STANDARDC. 链接器发现printf未实现,它被导入,因此链接器查看它拥有的所有.lib文件,并在导出的STANDARDC列表中找到匹配的printf It takes the implementation of printf from STANDARDC and links it into your program everywhere you reference the imported symbol printf . 它需要从STANDARDC实现printf ,并在引用导入的符号printf任何地方将它链接到您的程序中。

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

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