PDA

View Full Version : Please explain to me... No2




Soulstorm
Mar 24, 2005, 07:08 AM
Sorry, but I need some help (again).
I am moving forward into learning the basics of C++. My book (that damned book) says that in order to include the command "getchar in my file, in need to include the header <conio.h>. But then it says that in unix, that header is different (it is <curses.h>).

So, when I compile it in OS X using XCode, I need to include the <curses.h>. I do that: and this is my code

#include <iostream>
#include <curses.h>
using namespace std;

int main () {
cout << "Hello, World!\n";
getch();
return 0;
}

But althought the file can be compiled, the log tells me:
Session started at 2005-03-24 15:47:45 +0200.]
ZeroLink: unknown symbol '_stdscr'

Executable “c++ tool” has exited due to signal 6 (SIGABRT)

Can you point me into the right direction? Can you also tell me how can I see what functions and headers does each operating system include, so that I can use headers correctly?



noht*
Mar 24, 2005, 09:07 AM
you'll have to include libncurses if you use functions from it. if you build from the console (which i recommend), just use "g++ file.cpp -o myapp -lcurses"

in xcode, you should add libncurses.a to the "frameworks and libraries" folder of your project. note that curses is not included in a default install of the developer tools, you can get it via fink (http://fink.sf.net).

hope that helps a bit, feel free to ask again.

[e] whoops, that's not true. curses IS included, it's at /usr/lib/libcurses.dylib
you can't get there in xcode's open panel, however, as it's invisible for the finder...

Soulstorm
Mar 24, 2005, 11:07 AM
you'll have to include libncurses if you use functions from it. if you build from the console (which i recommend), just use "g++ file.cpp -o myapp -lcurses"

in xcode, you should add libncurses.a to the "frameworks and libraries" folder of your project. note that curses is not included in a default install of the developer tools, you can get it via fink (http://fink.sf.net).

hope that helps a bit, feel free to ask again.

[e] whoops, that's not true. curses IS included, it's at /usr/lib/libcurses.dylib
you can't get there in xcode's open panel, however, as it's invisible for the finder...

You misunderstood...
The method you describe, although I understand it, is far more complex than I need it to be. I intend to use XCode to build simple console applications. And I want to use code that will work in BOTH Mac and PC (provided I have the necessary compilers).

So my real problem is that the following code:
#include <iostream>
#include <curses.h>
using namespace std;

int main () {
cout << "Hello, World!\n";
getch();
return 0;
} works with DevC++ in PC (if I replace <curses.h> with <conio.h>), but does not work with XCode!
So I am requesting more info on how can I make C++ code that works in both PC and Macs.

I am asking this because in our University, they ask us to Program in DevC++ but I don't want to Use Virtual PC all the time, plus I find XCode to be more flexible.

So, can anyone give me any info on C++ headers in OS X?

HiRez
Mar 24, 2005, 11:59 AM
What happens if you run the app from Terminal instead of from Xcode? Or maybe try curses.getch() or ncurses.getch()? Supposedly ncurses replaced curses but I don't know if you need a separate header/library. Sorry I do not program in C++, but curses does work fine on the Mac when using Python, anyway. I'm sure there's a way to do it.

plinden
Mar 24, 2005, 02:19 PM
I am asking this because in our University, they ask us to Program in DevC++ but I don't want to Use Virtual PC all the time, plus I find XCode to be more flexible.

So, can anyone give me any info on C++ headers in OS X?

You've come across the perennial problem with cross compiler support in C/C++, although to be honest I'm not sure what the exact problem is. I was under the impression from reading about DevC++ that it used gcc as it's compiler. gcc is cross platform, such a simple program as yours should compile on PCs, Macs, Unix and Linux.

What happens if you include curses.h in DevC++ rather than conio.h?

MarkCollette
Mar 24, 2005, 03:30 PM
You need to use #ifdef. Unfortunately, I'm not sure which defines would be set for Win32 versus OS X, so I'll just put some placeholders in to illustrate my point:

#ifdef WIN_32
#include <conio.h>
#else
#include <curses.h>
#endif

#include <iostream>
using namespace std;

int main () {
cout << "Hello, World!\n";
getch();
return 0;
}

Mitthrawnuruodo
Mar 24, 2005, 04:07 PM
Try #include <iostream.h>
(Edit: instead of #include <iostream>, I mean...)

Soulstorm
Mar 24, 2005, 04:48 PM
You've come across the perennial problem with cross compiler support in C/C++, although to be honest I'm not sure what the exact problem is.
The problem is:

#include <iostream.h>
#include <curses.h>
#include <stdio.h>
using namespace std;

int main () {
cout << "Hello, World!\n Press any button to proceed. This is my first program with this application...";
getch();
return 0;
}

this code compiles just fine, but when I run the program, I get this message at the log: (using XCode)
[Session started at 2005-03-25 00:46:44 +0200.]
ZeroLink: unknown symbol '_stdscr'

Executable “c++ tool” has exited due to signal 6 (SIGABRT).
Why doesn't it work? Why it gives me an error message?

I try to compile it using the terminal:
And what i get is this:
ld: Undefined symbols:
_stdscr
_wgetch
And the file cannot be compiled. What the heck is going on?

NOTE: I can face this problem, by including <stdio.h> and then using getchar() instead of getc(), and it works just fine. I just wanna know what is the problem using getc().

Mitthrawnuruodo
Mar 24, 2005, 05:11 PM
The getc() needs #include <stdio.h>, just like getchar().

But while getchar() returns the next character from standard in (in this case the keyboard) the getc() needs an input stream as argument, and reads the next caracter from that: int getc(FILE *stream);

Stick with getchar(), for now... ;)

Mitthrawnuruodo
Mar 24, 2005, 05:16 PM
Just a thought: When programming in C/C++ this Programmer's reference (http://www.amazon.com/exec/obidos/ASIN/0072127066/ref=nosim/edazzlenet-20/102-7841414-3742555?dev-t=08FC0AFA9SSP0BEHY8G2) is your best friend. When I was teaching or when I'm prgramming in C++ this is basically all I need... It's cheap and a great help when your stuck on which functions you can and should use, and which files you need to include... it even has small examples...

Highly recommended!

Soulstorm
Mar 25, 2005, 12:43 AM
The getc() needs #include <stdio.h>, just like getchar().

But while getchar() returns the next character from standard in (in this case the keyboard) the getc() needs an input stream as argument, and reads the next caracter from that: int getc(FILE *stream);

Stick with getchar(), for now... ;)
I think that my book is not only outdated, but also bad written. Heck, what the ******* did they think when they let this proffessor tech us programming...

Greece... Beatiful country, but as far as computing is concerned, the people here don't even know what is it!

EDIT: The book you mention seems to have very helpful information... I will try to find something similar here in Athens, otherwise, I will buy this from Amazon.co.uk

noht*
Mar 27, 2005, 04:03 PM
You misunderstood...
The method you describe, although I understand it, is far more complex than I need it to be. I intend to use XCode to build simple console applications. And I want to use code that will work in BOTH Mac and PC (provided I have the necessary compilers).

So my real problem is that the following code:
works with DevC++ in PC (if I replace <curses.h> with <conio.h>), but does not work with XCode!
So I am requesting more info on how can I make C++ code that works in both PC and Macs.

I am asking this because in our University, they ask us to Program in DevC++ but I don't want to Use Virtual PC all the time, plus I find XCode to be more flexible.

So, can anyone give me any info on C++ headers in OS X?

ok, i think i wasn't specific enough. your problem has nothing to do with header files, but with the corresponding libraries. including a header file in a project provides the compiler with the appropriate function prototypes for the library routines that you are using. however, the linker still has to include actual code in your binary, and you have to tell it where to look for that code.
thus the error you describe, "ZeroLink: unknown symbol '_stdscr'". this means that the compiler has included references to the getch() method into the code using the information from the curses header, but the linker doesn't know where to look for the actual code for the getch() routine.
you'll have to include the curses library into your xcode project if you're using functions from that library.

regarding the <conio.h>/<curses.h> confusion, i believe conio.h is borland-specific. it is not always trivial to see where a specific function is declared. use man (nameoffunction) if you know the name of the method you're looking for.

mspock
Sep 9, 2005, 11:04 AM
regarding the <conio.h>/<curses.h> confusion, i believe conio.h is borland-specific. it is not always trivial to see where a specific function is declared. use man (nameoffunction) if you know the name of the method you're looking for.[/QUOTE]



Ok i want to use that as well, how do i install the curses.h library so that xcode knows about it and i can use fonctions from it ?