переведеи на русский The convention in C/C++ when implementing a module/library is to…
- User
переведеи на русский The convention in C/C++ when implementing a module/library is to divide it into headers (.h) and source files (.c, .cc, .cpp). You typically include only the bare minimum interface in the header file. That means things like class/struct definitions, function prototypes, and global constants. Then the implementations of said functions are placed in a corresponding source file.
When making the module available to the public, you would compile the source files into binaries (often .so or .dll depending on operating system) and deliver the .h files along with the .so files.
As a user of one of these libraries you need to do two things to properly use it:
include the header file in your source (e.g. #include <myCoolOtherFile.h>)
when compiling your code, tell the linker where to find the implementation binaries that correspond to the headers. (e.g. gcc myCoolFile.c -l myCoolOtherFile).
Side note: when you install a library it typically places the .h and .so files in directories where your compiler will look for them by default (on Linux, somewhere like /usr/include and /usr/lib/) so providing just the name of the library is usually sufficient for both of the above steps.
One of the benefits to packaging libraries in this way is to save compilation time. Imagine having to include the source code (.c files) for every library you used (stdio.h, stdlib.h, etc) in every project. You would need to compile each of those every time and each is potentially a massive amount of code. By using libraries as pre-compiled binaries and headers you get to save a lot of compilation time.
- Assistant
Принято в C/C++ при создании модуля/библиотеки делить его на заголовочные файлы (.h) и исходные файлы (.c, .cc, .cpp). Обычно в заголовочном файле включают только минимально необходимый интерфейс. Это означает такие вещи, как определения классов/структур, прототипы функций и глобальные константы. Затем реализации этих функций размещаются в соответствующем исходном файле.
Если вы хотите сделать модуль доступным для публики, вы компилируете исходные файлы в бинарные файлы (часто .so или .dll в зависимости от операционной системы) и поставляете .h файлы вместе с .so файлами.
Как пользователь одной из таких библиотек вам необходимо сделать две вещи, чтобы правильно её использовать:
-
Включить заголовочный файл в ваш исходный код (например, #include <myCoolOtherFile.h>).
-