Please explain to me... No2

Discussion in 'Mac Programming' started by Soulstorm, Mar 24, 2005.

  1. macrumors 68000

    Soulstorm

    Joined:
    Feb 1, 2005
    #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

    But althought the file can be compiled, the log tells me:
    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?
     
  2. macrumors member

    Joined:
    Jul 22, 2002
    Location:
    * hidden between worlds
    #2
    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...
     
  3. thread starter macrumors 68000

    Soulstorm

    Joined:
    Feb 1, 2005
    #3
    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?
     
  4. macrumors 601

    HiRez

    Joined:
    Jan 6, 2004
    Location:
    Western US
    #4
    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.
     
  5. macrumors 68040

    plinden

    Joined:
    Apr 8, 2004
    #5
    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?
     
  6. macrumors 65816

    MarkCollette

    Joined:
    Mar 6, 2003
    Location:
    Calgary, Canada
    #6
    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;
    }
     
  7. Moderator emeritus

    Mitthrawnuruodo

    Joined:
    Mar 10, 2004
    Location:
    Bergen, Norway
    #7
    Try #include <iostream.h>
    (Edit: instead of #include <iostream>, I mean...)
     
  8. thread starter macrumors 68000

    Soulstorm

    Joined:
    Feb 1, 2005
    #8
    The problem is:

    this code compiles just fine, but when I run the program, I get this message at the log: (using XCode)
    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:
    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().
     
  9. Moderator emeritus

    Mitthrawnuruodo

    Joined:
    Mar 10, 2004
    Location:
    Bergen, Norway
    #9
    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... ;)
     
  10. Moderator emeritus

    Mitthrawnuruodo

    Joined:
    Mar 10, 2004
    Location:
    Bergen, Norway
    #10
    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!
     
  11. thread starter macrumors 68000

    Soulstorm

    Joined:
    Feb 1, 2005
    #11
    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
     
  12. macrumors member

    Joined:
    Jul 22, 2002
    Location:
    * hidden between worlds
    #12
    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.
     
  13. macrumors newbie

    Joined:
    Aug 26, 2003
    #13
    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 ?
     

Share This Page