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

Soulstorm

macrumors 68000
Original poster
Feb 1, 2005
1,887
1
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*

macrumors member
Jul 22, 2002
76
0
* hidden between worlds
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.

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

macrumors 68000
Original poster
Feb 1, 2005
1,887
1
noht* said:
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.

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

macrumors 603
Jan 6, 2004
6,265
2,629
Western US
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

macrumors 601
Apr 8, 2004
4,029
142
Soulstorm said:
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

macrumors 68000
Mar 6, 2003
1,559
36
Toronto, Canada
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;
}
 

Soulstorm

macrumors 68000
Original poster
Feb 1, 2005
1,887
1
plinden said:
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

Moderator emeritus
Mar 10, 2004
14,657
1,464
Bergen, Norway
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

Moderator emeritus
Mar 10, 2004
14,657
1,464
Bergen, Norway
Just a thought: When programming in C/C++ this Programmer's reference 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

macrumors 68000
Original poster
Feb 1, 2005
1,887
1
Mitthrawnuruodo said:
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 f*** 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*

macrumors member
Jul 22, 2002
76
0
* hidden between worlds
Soulstorm said:
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

macrumors newbie
Aug 26, 2003
23
0
how to install curses.h for xcode

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 ?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.