Posted Joe Chucpp6 minutes read (About 878 words)0 visits
Load Functions from Shared Libraries
Loading the partial shared library can be beneficial for two reasons:
Original shared library can be very large sometimes(100+ Mb).
Original shared library is not accessible until runtime(OEM specific libraries such OpenCL).
1. Demo
We will start with a customized demo example to illustrate how to load partial shared library at runtime.
We will generate a simplified math library containing basic algorithmic operations(add, subtract, multiply and divide). test.cpp will load these functions at runtime.
1.1 Code
1 2 3 4
. ├── mathOP.cpp ├── mathOP.h └── test.cpp
1 2 3 4 5 6 7 8 9 10 11 12
// mathOP.h #ifndef MATH_OPERATIONS_H #define MATH_OPERATIONS_H extern"C" { doubleadd(double a, double b); doublesubtract(double a, double b); doublemultiply(double a, double b); doubledivide(double a, double b); } #endif// MATH_OPERATIONS_H
If you’re using a Mac with Apple silicon, you can follow my tutorial step by step to reproduce the results. For other operating systems, like Linux or Windows, the process is generally similar, but you may need to do some research and make a few adjustments.
To install jpeg library:
1
brew install libjpeg
You should be able to see the installed location in your terminal message. For example, mine is:
1 2 3 4 5 6
If you need to have jpeg first in your PATH, run: echo 'export PATH="/opt/homebrew/opt/jpeg/bin:$PATH"' >> ~/.zshrc For compilers to find jpeg you may need to set: export LDFLAGS="-L/opt/homebrew/opt/jpeg/lib" export CPPFLAGS="-I/opt/homebrew/opt/jpeg/include"
Let’s write a C++ program to read a jpeg format image. In our program, we only load needed functions to read an image.