Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 7 Next »

Need to try something out quickly in C? Then it’s helpful to know how to compile a one file C program as a way to make yourself a C sandbox.

First make sure you have a C++ compiler installed.

Create a text file with a text editor with something like:

#include <stdio.h>

int main(){
   printf("Hello World!\n");
   return 0;
}

Save the file into something like main.c. Then compile it at the command prompt using:

c++ -o test main.c
./test

This will compile the input file called main.c into a binary file called test. Then ./test is what is used to run the program.

On my machine this is what I see:

Eliots-MacBook-Pro-2:example eliotmuir$ vi main.c
Eliots-MacBook-Pro-2:example eliotmuir$ c++ -o test main.c
Eliots-MacBook-Pro-2:example eliotmuir$ ./test
Hello World!
Eliots-MacBook-Pro-2:example eliotmuir$ 

  • No labels