954,480 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

I want to make my own library, how can I go about doing that

Hi, I'm trying to make my own library so that I know the names of the functions and can use them more simply. Also, I normally don't get the File I/O right so if I write the Library once, then I can use it over and over again (without having much trouble).


I can write functions and classes and know the syntax of the language, I just want to know where I have to place the files to get them to work with a #include <...> statement (the <> really matters because then I don't have to put the file in all of my projects).

I'm using Ubuntu 10.10 and g++

jackmaverick1
Posting Whiz in Training
212 posts since May 2010
Reputation Points: 6
Solved Threads: 7
 

I would consider creating a DLL for commonly used functions.

pseudorandom21
Practically a Posting Shark
890 posts since Jan 2011
Reputation Points: 216
Solved Threads: 111
 

ok, how can I do that. Also, are DLLs okay on Ubuntu? if not, what else can I use.

What format are the librarys like iostream or cmath in?

jackmaverick1
Posting Whiz in Training
212 posts since May 2010
Reputation Points: 6
Solved Threads: 7
 

In Linux/Unix systems, a dll is called a shared library, and has a .so extension. Read the g++ and linker documentation (man pages) how to do this. It is quite simple to create a shared library with Linux. In any case, the compiler directive when creating the library is -shared. If you are creating a shared library from object (.o) files, then make sure you compile the object files with the -fPIC option. That generates what we call Position Independent Code (PIC), which is necessary for a shared library, since the functions may need to be relocated (different memory position) when they are loaded into the executable.

rubberman
Posting Virtuoso
1,564 posts since Mar 2010
Reputation Points: 277
Solved Threads: 179
 

>>Also, are DLLs okay on Ubuntu?
I think rubberman answered that.

>>What format are the librarys like iostream or cmath in?
What format? They are just C++ header files, without the .h at the end. You can check it out for yourself, they are usually located in "/usr/include/c++/4.4/" (or /usr/local instead of just /usr, and that version 4.4 depends on the version of your GCC compiler).

>>the <> really matters because then I don't have to put the file in all of my projects.
The difference between "" and <> with the include of headers is just that with "" the compiler will look for the file first in the current directory of the file that is being compiled, and then, it will look in the include directories (the stuff you specify with -I in the command-line compilation) and finally, will look into the standard system-wide include directories (like /usr/include/..). With <>, the compiler will not look in the current directory of the file being compiled, but it will look in all the same directories after (as with ""). Basically, you typically use "" when it is a custom header (part of the software/library you are working on), and <> otherwise. But really, there is little difference between the two (mostly a convention, but note that some compilers could behave differently).

To add your library's include directory (where you have all the header files) to the system's include paths. On Linux, these paths are set with the environment variable CPLUS_INCLUDE_PATH, see these instructions .

You will have to do something similar when you place your shared or static libraries in the /usr/lib folder.

mike_2000_17
Posting Virtuoso
Moderator
2,134 posts since Jul 2010
Reputation Points: 1,634
Solved Threads: 457
 

Thanks, that helped a lot :)

jackmaverick1
Posting Whiz in Training
212 posts since May 2010
Reputation Points: 6
Solved Threads: 7
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: