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)
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)
Do you have the correct includes for these functions?
Under the linking section of your project, you want to add Other linker flags : -lncurses.
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;
}
How to compile c in xcode?