Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

bahlquist

macrumors member
Original poster
Oct 6, 2010
41
0
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

macrumors 65816
Jul 11, 2006
1,221
2
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
Code:
-L/usr/lib -lcurses
to your gcc command line?
 

bahlquist

macrumors member
Original poster
Oct 6, 2010
41
0
Add
Code:
-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

macrumors 65816
Jul 11, 2006
1,221
2
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,
Code:
gcc -o test -lcurses test.c
 

bahlquist

macrumors member
Original poster
Oct 6, 2010
41
0
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

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

macrumors 68000
Feb 11, 2010
1,554
0
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

macrumors member
Original poster
Oct 6, 2010
41
0
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

macrumors 68000
Feb 11, 2010
1,554
0
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
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.