Stepan Loginov
Stepan Loginov

Reputation: 1767

Check that all symbols defined in static library

I built some static library with no errors.
When I tried to link it to some app I realized that some symbols is undefined inside library.

I want to get error on library building step not on app.

In other words

$ cat mylib.h
void foo();
$ cat mylib.c
#include "mylib.h"
// no foo() implementation here;
$ cat test.c 
#include "mylib.h"

int main() {
    foo();
    return 0;
}
$ gcc mylib.c -c
$ ar crf libmylib.a *.o
$ gcc test.c -lmylib -L.
/tmp/cc7201uP.o: In function `main':
test.c:(.text+0xa): undefined reference to `foo'
collect2: error: ld returned 1 exit status

I want to get error at one of those step

$ gcc mylib.c -c
$ ar crf libmylib.a *.o

Is it possible, and what is good practice for such situations?

Updated: I tried $ nm --undefined-only *.a output is :

mylib.o:

It's little bit strange for me. I expect something like foo() inside output.

Upvotes: 2

Views: 1034

Answers (1)

Maxim Egorushkin
Maxim Egorushkin

Reputation: 136238

A static library is just a bunch of .o files and those are expected to have undefined references. No linking is involved when producing a .a from .o files.

You can run nm --undefined-only libmylib.a and that produces a list of all undefined symbols. That list will include all the symbols you use from C standard library because, again, no linking is involved when producing .a files.

Upvotes: 3

Related Questions