Welcome to the most active Linux Forum on the web.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 05-26-2004, 09:36 AM   #1
Registered: Apr 2003
Posts: 178

Rep: Reputation: 30
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.

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

   int c;
   int a =10;
   int b = 20;
   c = pythagorean(a, b);
Old 05-26-2004, 09:45 AM   #2
LQ Newbie
Registered: May 2004
Posts: 28

Rep: Reputation: 15
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);

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!
Old 05-26-2004, 10:07 AM   #3
Registered: Apr 2003
Posts: 178

Original Poster
Rep: Reputation: 30
Smile 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 ?
Old 05-26-2004, 10:23 AM   #4
Senior Member
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
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.
Old 05-26-2004, 03:38 PM   #5
LQ Newbie
Registered: Jan 2002
Distribution: customized
Posts: 22

Rep: Reputation: 15
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.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
netbsd c library and header files >minimalist< Programming 0 10-31-2005 09:08 PM
Include Custom Header Kenji Miyamoto Programming 3 01-13-2005 08:46 PM
How to write Makefile in 2.6 for including header files in another dir ? ashbalu Programming 0 10-19-2004 03:45 PM
Difference between including a header file and 'class' declaration in C++ scoTtkIm Programming 1 09-09-2004 04:23 AM
ld cannot find library/header which _is_ there. jmorse Linux - Software 3 06-27-2004 04:18 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:54 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration