Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will C++20 modules change the anatomy of static and shared libraries?

Traditionally, C++ libraries are made of a header file + the implementation compiled in a binary file (.a, .so, .dylib, .dll, ...). The header file is #included in the source code and the binary part is linked to the final executable.

Will modules from C++20 change such layout? If so, will operating systems have to upgrade the way they distribute their core libraries, e.g. the Standard Library in Linux or other core dlls in Windows?

like image 298
Ignorant Avatar asked Sep 28 '20 15:09

Ignorant


People also ask

What is the difference between static and shared libraries?

Static libraries take longer to execute, because loading into the memory happens every time while executing. While Shared libraries are faster because shared library code is already in the memory.

What is the difference between shared library and static library in IIB?

If a shared library is deployed in a BAR file, it can still be used by applications or shared libraries in other deployed BAR files. Static libraries are packaged and deployed in the same BAR file as the applications that reference them.

Why Shared libraries are preferred over static libraries?

In shared libraries, no need to recompile the executable. Takes longer to execute, because loading into the memory happens every time while executing. It is faster because shared library code is already in the memory. Never has compatibility issue, since all code is in one executable module.

Why might you choose to link your program statically rather than use a shared library version?

Library references are more efficient because the library procedures are statically linked into the program. Static linking increases the file size of your program, and it may increase the code size in memory if other applications, or other copies of your application, are running on the system.

What is the difference between shared libraries and C++20 modules?

Shared libraries are the first "major" hurdle so far. C++20 modules have no special relationship with shared libraries. They are primarily a replacement of header files. This means that you would develop a shared library with C++20 modules in a similar fashion as you would with header files before C++20, at least with my current understanding.

What are modules in c++20?

Modules are the new C++20 method for importing functions and classes from external libraries and separate translational units. Modules provide an alternative to using header files. Some of the benefits of modules are: No need to write separate files for the interface ( .h) and the implementation ( .cpp)

What is the difference between a static and shared library?

This executable and the process of compiling it are both known as a static build of the program. Historically, libraries could only be static. They are usually faster than the shared libraries because a set of commonly used object files is put into a single library executable file.

What are static libraries in C++?

Static Libraries : A Static library or statically-linked library is a set of routines, external functions and variables which are resolved in a caller at compile-time and copied into a target application by a compiler, linker, or binder, producing an object file and a stand-alone executable.


1 Answers

Absolutely. Modules are a very different type of representing library code to users than conventional header/libraries. The main advantage of a module is to have it parsed to the level of the abstract syntax tree (AST) by the compiler. This only happens once per module -- in contrary to every time you include a particular header file. Thus, one possibility for speedup is to convert very frequent header files into modules and save a lot of compiler time in not re-compiling to AST many times, but just once. AST also works perfectly fine for templates ... it is a generic and complete description of the C++ language.

But this is currently also the main "drawback": ASTs are absolutely compiler dependent. There is no stability between vendors, systems, or even compiler versions. So distributing ASTs makes no sense, at least in the current toolchain environment.

Thus, in my understanding, modules are not easily a replacement for common header/libraries. They are a ideal replacement for lightweight (best header-only) and potentially highly templated code to be included many times in typical programs. Many libraries, foremost the standard libraries, are like this. But there are also many libraries of different design (small API+heavy binary backend). For those, I guess, we will have to continue to use include/libraries as before. But, C++ is backward compatible, so mixing modules with includes is no problem at all.

Also, modules can just wrap include files (e.g. with an API). This is probably not a very powerful usage of modules, but may lead to a more uniform programming pattern. In this case, you would still need to link your "so" or "dylib" or whatever library to the resulting code.

Thus, I believe the future at some point will distribute both, some C++ modules, but also conventional headers+libraries. Modules by themselves can be split into many files (only the "exported" symbols are visible to users). Modules will likely follow very different design guidelines than headers. It very likely will not be any advantage to have "one class per module", but rather "a whole bunch of logically connected code per module".

like image 106
Ralf Ulrich Avatar answered Oct 03 '22 13:10

Ralf Ulrich