Thread: How to make a Library file and header file in GCC?

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    19

    Exclamation How to make a Library file and header file in GCC?

    Hello,
    I know how to make a header file in C when using turbo c/c++ (had done it long time ago when i was using Windows xp) now i am using windows 7 with Gcc (DJGPP ) . But now i dont know how to make a lib file and header file.
    I am guessing is this what i should do? If i am wrong please do rectify me.

    (the sqaured.c file of the function that i am taking as an example to make a header)
    Code:
    float squared(float s_input)
    (
    float s_output;
    s_output=s_input*s_input;
    return(s_output);
    }
    i compiled it with this command and made a .o file(object file )
    gcc -c squared.c
    i go the object file.
    now how to make a lib file and insert this object in that library file and make a header file with the prototypes of the function?
    Idk if this is the right method. cuz i read somewhere that to make a lib file we need the obj file of the c file

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    You need to learn how to use something called ar: ar - GNU Binary Utilities I don't really use ar directly, the best thing is to just set up a static library project in your IDE where it will do a lot of work for you. Never the less, you can learn by reading that site.

  3. #3
    Registered User
    Join Date
    Dec 2012
    Posts
    19
    i dun know any good ide for windows7.
    Can u sugest me some?
    Which on do you use??

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    DJGPP is an IDE.

  5. #5
    Registered User
    Join Date
    Dec 2012
    Posts
    19
    But its for Windows xp or lower. not for 7. It doesnt work with 64bit windows 7.
    The RHIDE doesnt work in windows 7.

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Quote Originally Posted by mhrsolanki2020 View Post
    But its for Windows xp or lower. not for 7. It doesnt work with 64bit windows 7.
    The RHIDE doesnt work in windows 7.
    Then one wonders why you downloaded it. If you really want something else to use try visual studio or Code Blocks.

    At any rate, if you want to continue to use DJGPP, this page may help you understand more about the process and how ar fits into it. Since you know turbo C, you will be pleasantly surprised that a lot of the steps are the same; it is only the ar program that is new to you. DJGPP FAQ -- How do I create a library of object files? Any IDE that relies on GNU utilities to work will have the same steps.

  7. #7
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    As whiteflags mentioned, you can use your IDE. However for a quickstart on doing this with gcc, try the following with your example:

    1. Make a header file for your library called squared.h. Example:
    Code:
    #ifndef SQUARED_H
    #define SQUARED_H
    float squared(float s_input);
    #endif
    2. Make a simple tester (user program) for your library. I will call it test_squared.c. The tester must include squared.h. Example:

    Code:
    #include <stdio.h>
    #include "squared.h"
    
    int main()
    {
        printf("The result of squared(4.0f) is %.8f\n", squared(4.0f));
        return 0;
    }
    3. Make an implementation of your library. I will call it squared.c. The implementation must include squared.h. Example:
    Code:
    #include "squared.h"
    #include <math.h>
    
    float squared(float s_input)
    {
        return powf(s_input, 2.0f);
    }
    4. Compile the implementation into an .o file. Notice I am linking with libm because the squared.c implementation in turn uses the libm library. I will call it squared.o. Example command:

    gcc -Wall -std=gnu99 -c -o squared.o squared.c -lm

    5. Create an archive file to hold the object file(s). The GNU Naming convention is that the archive file must begin with "lib" and end with ".a". You can put as many object files as you want into the archive. I will call your archive libsquared.a. Example command:

    ar r libsquared.a squared.o

    6. Compile the tester and specify that you want to link to your libsquared library. The switch "-L." is needed if you have the archive file in the current directory. Without that switch, gcc will only look for libraries in the standard places. Example command:

    gcc -L. -Wall -std=gnu99 -o test_squared test_squared.c -lsquared

  8. #8
    Registered User
    Join Date
    Dec 2012
    Posts
    19
    Quote Originally Posted by c99tutorial View Post
    As whiteflags mentioned, you can use your IDE. However for a quickstart on doing this with gcc, try the following with your example:


    1. Make a header file for your library called squared.h. Example:
    Code:
    #ifndef SQUARED_H
    #define SQUARED_H
    float squared(float s_input);
    #endif

    2. Make a simple tester (user program) for your library. I will call it test_squared.c. The tester must include squared.h. Example:


    Code:
    #include <stdio.h>
    #include "squared.h"
    
    
    int main()
    {
     printf("The result of squared(4.0f) is %.8f\n", squared(4.0f));
     return 0;
    }

    3. Make an implementation of your library. I will call it squared.c. The implementation must include squared.h. Example:
    Code:
    #include "squared.h"
    #include <math.h>
    
    
    float squared(float s_input)
    {
     return powf(s_input, 2.0f);
    }

    4. Compile the implementation into an .o file. Notice I am linking with libm because the squared.c implementation in turn uses the libm library. I will call it squared.o. Example command:


    gcc -Wall -std=gnu99 -c -o squared.o squared.c -lm


    5. Create an archive file to hold the object file(s). The GNU Naming convention is that the archive file must begin with "lib" and end with ".a". You can put as many object files as you want into the archive. I will call your archive libsquared.a. Example command:


    ar r libsquared.a squared.o


    6. Compile the tester and specify that you want to link to your libsquared library. The switch "-L." is needed if you have the archive file in the current directory. Without that switch, gcc will only look for libraries in the standard places. Example command:


    gcc -L. -Wall -std=gnu99 -o test_squared test_squared.c -lsquared
    Thankyou , for your help..
    Code:
    #include <stdio.h>
    #include "squared.h"
    int main()
    { 
    printf("The result of squared(4.0f) is %.8f\n", squared(4.0f)); 
    return 0;
    }
    Can you explain me %.8f ?
    and
    Code:
    #include "squared.h"
    #include <math.h>
    float squared(float s_input)
    { 
    return powf(s_input, 2.0f);
    }
    powf?? where did that come from? i know there is a function named pow to which we can pass arguements like
    Code:
    pow(2,3);
    which means 2 to the power of 3 (2 cube).
    Last edited by mhrsolanki2020; 12-22-2012 at 10:48 PM.

  9. #9
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    >> Can you explain me %.8f ?
    Never used the printf function before, have you? It prints the double variable that you supply to 8 places after the decimal point.

    >> powf?? where did that come from?
    Same place pow comes from: there is a family of functions for pow that take different arguments and are named accordingly. powf is the variation that takes float arguments.

  10. #10
    Registered User
    Join Date
    Dec 2012
    Posts
    19
    Quote Originally Posted by whiteflags View Post
    >> Can you explain me %.8f ?
    Never used the printf function before, have you? It prints the double variable that you supply to 8 places after the decimal point.

    >> powf?? where did that come from?
    Same place pow comes from: there is a family of functions for pow that take different arguments and are named accordingly. powf is the variation that takes float arguments.
    Ohk . I have used, but never with the %.8f format specifiers..
    And i got error 1 exit status.. i changed powf to pow then it worked fine.

    if i change the return to
    Code:
    return(s_input*s_input);
    will there anything wrong? cuz it will save me from adding math.h, if i had to add math.h then why would i make a squaring function anyways??

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    If you are only going to compute a square, then yeah, using pow is quite unnecessary.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by whiteflags View Post
    DJGPP is an IDE.
    No it's not. DJGPP is a port of a number of GNU development tools, not including an IDE.

    A list of IDEs is here. I'm not sure how up-to-date it is though.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  13. #13
    Registered User
    Join Date
    Dec 2012
    Posts
    19
    Quote Originally Posted by laserlight View Post
    If you are only going to compute a square, then yeah, using pow is quite unnecessary.
    may be a for loop?
    i can ask the user if he want to square or cube or to the power of 4 ,5 6 whatever..
    and save it in x.
    and pass it to squared function,
    and then use a for loop, and loop it 'x' times, where in each loop it multiplies the previous result by s_input.

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by mhrsolanki2020
    may be a for loop?
    i can ask the user if he want to square or cube or to the power of 4 ,5 6 whatever..
    Then you don't only want to compute a square, so pow becomes appropriate.

    Quote Originally Posted by mhrsolanki2020
    and save it in x.
    and pass it to squared function,
    and then use a for loop, and loop it 'x' times, where in each loop it multiplies the previous result by s_input.
    That would be inefficient and you would have to deal with non-integral "power of ... whatever".
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    Registered User
    Join Date
    Dec 2012
    Posts
    19
    Quote Originally Posted by laserlight View Post
    Then you don't only want to compute a square, so pow becomes appropriate.


    That would be inefficient and you would have to deal with non-integral "power of ... whatever".
    um non-integral meaning???

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. GCC Header File Library Flag Listing?
    By LinuxSmith in forum Linux Programming
    Replies: 9
    Last Post: 08-15-2010, 07:42 PM
  2. Need Help With Header library file..
    By kensam in forum C Programming
    Replies: 8
    Last Post: 01-27-2010, 01:39 PM
  3. Gnu C++ Library Header file question?
    By xddxogm3 in forum C++ Programming
    Replies: 13
    Last Post: 10-01-2003, 03:48 PM
  4. C++ >> Standard Library >> Header File
    By hrk2006 in forum C++ Programming
    Replies: 2
    Last Post: 06-24-2003, 08:30 PM
  5. I want to make a *.lib for my header file
    By tudehopet in forum C++ Programming
    Replies: 6
    Last Post: 05-18-2002, 04:14 AM

Tags for this Thread