Pelles C forum

C language => Beginner questions => Topic started by: Manish Aggarwal on September 10, 2015, 09:42:52 AM

Title: How can I add functions to the static library?
Post by: Manish Aggarwal on September 10, 2015, 09:42:52 AM
If I want to add functions to the static library , I am unable to understand how should I do that...........
Title: Re: How can I add functions to the static library?
Post by: TimoVJL on September 10, 2015, 02:36:51 PM
Create my.lib
polib.exe -out:my.lib obj1.obj
Add obj2.obj obj3.obj
polib.exe my.lib obj2.obj obj3.obj
Title: Re: How can I add functions to the static library?
Post by: Manish Aggarwal on September 10, 2015, 06:45:51 PM
Thank you Sir
Based on your directions I was able to create my library file and modify it too.
But please Sir help me in one more issue.
To use that function from library file I need a header file. For that I wrote the prototype of that function and saved it with '.h' extension but when I include that file , this error displays :
POLINK: error: Unresolved external symbol 'fact'.
So please Sir help me in this issue.I shall be highly grateful to you.
Title: Re: How can I add functions to the static library?
Post by: TimoVJL on September 10, 2015, 07:08:15 PM
Do you included that lib too?
To linker options
or
#pragma comment(lib, "my.lib")
Title: Re: How can I add functions to the static library?
Post by: Manish Aggarwal on September 10, 2015, 07:20:12 PM
Yes Sir I included that but it did not work.
Title: Re: How can I add functions to the static library?
Post by: Manish Aggarwal on September 10, 2015, 07:35:23 PM
Sir for your convenience I want to tell you what I did So that you can correct me........
Sir I am new to programming So I must have done several several mistakes so please don't get annoyed...You may scold me but do correct me.
1. First of all I wrote a function to calculate factorial of a number as :

int fact(int num)
{
   int i,f=1;

   for(i=1;i<=num;i++)
    f=f*i;

    return(f);
}
2. Then I build it and a file fact.obj was created. Using this file I created a library file "fact.lib". I saved this file in pelles C library itself.
3. Then I created a header file as :

#ifndef fact_file
#define fact_file

int fact(int);

#endif

4. Then I saved that file with '.h' extension in the 'include' folder of pellesC in program files in c drive.

5. Then to use that function , I wrote a program as :
#include<stdio.h>
#include<fact.h>

int main()
{
   int a,b;
   a=6;
   b=fact(a);
   printf("%d",b);
}

6. Since the location of header file is standard I didnt use these quotes "  ".Instead I used < >.
7. Then I selected project options>linker and there i  included fact.lib .
8. Now If I build the program it gives the error as I told you earlier.

Title: Re: How can I add functions to the static library?
Post by: TimoVJL on September 10, 2015, 08:24:16 PM
I could be calling convention problem.#ifndef fact_file
#define fact_file
int __cdecl fact(int);
#endif
Look at example project.
Title: Re: How can I add functions to the static library?
Post by: Manish Aggarwal on September 11, 2015, 05:10:52 AM
Thankyou So much Sir for taking pain.......I am highly grateful to you  :)