I'm getting an implicit declaration error only when using -std=c99

1.7k Views Asked by At

For some reason, -std=c99 is keeping gcc from seeing the declaration of function wborder_set() (which lives in <curses.h>)

#include <curses.h>
#include <locale.h>

int main(int argc, char* argv[]){
  initscr(); // start ncurses
  cbreak();  // don't wait for lf to getch
  noecho();  // don't copy entered characters
  nonl();    // use /r/l
  clear();   // clear the screen!

  setlocale(LC_CTYPE, "");

  int ySize, xSize;
  getmaxyx(stdscr, ySize, xSize);
  WINDOW *upperWin = newwin(ySize, xSize, 0, 0);

  // magic utf encodings for window border
  wborder_set( upperWin, "\u1234", "\u1234", "\u1234", "\u1234",
           "\u1234", "\u1234", "\u1234", "\u1234" );
  wrefresh(upperWin);
  getch();
  return 0;
}

Compiling with gcc test.c -lncursesw -o cursestest works fine! If, however, I compile with

gcc test.c -std=c99 -lncursesw -o cursestest

it replies,

cursescurses.c: In function ‘main’: cursescurses.c:18:7: warning: implicit declaration of function ‘wborder_set’ [-Wimplicit-function-declaration] wborder_set( upperWin, "\u1234", "\u1234", "\u1234", "\u1234",

which leads me to believe that I can't trust that it is properly linking wborder_set.

Why would this happen?

3

There are 3 best solutions below

0
On BEST ANSWER

It turns out the problem was that I had included the wrong version of <curses.h>. The function wborder_set implements unicode and needed the unicode/wide char extension of the ncurses library, which I included with #include <ncursesw/curses.h>. With this substitution, the program compiled as expected.

0
On

Defining _XOPEN_SOURCE_EXTENDED would help, but Linux (like most systems) has pitfalls in the form of extensions which conflict with the standards, and differing interpretations of the standards. So... for Linux, the universal band-aid for this is to start by defining _GNU_SOURCE (which turns on the appropriate X/Open defines as an afterthought).

If your ncurses configuration includes a reasonably current ncursesw5-config script it will add that -D option.

2
On

Your <curses.h> doesn't seem to include a prototype for the function. The rules changed in C99: using functions without a declaration is not allowed anymore. Before, they would be assumed to return int.

This has nothing to do with linking, but this a question about the compiler expecting a certain return type for a function.