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)? From where do we export the symbols and why? What is the relation of exporting a symbol with the name mangling by the compiler?
我一直在寻找“输出符号”这个词。在C/ c++或关于库(共享/静态)中导出符号意味着什么?我们从哪里导出符号,为什么?输出一个由编译器管理的名称的符号的关系是什么?
1 个解决方案
#1
16
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. Program SOMEPROG needs to print to console, so it will call printf
. But you don't actually implement printf
in your program SOMEPROG, you just use it (=import it), while the implementation is elsewhere.
假设您有一个库“STANDARDC”(随机名称)和您的程序SOMEPROG。程序SOMEPROG需要打印到控制台,因此它将调用printf。但是,实际上并没有在程序SOMEPROG中实现printf,而是使用它(=import它),而实现在其他地方。
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). printf
is one of such functions, so it will appear in the exported list.
库STANDARDC有它导出的符号列表,其中包含在库中实现的所有函数,并且可以从外部调用(=export functions)。printf是其中一个函数,因此它将出现在导出的列表中。
The compiler goes through your SOMEPROG.C and sees that you reference printf
, but there's no implementation for it. 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.
编译器通过你的SOMEPROG。C看到你引用printf,但是没有实现。编译器将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. 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. It takes the implementation of printf
from STANDARDC and links it into your program everywhere you reference the imported symbol printf
.
链接器接收你的等深线。obj文件和标准的.lib文件,并查看在SOMEPROG.obj中使用了哪些函数。链接器发现printf没有实现,它是导入的,所以链接器会检查所有的.lib文件,并在输出的STANDARDC列表中找到匹配的printf。它从STANDARDC获得printf的实现,并将其链接到您的程序中,您可以在任何地方引用导入的符号printf。
#1
16
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. Program SOMEPROG needs to print to console, so it will call printf
. But you don't actually implement printf
in your program SOMEPROG, you just use it (=import it), while the implementation is elsewhere.
假设您有一个库“STANDARDC”(随机名称)和您的程序SOMEPROG。程序SOMEPROG需要打印到控制台,因此它将调用printf。但是,实际上并没有在程序SOMEPROG中实现printf,而是使用它(=import它),而实现在其他地方。
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). printf
is one of such functions, so it will appear in the exported list.
库STANDARDC有它导出的符号列表,其中包含在库中实现的所有函数,并且可以从外部调用(=export functions)。printf是其中一个函数,因此它将出现在导出的列表中。
The compiler goes through your SOMEPROG.C and sees that you reference printf
, but there's no implementation for it. 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.
编译器通过你的SOMEPROG。C看到你引用printf,但是没有实现。编译器将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. 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. It takes the implementation of printf
from STANDARDC and links it into your program everywhere you reference the imported symbol printf
.
链接器接收你的等深线。obj文件和标准的.lib文件,并查看在SOMEPROG.obj中使用了哪些函数。链接器发现printf没有实现,它是导入的,所以链接器会检查所有的.lib文件,并在输出的STANDARDC列表中找到匹配的printf。它从STANDARDC获得printf的实现,并将其链接到您的程序中,您可以在任何地方引用导入的符号printf。