Using curses.h in Unix

Discussion in 'Mac Programming' started by bahlquist, Oct 8, 2010.

  1. macrumors member

    Joined:
    Oct 6, 2010
    #1
    I am new to programming. I want to use the curses library (mostly for getch()), but when I compile the following program:

    #include <curses.h>

    main()
    {
    getch();
    }

    I get errors:

    Undefined symbols:
    "_stdscr", referenced from:
    _stdscr$non_lazy_ptr in ccHJ7Nf8.o
    "_wgetch", referenced from:
    _main in ccHJ7Nf8.o
    ld: symbol(s) not found
    collect2: ld returned 1 exit status

    I read that you must link the library or something like that. Any ideas?
    Thanks.
     
  2. LPZ
    macrumors 65816

    Joined:
    Jul 11, 2006
    #2
    Add
    Code:
    -L/usr/lib -lcurses
    to your gcc command line?
     
  3. thread starter macrumors member

    Joined:
    Oct 6, 2010
    #3
    Thanks for your reply. When I do that while in the directory my source file is in, I get the error:

    -bash: -L/usr/lib: No such file or directory
     
  4. LPZ
    macrumors 65816

    Joined:
    Jul 11, 2006
    #4
    Well, /usr/lib should be in the default search path anyway. Omit the -L/usr/lib and just use -lcurses.

    What then?

    ie,
    Code:
    gcc -o test -lcurses test.c
     
  5. thread starter macrumors member

    Joined:
    Oct 6, 2010
    #5
    My source file is main.c. I used:

    gcc -o main -lcurses main.c

    with no errors. But when I try to compile main.c, I get the same errors.
     
  6. macrumors 68000

    ulbador

    Joined:
    Feb 11, 2010
    #6
    You also need to make sure you have the devel package for curses installed, if you didn't install it from source. How to do this will vary wildly depending on what your flavor of Unix or Linux is.

    Edit...

    You say this went off without a hitch:

    gcc -o main -lcurses main.c


    That means your program compile was successful and you'll have a file named "main" in the current directory which you can just execute with:

    ./main

    If you are trying to compile something else after this, you are a little confused...
     
  7. thread starter macrumors member

    Joined:
    Oct 6, 2010
    #7
    OK, thanks. Yes, duh, the command:

    gcc -o main -lcurses main.c

    compiles main.c (new to all of this). But I'm still confused. Why can I not handle the inclusion within my source code? That would be a much cleaner solution.
     
  8. macrumors 68000

    ulbador

    Joined:
    Feb 11, 2010
    #8

    What inclusion are you talking about? The -lcurses?

    It's a shared library. The #include part in your source tells the compiler that you want to use code that is prototyped in the curses.h. The -lcurses part actually links that code into your program.

    As your program gets larger and larger, instead of just dropping all your code in a single file, you will create separate files and just link all the code in.

    Eventually you'll create a neat little Makefile to do this for you
     

Share This Page