This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

How to create Header file for a C function

I created a new project( CHOOSING THE library function) and write the code :

#include<stdio.h>

//signed long int pow(signed int base, signed int power)
signed long int pow(signed int base, signed int power); signed long int pow(signed int base, signed int power)
{ signed long int result_pow = 1;
for(;power>0;power--)
{ result_pow = result_pow * base;
} return(result_pow);
}

this generated a power.lib as library file after building .
Now I created a header file with the code :

#ifndef __power_h
#define __power_h

//signed long int pow(signed int base, signed int power)
signed long int pow(signed int base, signed int power); signed long int pow(signed int base, signed int power)
{ signed long int result_pow = 1;
for(;power>0;power--)
{ result_pow = result_pow * base;
} return(result_pow);
}

#endif

Now I m having the error :
Power.axf: Error: L6218E: Undefined symbol main (referred from rtentry2.o).

=>How to link the new created header file with library file .
=>How do i solve this error and HOW TO CREATE A 'C' HEADER FILE FOR 'C' FUCTIONS .

  • This really is basic 'C' textbook stuff - nothing to do with the fact that it's a library, and nothing specifically to do with Keil.

    c-faq.com/.../decldef.html

    There are some general programming books here: http://www.keil.com/books

    Here's an online textbook: publications.gbdirect.co.uk/.../

  • You should explain to the users of your library how to avoid conflicts between your pow() and the Standard library's pow() declared in math.h.

  • I have to make a separate function . I was using this as testing and to get to know how to create a user defined function like that in a header file .
    => It will be very nice of you if you just give a simple example to create and use such a user defied header file created in C.

  • Was the example in the provided C-FAQ link not simple enough?

  • According to that I created a .h file ad write the code as :-

    #include<stdio.h>

    extern signed long int pow(signed int base, signed int power);
    signed long int pow(signed int base, signed int power)
    { signed long int result_pow = 1;
    for(;power>0;power--)
    { result_pow = result_pow * base;
    } return(result_pow);
    }

    and then i added this header file in my main C file as :-

    #include<stdio.h>
    #include<lpc21xx.h>
    #include<power.h>

    int main(void)
    { signed int si_int = 2; /* initialize the serial interface */ PINSEL0 = 0x00050000; /* Enable RxD1 and TxD1 */ U1LCR = 0x83; /* 8 bits, no Parity, 1 Stop bit */ U1DLL = 97; /* 9600 Baud Rate @ 15MHz VPB Clock */ U1LCR = 0x03; /* DLAB = 0 */

    si_int = pow(si_int,si_int); printf("%ld",si_int); while(1);
    }

    Now it is compiling correctly but when it starts debugging , command winndow shows:

    Running with Code Size Limit: 32K
    Load "C:\\Keil\\Himanshu\\Power\\Power.AXF"

    *** Restricted Version with 32768 Byte Code Size Limit
    *** Currently used: 4108 Bytes (12%)

    BS \Power\17
    BS \Power\12
    BS \Power\11
    BS \Power\10
    BS \Power\7

    And I m not able to see any output on UART .
    Also when I try to add si_int in watch1 it shows asn error " can't add si_int ". While the command window display the following message :

    WS 1,'si_int
    _____^
    *** error 34: undefined identifier

    I checked it number of times but there is no " ' " before si_int .

    => please help me out

  • Sorry the second error is :

    WS 1,'result_pow
    _____^
    *** error 34: undefined identifier
    WS 2,'si_int
    _____^
    *** error 34: undefined identifier

  • Start by helping yourself out: there are very clear and simple instructions on how to post source code:

    www.danlhenry.com/.../keil_code.png

    please follow those instructions!

  • According to that I created a .h file ad write the code as :-

    #include<stdio.h>
    
    extern signed long int pow(signed int base, signed int power);
    signed long int pow(signed int base, signed int power)
    {
    signed long int result_pow = 1;
    for(;power>0;power--)
    {
    result_pow = result_pow * base;
    }
    return(result_pow);
    }
    


    and then i added this header file in my main C file as :-

    #include<stdio.h>
    #include<lpc21xx.h>
    #include<power.h>
    
    int main(void)
    { signed int si_int = 2;
    /* initialize the serial interface */
    PINSEL0 = 0x00050000; /* Enable RxD1 and TxD1 */
    U1LCR = 0x83; /* 8 bits, no Parity, 1 Stop bit */ U1DLL = 97; /* 9600 Baud Rate @ 15MHz VPB Clock */ U1LCR = 0x03; /* DLAB = 0 */
    
    si_int = pow(si_int,si_int);
    printf("%ld",si_int);
     while(1);
    }
    

    Now it is compiling correctly but when it starts debugging , command winndow shows:

    Running with Code Size Limit: 32K
    Load "C:\\Keil\\Himanshu\\Power\\Power.AXF"

    *** Restricted Version with 32768 Byte Code Size Limit
    *** Currently used: 4108 Bytes (12%)

    BS \Power\17
    BS \Power\12
    BS \Power\11
    BS \Power\10
    BS \Power\7

    And I m not able to see any output on UART .
    Also when I try to add si_int in watch1 it shows asn error " can't add si_int ". While the command window display the following message :

    WS 1,'result_pow
    _____^
    *** error 34: undefined identifier
    WS 2,'si_int
    _____^
    *** error 34: undefined identifier
    I checked it number of times but there is no " ' " before si_int .

    => please help me out

  • "According to that I created a .h file ad write the code as ..."

    No. You didn't follow the links contained therein, one of which would have taken you here:

    c-faq.com/.../hfiles.html

    Function declarations belong in the .h file.
    Function definitions belong in the .c file.

  • Thanks alot "Dan Hennry" ad "Andy Neil" , finally I got the output from my first header file .
    Thnks alot again......