[cs631apue] getprogname and setprogname

Jan Schaumann jschauma at stevens.edu
Mon Sep 16 12:10:55 EDT 2013

Nick Smith <nsmith1 at stevens.edu> wrote:
> In the code style guide, there is a note to use getprogname(3) and  
> setprogname(3) to get/set the name of the executable for compatibility  
> on some systems. I've tried including this in my code and haven't been  
> able to include the definitions on my GNU/Linux system or on the 
> linux-lab.

Correct, Linux does not provide getprogname(3)/setprogname(3) as part of
the standard library:

$ cat moo.c
#include <bsd/stdlib.h>
#include <stdio.h>

main(int argc, char **argv) {
	if (argc != 2) {
		fprintf(stderr, "Usage: %s 'moo'\n", getprogname());
	printf("%s %s\n", getprogname(), argv[1]);
$ cc -Wall moo.c
/tmp/ccbIIvi9.o: In function `main':
moo.c:(.text+0x16): undefined reference to `setprogname'
moo.c:(.text+0x21): undefined reference to `getprogname'
moo.c:(.text+0x5a): undefined reference to `getprogname'
collect2: ld returned 1 exit status

Note that the error is not a syntax error at compile time, but a linking
error.  (We will go into the differences in more detail in a future

On Linux, these functions are provided by a different library, called

$ cc -Wall moo.c -lbsd
$ ./a.out
Usage: cowsays 'moo'
$ ./a.out moo
cowsays moo

Note that the package providing the library and header files does not
install a manual page for getprogname(3)/setprogname(3):

$ dpkg -S /usr/include/bsd/stdlib.h           
libbsd-dev: /usr/include/bsd/stdlib.h
$ dpkg -L libbsd-dev 

This is rather unfortunate, since normally a manual page would include
the information of which library to link against.  See, for example,


       ceil, ceilf, ceill - ceiling function: smallest integral value not less
       than argument

       #include <math.h>

       double ceil(double x);
       float ceilf(float x);
       long double ceill(long double x);

       Link with -lm.


The quality of manual pages, documentation, and packages varies
significnatly across different versions of Unix (and even different
variations or distributions of Linux).


More information about the cs631apue mailing list