Step 1: Create your library
Step 2: Compile your library
g++ -Wall -shared -fPIC -o ~/libs/libmylibrary.so libmylibrary.cpp
The format goes like this: g++ [options] [library placement on filesystem] [file(s) that make up library]
I also had a ~/libs/ folder created so I could compile and install as user. If you want system-wide access, run the command as root and use "-o /usr/local/lib/libfilename.so".
Step 3: Test your library
If you are using a namespace in your library, be sure to use it or prefix any methods or constructors with it.
In your main() method, create an instance of the object, call its method(s), and delete it at the end. An example would be so:
ExampleObj * myobj = new ExampleObject();
Now the best part: Compiling your program and linking it to your library. Do so with this command:
g++ main.cpp -o ~/bin/libtest -L$HOME/libs -lmylibrary
A couple IMPORTANT things to note here:
- ~/bin/ is a directory I created. You may have to change location or create the directory yourself
- The -l command which links the library has omitted the "lib" prefix, so it is mylibrary instead of libmylibrary.
You may have to add the following around the body of your header file.
This goes after the include statements, but before the namespace/class declarations
And after end of namespace/class declarations, but before final #endif tag:
This should fix some c++ specific issues you may encounter.