Static Library

3

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

#ifndef STATIC_H
#define STATIC_H

extern int func();


#endif
    
asked by anonymous 10.07.2015 / 16:49

2 answers

2

First option and I think you should have tried is to use the header:

#include "libstatic.a"

Point to the correct location for the library.

2nd option found in this response from SOen : / p>

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 Lib in C (see if you have not forgotten any steps) Link

    
10.07.2015 / 16:57
2

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

Well, 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
    
10.07.2015 / 17:35