Thread: How to make my own header/library?

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    119

    How to make my own header/library?

    I wrote a function that I would like to be able to learn how to make a header file and library file for, so I can learn how. Any help greatly appreciated...


    --Ash

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    Which compiler?

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    119
    Well I have Dev-c++ and Pacific C, preferably Dev-c++, but if you know how for either one i'd be grateful...

  4. #4
    Registered User
    Join Date
    Mar 2005
    Location
    Juneda
    Posts
    291
    dev-cpp (/w mingw) allows you to create your own dll's, then on compiling it will create de dll plus the *.a library (in other compilers is a *.lib file); then simply create an *.h file and dllimport in it the dll functions names. similarly you can create static libraries that dev-cpp will compile to *.a, but here I'm not sure if you have to work with the *.def file (also created by the compiler) to import the functions, or maybe you can do it easilly like in dll's.
    niara

  5. #5
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Quote Originally Posted by Niara
    dev-cpp (/w mingw) allows you to create your own dll's, then on compiling it will create de dll plus the *.a library (in other compilers is a *.lib file); then simply create an *.h file and dllimport in it the dll functions names. similarly you can create static libraries that dev-cpp will compile to *.a, but here I'm not sure if you have to work with the *.def file (also created by the compiler) to import the functions, or maybe you can do it easilly like in dll's.
    niara
    That's just way too much information for a simple question. For libraries, you can work with them the same way you would work with a .cpp source file. If you create an instance of a class or declare a global variable, you'd have to extern that variable in order to use it in another source. Most of the time, you wouldn't need to do this, anyway. Libraries are best used for classes, structures, functions and macros that you'll want to use in your source code but you don't want to clutter the code.

    An important thing to know is to define the whole code in an #ifndef so that you don't end up redeclaring the same classes incase you end up including libraries twice.

    Here's a quick example of a library and a CPP coordinating:

    slylib.h
    Code:
    /* This is our library */
    
    #IFNDEF SLYLIB_H
    #DEFINE SLYLIB_H
    
    class foo {
       public:
         foo();
         int foobar();
       private:  
         int bar;
         int baz;
    };
    
    foo::foo() { 
        bar = 10;
        baz = 5
    }
    
    int foo::foobar() {
        return bar + baz;
    }
       
    #ENDIF
    main.cpp
    Code:
    /* This is our source file */
    #include <iostream>
    #include "slylib.h"   /* Everything in my library can now be used */
    
    int main() {
       foo exmpl;
    
       std::cout << exmpl.foobar();
    
       return 0;
    }
    Oh and by the way... this is C++ but it's the same idea for C.
    Last edited by SlyMaelstrom; 12-29-2005 at 12:45 PM.
    Sent from my iPadŽ

  6. #6
    Registered User
    Join Date
    Dec 2005
    Posts
    119
    Thanks to both of you! I didn't understand all of the first reply (sorry, I'm new to c) but it was still helpful, and I'm going to use the 2nd reply right now.

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    #ENDIF
    Can you put preprocessor directives in uppercase?
    Code:
    baz = 5
    You need a semicolon . . . okay, so it was just a sample program. A C++ one in the C forum.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Yep, yep... you're right on both.

    I wrote this in work during lunch and made a few errors. Preprocessor derectives should be lowercase and I wrote it in C++ forgetting what forum I was in and after I posted it I wasn't up for changing it into C.
    Sent from my iPadŽ

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to make a Packet sniffer/filter?
    By shown in forum C++ Programming
    Replies: 2
    Last Post: 02-22-2009, 09:51 PM
  2. "Cannot make pipe"
    By crepincdotcom in forum C Programming
    Replies: 5
    Last Post: 08-16-2004, 12:43 PM
  3. HELP!wanting to make full screen game windowed
    By rented in forum Game Programming
    Replies: 3
    Last Post: 06-11-2004, 04:19 AM
  4. make all rule
    By duffy in forum C Programming
    Replies: 9
    Last Post: 09-11-2003, 01:05 PM
  5. Replies: 6
    Last Post: 04-20-2002, 06:35 PM