Pq101

Q: How to compile c in xcode?

I have recently started using Xcode and i am unable to use getch() ,clrscr() etc functions to compile c code. Is there a way to make it work.

MacBook Pro, OS X El Capitan (10.11.6)

Posted on Aug 6, 2016 6:39 AM

Close

Q: How to compile c in xcode?

  • All replies
  • Helpful answers

  • by rccharles,

    rccharles rccharles Aug 6, 2016 8:21 AM in response to Pq101
    Level 6 (8,459 points)
    Classic Mac OS
    Aug 6, 2016 8:21 AM in response to Pq101

    Do you have the correct includes for these functions? 

  • by VikingOSX,

    VikingOSX VikingOSX Aug 6, 2016 9:17 AM in response to Pq101
    Level 7 (20,591 points)
    Mac OS X
    Aug 6, 2016 9:17 AM in response to Pq101

    Under the linking section of your project, you want to add Other linker flags : -lncurses.

    Screen Shot 2016-08-06 at 12.07.21 PM.jpgScreen Shot 2016-08-06 at 11.06.43 AM.jpg

     

    Here is example code that is a combination of using this 2010 post, and an example from (7. input functions) from the NCURSES Programming Howto site. It compiles cleanly, and runs as it should in the Terminal. It does not launch Terminal from within Xcode and run the code though.

    //

    //  main.c

    //  ncurse_this

    //

    //  Created by VikingOSX on 8/6/16.

    //  Copyright © 2016 org.codesmith.us. All rights reserved.

    //

     

     

    #include <stdio.h>

    #include <ncurses.h>

    #include <string.h>

     

     

     

    #define WORLD_WIDTH 50

    #define WORLD_HEIGHT 20

     

     

     

    int main(int argc, const char * argv[]) {

     

     

        WINDOW *Win;

        int offsetx, offsety;

       

        char mesg[] = "Enter a string: ";

        char str[80];

        u_long row, col;

       

        initscr();

        refresh();

           

        offsetx = (COLS - WORLD_WIDTH) / 2;

        offsety = (LINES - WORLD_HEIGHT) / 2;

           

        Win = newwin(WORLD_HEIGHT,

                     WORLD_WIDTH,

                     offsety,

                     offsetx);

           

        box(Win, 0 , 0);

        wrefresh(Win);

        getmaxyx(stdscr,row,col);

        mvprintw(row/2.0, (col-strlen(mesg))/2.0, "%s", mesg);

        /* print the message at the center of the screen */

        getstr(str);

        mvprintw(LINES - 2, 0, "You Entered: %s", str);

        getch();

        delwin(Win);

        endwin();

           

        return 0;

    }

    Screen Shot 2016-08-06 at 12.16.26 PM.jpg