PDA

View Full Version : Using curses.h in Unix




bahlquist
Oct 8, 2010, 12:31 PM
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.



LPZ
Oct 8, 2010, 12:45 PM
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.

Add -L/usr/lib -lcurses to your gcc command line?

bahlquist
Oct 8, 2010, 12:50 PM
Add -L/usr/lib -lcurses to your gcc command line?

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

LPZ
Oct 8, 2010, 12:52 PM
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

Well, /usr/lib should be in the default search path anyway. Omit the -L/usr/lib and just use -lcurses.

What then?

ie, gcc -o test -lcurses test.c

bahlquist
Oct 8, 2010, 01:08 PM
Well, /usr/lib should be in the default search path anyway. Omit the -L/usr/lib and just use -lcurses.

What then?

ie, gcc -o test -lcurses test.c

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.

ulbador
Oct 8, 2010, 01:09 PM
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...

bahlquist
Oct 8, 2010, 01:24 PM
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.

ulbador
Oct 8, 2010, 01:34 PM
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.


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