https://github.com/epomatti/cpp-static-dynamic-lib
Static and Linked/Shared libraries with C++
https://github.com/epomatti/cpp-static-dynamic-lib
c cpp gcc
Last synced: 2 months ago
JSON representation
Static and Linked/Shared libraries with C++
- Host: GitHub
- URL: https://github.com/epomatti/cpp-static-dynamic-lib
- Owner: epomatti
- License: mit
- Created: 2023-05-05T16:26:45.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2023-05-05T20:08:50.000Z (about 3 years ago)
- Last Synced: 2025-06-08T01:06:01.757Z (about 1 year ago)
- Topics: c, cpp, gcc
- Language: C++
- Homepage:
- Size: 5.86 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# 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.
```sh
g++ -c shared.cpp
ar -cvq shared.a shared.o
g++ main.cpp shared.a -o main.out
```
To run the applicatoin:
```sh
./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.
```sh
g++ -c -fPIC shared.cpp
g++ -shared shared.o -o libshared.so
g++ -L. main.cpp -lshared -o main.out
```
To run the application:
```sh
./main.out
```
This will print `Hello V1!` from the shared library.
Now to demonstrate the effect of a dynamic update compile the V2:
```sh
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:
```sh
# Hello V2!
./main.out
```
## References
- [YoLinux](http://www.yolinux.com/TUTORIALS/LibraryArchives-StaticAndDynamic.html)
- [Jamie King](https://youtu.be/Jzh4ZULXsvo)
- [C Programming](https://www.cprogramming.com/tutorial/shared-libraries-linux-gcc.html)
- [Stackoverflow nested dependencies](https://stackoverflow.com/questions/61775728/gcc-shared-library-undefined-reference-on-nested-dependencies)