Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does exporting a symbol mean?

Tags:

c++

c

export

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?

like image 964
Raulp Avatar asked Jun 05 '12 19:06

Raulp


People also ask

What is an exported symbol?

The Export Symbol (EXPORT) command identifies a symbol name available to be exported from a service program. If the exported symbols contain lowercase letters, the symbol name should be enclosed within apostrophes as in Figure 1. If apostrophes are not used, the symbol name is converted to all uppercase letters.

What happens when you export a document?

In a personal computer application, to export is to convert a file into another format than the one it is currently in. Once the file is exported to the desired format (specified in its file name suffix), it can be opened and worked on by an application that recognizes and uses this format.

What is export in C?

Exporting is relative to that process. In C/C++ if you want to declare a function to use it without defining it in a source file, you should use the keyword "extern". In the file where that function is defined you have nothing special to make, by defaults things at global scope are automatically exported.


1 Answers

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).


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.

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.

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.

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.

like image 112
littleadv Avatar answered Oct 09 '22 10:10

littleadv