Static library

Asked

Viewed 169 times

3

I created a static library on C, that is, after compiled the file .c, generated a file with extension .a. In the archive .c implemented some functions. How could I read that lib in another program and call a function from it? I’m just giving the include. h in the program, and when I compile the program it displays in the error message: indefinite reference to func() that follows below:

#ifndef STATIC_H
#define STATIC_H

extern int func();


#endif
  • she is with extension . h?

  • 1

    the static library is with extension . a

2 answers

2


Option 1 and I think you should have tried to use the header:

#include "libstatic.a"

Point to the correct library location.

2nd Option found in this answer of Soen:

cc -o yourprog yourprog.c -lstatic

or

cc -o yourprog yourprog.c libstatic.a

3rd Option also found in Soen:

gcc -I. -o jvct jvct.c libjvc.a

I found a link that teaches you how to create a C Lib (see if you haven’t forgotten any steps) Link

  • I tried to include the . h that contains the prototypes of the functions implemented in the static library (.a), and I compiled the program using -l+(name_lib_static) when compiling, but in compiling it gives the undefined refaction error in the call of the library function.

  • You tested the second option, and the link I referenced?

  • It worked out using the third option, thank you!!

2

I believe you have already created a . h by exporting the library functions; if you have done so just include the . a in the line that mounts your executable.

Well, I think I’d better put a more complete example:

test. h:

#ifndef STATIC_H
#define STATIC_H

    extern int func();


#endif

Static. c:

#include "teste.h"

int func() {

    return 10;

}

app. c:

#include <stdio.h>
#include "teste.h"

int main(int argc, const char **argv) {

    printf("func() retornou %d\n",func());
    return 0;
}

To compile:

gcc -o static.o -c static.c
ar rcs static.a static.c
gcc -o app.o -c app.c
gcc -o teste app.o static.a
  • So, I believe that’s it. I’m using extern, but only in . h, but it still didn’t. I posted . h in the question.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.