Skip to content

epomatti/cpp-static-dynamic-lib

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CPP Lib compile

Static and dynamic C/C++ compilation.

Build commands

Static

This will embed the dependencies in the executable, and changes to the dependencies will require the client application to be built again.

g++ -c shared.cpp
ar -cvq shared.a shared.o
g++ main.cpp shared.a -o main.out

To run the applicatoin:

./main.out

Dynamic

A dynamic dependency (shared / linked library) allows the dependency to be updated without changes to the client code, giving that the interface between the programs remain compatible.

g++ -c -fPIC shared.cpp
g++ -shared shared.o -o libshared.so
g++ -L. main.cpp -lshared -o main.out

To run the application:

./main.out

This will print Hello V1! from the shared library.

Now to demonstrate the effect of a dynamic update compile the V2:

g++ -c -fPIC sharedv2.cpp -o shared.o
g++ -shared shared.o -o libshared.so

This will update the library and running the main program will output Hello V2! as a result:

# Hello V2!
./main.out

References

About

Static and Linked/Shared libraries with C++

Topics

Resources

License

Stars

0 stars

Watchers

1 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors