Uploaded by balce.angelaanne

Running a C Program in Mac 26ce5616dbe128a5acc8955f634abbe7

advertisement
Running a C Program in Mac OS
Supplemental Material
1. C Compiler
Install Xcode from the App Store. Xcode comes with the C compiler that is required for you to be able to
compile and run a C program.
2. Code Editor
You can use Xcode as your code editor but many of my previous students recommend using Visual
Studio Code (https://code.visualstudio.com/download) as it has a cool interface – even cooler than
Code::Blocks!
Visual Studio Code Interface
3. Run your Terminal
The Terminal is the command prompt’s counterpart for Mac. We will use this to compile and run our
programs instead of Xcode as running C programs in Xcode is too troublesome.
Icon for Terminal in Mac
4. Locate your Working Folder
After opening the terminal, find the caret (the blinking chorva indicating that you can type some text
there). The terminal is a command-line interface so we can do stuff by just inputting commands here. To
browse through the folders in your computer, we can use the command cd in the terminal.
In your file manager (Finder ‘ata ang tawag?), copy the path of the folder containing your C programs. My
tip is to place your programs inside a folder in your Desktop so you can access that folder easily in the
terminal. Once you have, copied the path, input the following command:
CE 25 – MATHEMATICAL METHODS IN CIVIL ENGINEERING II
Running a C Program in Mac OS
Supplemental Material
cd path_here <press enter>
For example,
cd Desktop/CE-25-lang-malakas/Cluster-1 <press enter>
After entering the command in the example above, the terminal is now looking inside the folder Cluster1 which is inside another folder CE-25-lang-malakas which is another folder in Desktop. Just to be sure,
do not use spaces in the names of your folders and files. I do not have a Mac so I cannot confirm this,
but it seems that the terminal cannot access folders and files if they have spaces. In this case, use dashes
or underscores to fill in spaces between words.
5. Compiling your C program
Now that the terminal is looking inside the folder containing your C program, we can use the following
command to compile (to understand this process, refer to Module 1A – Build Process) your source code
(.c file):
gcc your_program.c -o exe_filename -w -Wall <press enter>
For example,
gcc MX-1_Musikulot.c -o lezzgo -w -Wall <press enter>
After entering the command above, the C compiler will build MX-1_Musikulot.c and create an executable
file (.exe) named lezzgo.exe. Inside the Cluster-1 folder, we should see this new lezzgo.exe file.
6. Running the executable file
Option 1: Go to your working folder and run (by double-clicking) the executable file. The program interface
in using this option is cleaner than that of Option 2.
Option 2: We can run the .exe file using the Terminal by entering the command:
./exe_filename <press enter>
For example,
./lezzgo <press enter>
Entering the command above will run your program inside the Terminal. Using this option can make your
terminal messy, but it is more convenient as you do not leave the Terminal window instead of switching
windows everytime you are running a program. Also, pressing the up and down arrow keys in the Terminal
copies your previous commands onto the current command-line, saving a lot of typing time. If you want
to clear the stuff in the Terminal window, you can just use the command clear.
CE 25 – MATHEMATICAL METHODS IN CIVIL ENGINEERING II
Download