5
I would like to know how to create a new library in C++
. if one can give an example, it can be a widespread example.
5
I would like to know how to create a new library in C++
. if one can give an example, it can be a widespread example.
7
Two types of libraries will be explained: static and shared. Below is an example with the creation of both in the command line Linux.
Static library: Used in the compilation
main.cpp
#include "libprint.h"
int main(){
//Função guardada numa biblioteca estática
print_ola("Rafael");
}
libprint. h (Static library header) - it is good to create a header file for your library.
#ifndef LIBPRINT_H
#define LIBPRINT_H
void print_ola(const char *);
#endif
printola.cpp (Code from your static library)
#include "libprint.h"
#include <iostream>
void print_ola(const char *nome){
std::cout << "Olá, " << nome << "!" << std::endl;
}
Steps to create static library:
Generate printola.cpp object code: g++ -c printola.cpp
. Now package the object code in library form:
ar crv libprintola.a printola.o
Ready! You already have a static library called libprintola. a! (more object codes can be inserted into a single library ex: ar crv libprintola.a printola.o printalgo.o etc
).
Compilation and execution:
g++ main.cpp -o programa libprintola.a
./programa
Exit: Hello, Rafael!
Bibliotecas Compartilhadas: - (with the same previous files!)
g++ -c -fpic printola.cpp
creates the object code object that can be used as shared.
g++ -shared -o libprintola.so printola.o
creates your shared call library libprintola.!
Testing: g++ -L. -Wl,-rpath=. -Wall -o programa main.cpp -lprintola
Which should display the same result. (but watch out! each one works differently!)
Browser other questions tagged c++
You are not signed in. Login or sign up in order to post.