How to execute and compile a program in Xcode via command line in c and where's the executable?

StackOverflow https://stackoverflow.com/questions/18818958

  •  28-06-2022
  •  | 
  •  

Question

I'm trying to execute via command line a code written in C. I tried gcc -o file file.c, but it did not work. I need to learn how to compile and execute a code using gcc and llvm without graphical interface. Furthermore when I compile the program I cannot find the executable file in Finder (there's no Developer folder in Library).

Thanks in advance.

Was it helpful?

Solution

You can use xcrun tool:

#/usr/bin/xcrun cc -o file file.c

Note: if you have several Xcode versions you can chose with xcode-select and your command above will use compiler and the rest of the tools from the selected SDK.

OTHER TIPS

If file.c is in the Desktop directory: Did you change to that directory beforehand?

Usually Terminal.app starts in the home directory, e.g.: /Users/yourname

To get to the Desktop directory:

cd ~/Desktop

Then check if the source file is there:

ls -l file.c

Then try again to compile:

gcc -o file file.c

Check for any error messages. If no output is given everything is fine and there should be an executable which can be (surprise!) executed:

ls -l file
 ./file
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top