Compiling and running a code written from Sublime Text in Mac's Terminal

So the title is basically it. But first off, can I in fact run/compile a code I wrote in Sublime Text in my macs terminal? I typed the code up and saved it as resistance.cpp (its a program that calculates resistance of two resistors placed in parallel and series based off what values the user inputs as the two resistances), it saves as a cpp file on my desktop.

I have Xcode, but I don't like it, and I read somewhere to install Command Line Tools from its preferences menu in order to run in the terminal but it doesn't work.

Any ideas? Can it be done? How so? Been at this for like a day, sorry if i seem anxious to get it to work/find out
You sure can.
You have two options for a compiler with the command line tools from Apple; clang or gcc. It comes down to a matter of preference, really. They both do the same thing.

So, to compile your cpp file, you will first need to have the terminal in the same working directory as the file. Then enter the following command:
g++ -o ProgramName resistance.cpp

Some additional flags for g++ or clang++ are:
-L<library search path> - adds an additional search path for libraries
-I<header search path> - adds an additional header search path
-l<library name> - links a library to your project
-D<preprocessor definition> - (such as _DEBUG)
-framework <framework name> - Links a framework in OS x.
-Wall - outputs all compiler warnings

in g++ if you plan on using C++11 features, you must use the following flag:

in clang++ you must use:
-std=c++11 -stdlib=libc++

Also, the version of gcc that apple provides is gcc 4.2, which is outdated and doesn't have C++11 support (afaik). Apple has only 'approved' this version for use, because they tailor the release to their products. However, you can install the most up to date version of gcc or clang (that supports C++11) with some software called macports, which is akin to linux's apt-get.
This link details that process.

EDIT: (spelling)
Last edited on
This worked marvelously, thank you!
Glad to help!
Topic archived. No new replies allowed.