Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Oct 8, 2010, 12:31 PM   #1
bahlquist
macrumors member
 
Join Date: Oct 2010
Using curses.h in Unix

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.
bahlquist is offline   0 Reply With Quote
Old Oct 8, 2010, 12:45 PM   #2
LPZ
macrumors 65816
 
Join Date: Jul 2006
Quote:
Originally Posted by bahlquist View Post
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?
LPZ is offline   0 Reply With Quote
Old Oct 8, 2010, 12:50 PM   #3
bahlquist
Thread Starter
macrumors member
 
Join Date: Oct 2010
Quote:
Originally Posted by LPZ View Post
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
bahlquist is offline   0 Reply With Quote
Old Oct 8, 2010, 12:52 PM   #4
LPZ
macrumors 65816
 
Join Date: Jul 2006
Quote:
Originally Posted by bahlquist View Post
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
LPZ is offline   0 Reply With Quote
Old Oct 8, 2010, 01:08 PM   #5
bahlquist
Thread Starter
macrumors member
 
Join Date: Oct 2010
Quote:
Originally Posted by LPZ View Post
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.
bahlquist is offline   0 Reply With Quote
Old Oct 8, 2010, 01:09 PM   #6
ulbador
macrumors 68000
 
ulbador's Avatar
 
Join Date: Feb 2010
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...
ulbador is offline   0 Reply With Quote
Old Oct 8, 2010, 01:24 PM   #7
bahlquist
Thread Starter
macrumors member
 
Join Date: Oct 2010
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.
bahlquist is offline   0 Reply With Quote
Old Oct 8, 2010, 01:34 PM   #8
ulbador
macrumors 68000
 
ulbador's Avatar
 
Join Date: Feb 2010
Quote:
Originally Posted by bahlquist View Post
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

Last edited by ulbador; Oct 8, 2010 at 01:43 PM.
ulbador is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Unix YOO-NIX Mac Basics and Help 4 Jul 20, 2013 06:05 PM
How UNIX is OS X? Caze OS X 10 Jun 16, 2013 03:43 PM
UNIX/linux monkeybagel Mac Pro 6 Nov 30, 2012 02:54 AM
UNIX question regarding wget Let's Sekuhara! Mac Programming 5 Jun 7, 2012 08:34 AM

Forum Jump

All times are GMT -5. The time now is 08:28 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC