LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How do I custom write a library and use it in the header declaration ? (https://www.linuxquestions.org/questions/programming-9/how-do-i-custom-write-a-library-and-use-it-in-the-header-declaration-186049/)

Linh 05-26-2004 09:36 AM

How do I custom write a library and use it in the header declaration ?
 
Let say I want to write predefine a pythagorean theorem in a C library where a = b (square) + c (square). Then I would use the header #include <pythagorean.h>, and then call the function c = pythagorean(a, b)

How would I do this ? Below is a code example.

==========================
Code:

#include <stdio.h>
#include <string.h>
#include <pythagorean.h>

main()
{
  int c;
  int a =10;
  int b = 20;
  c = pythagorean(a, b);
}


nimra 05-26-2004 09:45 AM

I am not sure if I understood you right:

Code example:
-----------------------------------
#ifndef _pythagorean_H_
#define _pythagorean_H_

// prototype the function

int pythagorean(int a, int b);

#endif
-----------------------------------
save this file under pythagorean.h

then open another file with the name pythagorean.cpp
---------------------------------------
#include "pythagorean.h"

int pythagorean(int a, int b){
return a*a + b*b;
};
---------------------------------------

include the file in the main program with

#include "pythagorean.h"
instead of
#include <pythagorean.h>

and compile with g++ pythagorean.cpp main.cpp

I hope it was the answer of your question!

Linh 05-26-2004 10:07 AM

reply
 
Thank you nimra for your help.

When the file pythagorean.cpp compile, will it be in machine language format ?
Will the file pythagorean.h be in machine language format ?

itsme86 05-26-2004 10:23 AM

Header files don't get compiled. The #include directive simple replaces the #include line with the contents of the file you specify. So basically your program would look like it has the contents of all the #include'd files and your source code to the compiler.

With nimra's example, you'd only end up with one binary file. So if you did [b]g++ pythagorean.cpp main.cpp -o myprog[b] then you'd have your original files still intact, looking the same way they did when you last saved them and you'd also have a myprog executable program.

DarkBeethoven 05-26-2004 03:38 PM

I believe what you want is to create a real library so that you can do

#include <pythagorean>

instead of

#include "pythagorean"

What you want to do is compile your code, but not link it altogether (g++ blah.cpp -c) I believe......and create a real library out of it.....

To create libraries, do "man ar" (ar means archive) for details, to create something like: libpythagorean.a

Once you have created the library, you should be able to do something like:

g++ main.cpp -l pythagorean (this will include the newly created library) to be linked to your main program.


All times are GMT -5. The time now is 08:39 AM.