Skip to content

modules: programs: dev: add gcc-libiberty

Valentin Chassignol requested to merge (removed):pkgs/libiberty into master

gcc liberty library allows devs to use well-written implementations of standard functions (or at least common).

It provides :

  • Supplemental Functions (no standard functions)
  • Replacement Functions (rewrite of standard functions)
  • Extensions (unused here)

For instance, asprintf family or getopt_long are no more available by default (and can not be enabled because of -pedantic, explained under) on school computers.

They are particulary useful when manipulating strings or memory. There is a lot of rewrite for memory management function (xmalloc, xrealloc, memcpy, memmove, ...), string manipulation (str* functions, *sprintf functions ...).

Actually, the code is compiled using : gcc test.c -std=c99 -pedantic -Werror. For instance, asprintf functions can be enable with GNU_SOURCE macro (DEFAULT_SOURCE does not). But, by setting GNU_SOURCE macro, it activates Float extensions which are not ISO C and trigger the -pedantic error with gcc.

Float extension error
/include/stdlib.h:140:8: error: ISO C does not support the ‘_Float32’ type [-Werror=pedantic]
  140 | extern _Float32 strtof32 (const char *__restrict __nptr,
      |        ^~~~~~~~
/include/stdlib.h:146:8: error: ISO C does not support the ‘_Float64’ type [-Werror=pedantic]
  146 | extern _Float64 strtof64 (const char *__restrict __nptr,
      |        ^~~~~~~~
/include/stdlib.h:152:8: error: ISO C does not support the ‘_Float128’ type [-Werror=pedantic]
  152 | extern _Float128 strtof128 (const char *__restrict __nptr,
      |        ^~~~~~~~~
/include/stdlib.h:158:8: error: ISO C does not support the ‘_Float32x’ type [-Werror=pedantic]
  158 | extern _Float32x strtof32x (const char *__restrict __nptr,
      |        ^~~~~~~~~
/include/stdlib.h:164:8: error: ISO C does not support the ‘_Float64x’ type [-Werror=pedantic]
  164 | extern _Float64x strtof64x (const char *__restrict __nptr,
      |        ^~~~~~~~~

It don't know why but it does not trigger pedantic warning when compiling with clang.

This way, the merge request re-allows student to have a well-written set of functions which can no more be enabled on computers (which are allowed and used).

Example of code

example.c

#include <libiberty/libiberty.h>
...
char *my_str = xasprintf("%c%d%s", c, 8, "Hello my friend !");

and compiled with -liberty

gcc example.c -std=c99 -pedantic -Werror -liberty

Another example would be the given function xmalloc of 42sh which is implemented in libiberty here

More information on the documentation : https://gcc.gnu.org/onlinedocs/gcc-7.5.0/libiberty.pdf

Edited by Marc Schmitt

Merge request reports