Wednesday, July 11, 2012

Creating shared library for Linux in Netbeans

In this post I'll describe how to create a shared library for Linux in Netbeans.



First we need to create a Dynamic Library project. To do it go to File -> New Project -> C/C++ -> C/C++ DynamicLibrary:

Click Next and enter the name of your project:
I've selecte name "TestLibrary".
Now we should add sources of the library. To keep things simple we will have only one function in our library int increment(int i) that will return number i increased by one. Here is the code of header file that defines interface of our function (file test.h):


The implementation of the function is in the another file (test.cpp):

We need extern "C" because g++ mangles names of the functions to create a unique name for a function. If we use extern "C" name will not be mangled and this function could be used from other program.
Now we can build the library by just clicking Build on our project.

To simplify using of this library we will "install" it so that any program could use it. To do it copy the header file (test.h) to /usr/local/include and library file to /usr/local/lib. The built library is in:


Now we can write simple C++ program to check if our library works. Here is a simple example:


To build it we should use the following command:

-l flag specifies that compiler should look for the file libLibraryTest.so


Now we can run the result program:

That's all.
Good luck.

6 comments:

  1. Thank you for the information.
    I find it useful. Now I think how I can use it for virtual data room picked out with the help of data room providers comparison.

    ReplyDelete
  2. E shram Card is a labor registration portal created by the Labor Department, Government of india.

    ReplyDelete
  3. Computer Full Form is very usefull in students for study in education

    ReplyDelete
  4. All type services start in Bihar Govt go to see this services and help for filling form RTPS Bihar

    ReplyDelete
  5. i like your post and all your headings are good....hindi skill

    ReplyDelete