• C

How do I compile a simple C program using Terminal on a mac?

I used Filezilla to upload a file called main.c to a server that has the gcc 4.4.7 package installed on it. My teacher gave the following instructions in bold below. But I can't seem to run the main.c program without getting several "No such file or directory" messages. For example, I tried typing "gcc main.c –o main.out" and it did not work. I just want to be able to view the output of a "hello world" program in Terminal.

Basic syntax of a compile: gcc programname.c –o nameofexecutable.out
Execute the program by entering nameofexecutable.out from the command-line
shampouyaAsked:
Who is Participating?
 
jmcgOwnerCommented:
I just tried this below and nothing happened

gcc main.c -o main.out
main.out

If nothing happened, i.e. no warning messages, then things are pretty close to working. The gcc command is installed and available in your shell's PATH and it appears to have produced a "main.out" file or else that line would have caused a message as well.

If the main.c that you compiled contained syntactically correct code that just doesn't happen to do anything visible, this is exactly what you would see. Try again with a HelloWorld.c program:

#include <stdio.h>
 
int main()
{
    printf("Hello, world!\n");
}

Open in new window


[For more examples in other languages, see the List of Hello world program examples Wikipedia article.]
0
 
ozoCommented:
"gcc main.c o main.out"
used a character.  It should be a - character
the command should be
gcc main.c -o main.out
instead of
gcc main.c –o main.out
0
 
shampouyaAuthor Commented:
I just tried this below and nothing happened

gcc main.c -o main.out
main.out
0
Network Scalability - Handle Complex Environments

Monitor your entire network from a single platform. Free 30 Day Trial Now!

 
ozoCommented:
What was in main.c?
0
 
Dave BaldwinFixer of ProblemsCommented:
Apple Macs do not come with 'gcc' installed, you have to do it yourself.  Here's an article that describes a number of the issues with that: https://blog.udemy.com/c-compiler-for-mac/
0
 
shampouyaAuthor Commented:
Thank you so much, very valuable insight! I think I was missing my main method. Once I added that, I could see the output in terminal.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.