Anake
Anake

Reputation: 7659

Cannot compile basic tutorial in C

I am just beginning to try to learn C and have been trying to do tutorials but they won't compile. The errors seem to suggest it is an issue with me using a 64 bit OS (lion), but I can't see how that could be the case with such a simple program.

The code: (copied directly from a 'thenewboston' tutorial)

#include <stdio.h>

int main(void)
{
    printf("Hello World");
    getch();
}

I then entered this in terminal:

gcc tnb_1.c

And the error I got was:

Undefined symbols for architecture x86_64:
  "_getch", referenced from:
      _main in cc2nMvOk.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status

The compiler I am using is the standard one installed with xcode.

I am sorry if this question has an obvious answer, but as I can't get past the first hurdle I am finding it hard to learn anything.

Thank you for your help

Upvotes: 1

Views: 1770

Answers (2)

Mitch Lindgren
Mitch Lindgren

Reputation: 2170

If you replace getch() with getchar() or getc(stdin), it should work. As Oli Charlesworth suggested, though, you might want to look at different tutorials. getch() is in the curses library (and some old DOS compilers), so you won't be able to build an executable without linking that. If the tutorial didn't specify that, it's probably not very good.

Upvotes: 3

Trott
Trott

Reputation: 70133

Use getchar() instead of getch(). getch() is non-standard.

Using getchar() will (probably) mean you need to press enter whereas getch() does not, but if all you're trying to do is compile a simple program to get a start with C, that probably doesn't matter to you.

Upvotes: 7

Related Questions