1

I want to create a static library implementing a TUI interface using ncurses. I'm new to this topic and applying these answers didn't work out for me.

I want to link varViewer.c in a library. I use this code to compile a example file.

Compiling src/example.c and src/viewer/varViewer.c:

    gcc -Wall -Wextra -Wnonnull  -Isrc/viewer/
     -c src/example.c -o obs/example.o -lncurses
     -fsanitize=undefined -fprofile-arcs -ftest-coverage -Winline -O2
    gcc -Wall -Wextra -Wnonnull  -Isrc/viewer/ 
     -c src/viewer/varViewer.c -o obs/viewer/varViewer.o -lncurses
     -fsanitize=undefined -fprofile-arcs -ftest-coverage -Winline -O2

Linking obs/example.o to bin/example:

    gcc -o bin/example -Isrc/viewer/
     obs/example.o obs/viewer/varViewer.o -lncurses
     -fsanitize=undefined -fprofile-arcs -ftest-coverage -Winline -O2

bin/example works as expected. I tried to compile a suitable object file:

Compiling src/viewer/varViewer.c:

    gcc -Wall -Wextra -Wnonnull  -Isrc/viewer/
    -c src/viewer/varViewer.c -o lib/objects/varViewer.o
    -lncurses -fPIC -O2

and put it into library:

    ar -cvr lib/libvarViewer.a lib/objects/varViewer.o

when I try to use it in other projects, a include it using

-L ..../src/viewer/lib -lvarViewer

flags but all references to functions I used from ncurses library and string.h are undefined:

nm /lib/libvarViewer.a

                .... 
             U __snprintf_chk
             U __stack_chk_fail
             U start_color
             U stdscr
             U strchr
             U strlen
             U strncmp
             U strncpy
             U strnlen
             U waddnstr
             U wborder
             U wclear
             U wcolor_set
             U wgetnstr
             U winsdelln
             U winsnstr
             U wmove
             U wrefresh

What is the correct way to call ar? I already tried to produce a "thin" library using -T option.

Community
  • 1
  • 1

1 Answers1

0

The command

gcc -Wall -Wextra -Wnonnull  -Isrc/viewer/
-c src/viewer/varViewer.c -o lib/objects/varViewer.o
-lncurses -fPIC -O2

will simply compile the varViewer.c file and thus, will ignore the libraries because it won't link to a final executable.

Consequently, the object file varViewer.o will have undefined references (U reported in nm as you have shown) to the symbols not defined within varViewer.c. These not only include the libncurses library but also the regular libc library (which provides symbols such as strlen, strncpy, ...). However, remember that libc will be automatically added into your link stage by the compiler unless you explicitly state otherwise.

You could use the command ar x to extract the object files (.o) from other static libraries and then use ar again to generate a new library. While this is possible I don't think that including all the libraries into yours is a good idea. I think that it is better to link against all the libraries needed because that ensures that the application is linked against the latest version available -- while if you embed one library into yours, then will stick to that version until you upgrade it.

Harald
  • 3,110
  • 1
  • 24
  • 35
  • Thank you:-) I tried to follow your advice and linked against ncurses lib. Unfortunately when I try to link against `/usr/lib/x86_64-linux-gnu/libncurses.a` using `gcc -I../viewer/lib garbage.c -L../viewer/lib/ -lvarViewer -l:libncurses.a` I get a lot of undefined references while `gcc -I../viewer/lib garbage.c -L../viewer/lib/ -lvarViewer -lncurses` works. Am I missing something? According to [this question](http://stackoverflow.com/questions/3514852/statically-link-ncurses-to-program?rq=1) this should work. –  Aug 26 '16 at 15:35
  • Undefined references to which symbols? – Harald Aug 26 '16 at 16:25
  • I try to summarizes it: In functions I wrote macros like `COLS, LINES, stdscr` are undefined. In functions from libncurses some terminal related symbols (tparm, SP, cur_term, ...) are missing: example: /usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/libncurses.a(lib_box.o): In function `wborder': (.text+0x1f): undefined reference 'acs_map' –  Aug 26 '16 at 17:00
  • Those are in `libtinfo.a` – Thomas Dickey Aug 27 '16 at 00:42