NO

Author Topic: How can I add functions to the static library?  (Read 3711 times)

Manish Aggarwal

  • Guest
How can I add functions to the static library?
« 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...........

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2115
Re: How can I add functions to the static library?
« Reply #1 on: September 10, 2015, 02:36:51 PM »
Create my.lib
Code: [Select]
polib.exe -out:my.lib obj1.obj
Add obj2.obj obj3.obj
Code: [Select]
polib.exe my.lib obj2.obj obj3.obj
May the source be with you

Manish Aggarwal

  • Guest
Re: How can I add functions to the static library?
« Reply #2 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.

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2115
Re: How can I add functions to the static library?
« Reply #3 on: September 10, 2015, 07:08:15 PM »
Do you included that lib too?
To linker options
or
#pragma comment(lib, "my.lib")
May the source be with you

Manish Aggarwal

  • Guest
Re: How can I add functions to the static library?
« Reply #4 on: September 10, 2015, 07:20:12 PM »
Yes Sir I included that but it did not work.

Manish Aggarwal

  • Guest
Re: How can I add functions to the static library?
« Reply #5 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.

« Last Edit: September 10, 2015, 07:40:45 PM by Manish Aggarwal »

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2115
Re: How can I add functions to the static library?
« Reply #6 on: September 10, 2015, 08:24:16 PM »
I could be calling convention problem.
Code: [Select]
#ifndef fact_file
#define fact_file
int __cdecl fact(int);
#endif
Look at example project.
May the source be with you

Manish Aggarwal

  • Guest
Re: How can I add functions to the static library?
« Reply #7 on: September 11, 2015, 05:10:52 AM »
Thankyou So much Sir for taking pain.......I am highly grateful to you  :)