Trying to make my own library

Jan 30, 2011 at 1:20pm
Hi guys,

I'm trying to make my proper library. So far I only have one class for it: bigint.
But I'm also going to add a function for primes etc
I searched everywhere, but it's all for Linux, can someone please tell me how to do this on Windows?
I have: bigint.h , bigint.cpp and primes.h (just a function)

How do I make a library of them, so I can use them afterwards?

cheers,

xander
Jan 30, 2011 at 1:38pm
What is "function for primes" ?
Jan 30, 2011 at 1:40pm
Well, it's a function where you pass argument "int max" and it returns an std::list with all the primes up until that argument.
Jan 30, 2011 at 1:43pm
That's compiler specific. In any case, you have to supply the header files, and library files. For GCC this would be lib*yourlib*.a files, I believe Visual C++ uses *.lib files.

That's why, unless you want to hide your source code, it would make sense to distribute the source code, so the users can compile the libraries themselves.

http://en.wikipedia.org/wiki/Library_(computing)

Basically, in C/C++ a library is just a bunch of object code, distributed together with the headers.

Here's how to create one with gcc.

http://www.adp-gmbh.ch/cpp/gcc/create_lib.html
Last edited on Jan 30, 2011 at 2:26pm
Jan 30, 2011 at 2:10pm
It doesn't state where exactly I have to put my .o ?
Jan 30, 2011 at 2:19pm
Ok, so I have my libbigint.dll.a, now where do I put it??
Jan 30, 2011 at 2:33pm
Sorry, but what do you mean? Where you put it doesn't matter anymore, you have your library, you're done. If you want to use the library yourself, you'll have to put it into one of the directories that are within the search path of your linker.

(Besides, what I said wasn't 100% correct. There are compiler/linker independent libraries, but making them is AFAIK a bit more complicated.)
Jan 30, 2011 at 3:50pm
Ok so I set the linker to the right path.
How do I use it now?
It keeps saying it doesn't recognise the name of my class in the library.
Jan 30, 2011 at 4:10pm
You need to include the headers.
Jan 30, 2011 at 7:23pm
I know, and I'm doing that now.
But I'm working with the .h and .cpp file instead of the .dll.
Also, I have to copy them to the folder of my project...
Jan 30, 2011 at 7:28pm
libbigint.dll.a


'.dll' is the standard Win32 extension for a shared dynamic library.
'.a' is the standard *nix extension for a shared static library.

You should work out whether you're making dynamic or static, and whether you're compiling for Win32 (or Win64, I suppose) or *nix, and stick with the appropriate extension. Many tools will assume they're looking for a file with the right extension.
Jan 31, 2011 at 6:38am
Ok, I have a .dll now. (I compiled .h and .cpp to .dll)
Where do I put it now?
Jan 31, 2011 at 7:19am
Jan 31, 2011 at 3:41pm
Ok, so I put the .dll in the right dir, now how do I call it?
#include "bigint.dll" doesn't work
Jan 31, 2011 at 6:11pm
xander... are you really sure you UNDERSTOOD what a library, and what a header is? It appears you didn't- please read this:

A compiler ( http://en.wikipedia.org/wiki/Compiler ) translates source code into object code. Object code is very close to executable code.
In C and C++, before the compilation a preprocessor ( http://en.wikipedia.org/wiki/Preprocessor )
modifies the source code. It transforms macros into source code, and replaces #include directives with the contents of the included header files.
After that, a linker ( http://en.wikipedia.org/wiki/Linker_%28computing%29 ) is used to combine (link, but explaining a word with itself is bad style, or so I was told) the object files into a single executable.

That's the general gist of it.

A library in C and C++ contains object code. You do not include object code, you link it. However, so the symbols of the linked library are known within the source code, you need a header that contains the symbol definitions.

Example: You have 3 files:
main.cpp contains the main function, and the general programming logic.
1
2
3
4
5
6
7
8
//main.cpp
#include "faculty.h"
int main(int argc, char** argv)
{
      int a = 5;
      int b = faculty(a);
      return 0;
}

faculty.cpp contains a function to calculate the faculty of a number.
1
2
3
4
5
6
7
8
9
10
11
//faculty.cpp
include "faculty.h"
int faculty(int N)
{
int n=1;
for(int i=2; i<=N;i++)
{
    n*=i;
}
return n;
}

faculty.h contains the signature of that function
int faculty(int);

Now, first the preprocessor:
#include "faculty.h"
Is replaced by the contents of faculty.h in main.cpp and faculty.cpp
main.cpp and faculty.cpp are compiled to the object files main.o and faculty.o respectively.
The linker is invoked, and links the object files main.o and faculty.o to the executable main.exe

A library contains compiled object code. To use it, you have to link the object code, and include the appropitiate header files.
Last edited on Jan 31, 2011 at 6:22pm
Feb 1, 2011 at 6:40pm
But where do I have to place the .h and .cpp files???
I always have to place them in the dir of my project AND add them to the project...
Feb 1, 2011 at 7:12pm
That has nothing to do with C++ programming anymore. That is your compiler settings. Your IDE probably has a search path for header/library files (and they don't need to be part of the project. They just need to be somewhere your compiler looks).
Topic archived. No new replies allowed.