Run Your First C++ Program in CLion
Build and run a C++ greeting in CLion and the macOS Terminal.
Introduction
30 Second Summary
The confusing part often begins after you press Run. Several hidden steps turn text into a program your Mac can execute.
In this project, you will build a small C++ greeting with CLion's CMake workflow. You will expose the hidden compilation step by building the same message directly with Apple Clang in CLion's Terminal.
What You'll Build
You'll watch your personalized greeting appear in CLion's Run tool window before producing the same result from a manually compiled executable.
By the end of this project, you'll have:
- A working C++ greeting displayed in CLion's Run tool window.
- A personalized output that proves your source edit reaches the next build.
- The ability to explain the build path from main.cpp through CMake to an executable.
- Secret Mission: Create hello.cpp. Compile it with Apple Clang. Launch the resulting executable without CLion's Run button.
Before We Start
Before the hands-on work begins, this is your chance to commit to the greeting you are building and why both workflows matter. You will run it through CLion's CMake workflow and compile it directly from the Terminal.
Install and Connect the Mac C++ Toolchain
Your local C++ greeting needs development tools before CLion can turn source code into a program. At the moment, CLion has no macOS compiler toolchain to call.
Apple Command Line Tools for Xcode provide Apple Clang as the compiler. This step installs the tools before connecting them to CLion.
In this step, get ready to:
- Confirm that macOS has no active developer directory.
- Install Apple Command Line Tools for Xcode.
- Connect Apple Clang to CLion.
Check the missing developer directory
- Check the active developer directory by running this command:
xcode-select --print-path
What does this check?
The xcode-select --print-path command asks macOS for its active developer directory. A valid path shows where command-line developer tools are currently located.
You will see xcode-select: error: Unable to get active developer directory. because no toolchain is active. This is the intended baseline for the installation.
Install and activate the developer tools
The standalone Command Line Tools package adds the compilers that CLion needs. macOS provides its installer through xcode-select.
- Start the Command Line Tools installer by running this command:
xcode-select --install
What does this command do?
The xcode-select --install command asks macOS to download its standalone developer tools. The package includes Apple Clang for compiling C and C++ programs.
Before you repeat the directory check, which installation path do you expect macOS to return?
- Check the active developer directory again by running this command:
xcode-select --print-path
What should you see?
You should see /Library/Developer/CommandLineTools. This path confirms that the standalone Command Line Tools package is active.
A different path means the installed package still needs to be selected system-wide. This change requires administrator approval from your Mac account.
- Activate the standalone package only if your displayed path differs by running this command:
sudo xcode-select --switch /Library/Developer/CommandLineTools
What does this selection change?
The command sets /Library/Developer/CommandLineTools as the system-wide developer directory. Future compiler commands use tools from this location.
🙋♀️ Still seeing the wrong path?
Repeat the directory check after the selection command finishes. Confirm that you entered the password for your Mac account when prompted.
Ask for help with the active developer directory.
Verify Apple Clang in CLion
The active path proves that macOS can find its developer tools. The compiler version confirms that Apple Clang can respond before CLion tries to detect it.
- Verify the compiler by running this command:
clang --version
Check the compiler response
You should see an Apple Clang version in the output. This confirms that the compiler is available from macOS Terminal.
- Press Command-Space bar to open Spotlight.
- Type CLion into Spotlight.
- Press Return to open CLion.
- Select Settings from the application menu.
- Select Build, Execution, Deployment in the Settings sidebar.
- Select Toolchains.
- Select the default macOS toolchain in the left pane.
- Confirm that the C Compiler and C++ Compiler fields are populated.
Both compiler fields show detected Apple Clang entries. CLion's default macOS toolchain is now configured to use the installed compilers.
Are the compiler fields empty?
- Return to the macOS Terminal window from earlier.
- Repeat verification command.
- Apply the developer directory selection command above if the path differs.
- Restart CLion after both Terminal checks succeed.
Ask for help with CLion toolchain detection.
- Capture the Toolchains screen with the populated compiler fields visible.
That is the foundation in place. Next up, you will create the hello CMake project and watch it run inside CLion.
Create and Run the Hello Project
Your Apple Clang toolchain is connected in the CLion session from earlier. The next proof is a real C++ program that builds inside CLion.
A visible first run proves the full build path works. It confirms that the compiler reaches a CMake target through a run configuration.
In this step, get ready to:
- Create a C++20 executable project named hello.
- Replace main.cpp with a C++ greeting.
- Build the hello target from the editor.
Create the CMake project
A CMake target defines the executable that CLion builds. The new project wizard creates that target with its starter files.
- Click File in CLion's top menu.
- Select New Project.
You'll see project template choices in the new project wizard.
- Select C++ Executable from the template list.
You'll see settings for the project name and language standard.
- Name the project hello.
- Select C++20 as the language standard.
The wizard is now set to create a C++ executable named hello.
- Click Create.
CLion opens the hello project. CMake loading begins automatically.
Write the greeting source
The CMakeLists.txt file defines the hello target. The main.cpp file supplies the source code for that target.
- Wait for the CMake project load to finish.
- Confirm the Project tree contains main.cpp.
- Confirm the project root contains CMakeLists.txt.
That confirms the scaffold is complete. The target definition sits beside its source file.
- Double-click main.cpp in the Project tree.
The editor shows CLion's generated starter source.
- Select all the starter code in main.cpp.
- Fill the selected area by pasting this code:
#include <iostream>
int main() {
std::cout << "Hello World!" << '\n';
}
What does this code do?
- The #include <iostream> line makes the standard output tools available.
- The main() function marks where the program starts.
- The std::cout object sends the greeting to the program output.
- The '\n' character ends the output with a new line.
- Save main.cpp.
- Confirm std::cout prints Hello World!.
- Confirm '\n' follows the greeting.
Seeing Source Errors?
- Select all the generated starter source before pasting if the editor contains more than one main() function.
- Compare every bracket and quote with the full-file reference below.
Help me find the problem in my C++ greeting source.
✔️ Awesome, I've got everything!
Your saved main.cpp now matches the source required by the hello target.
ⓧ I'd like to double check the full code
#include <iostream>
int main() {
std::cout << "Hello Cruel World!" << '\n';
}
Run the hello target
CLion's run configuration links the hello target to a build step. Choosing Run builds the target before launching it.
Before you run it, what greeting do you expect the Run tool window to show?
- Click the gutter action beside the declaration of main().
- Select Run.
CLion builds the hello target first. You'll then see Hello World! in the Run tool window.
Greeting Missing from the Run Output?
- Wait for CMake loading to finish if Run is unavailable.
- Save main.cpp if the Run tool window still shows the generated starter output.
Help me troubleshoot why my CLion project does not print the greeting.
That first run is working. The Run tool window proves that your source reaches a working executable.
Next, you'll personalize the greeting. A second run will test the edit-build-run loop.
Personalize the Program and Run It Again
The first greeting proved that your C++ source can travel through CLion's CMake target to a running program. You now have a working baseline for testing a real source edit.
A personalized message gives you a clear before-and-after result. This run also exposes how much of the build process CLion handles through a single click.
In this step, get ready to:
- Replace the original greeting with a personalized message.
- Run the hello configuration from CLion's main toolbar.
- Identify which build work CLion handles behind the Run button.
Personalize the greeting
The std::cout expression already sends text to standard output. You only need to change the message inside its quotation marks.
- In main.cpp, find this line:
std::cout << "Hello World!" << '\n';
What are you changing?
The current string literal controls the visible greeting. The std::cout output pattern already works.
- Replace the line with this personalized version:
std::cout << "Hello from my Mac!" << '\n';
What does this change do?
Only the string literal changes. The std::cout expression still ends with '\n'.
Each greeting therefore finishes on its own line.
- Save main.cpp.
✔️ Awesome, I've got everything!
Your saved main.cpp now contains the personalized greeting.
ⓧ I'd like to double check the full code
#include <iostream>
int main() {
std::cout << "Hello from my M4 Mac!" << '\n';
}
How does the full file fit together?
This is the complete source state for this step. It preserves the existing program structure.
The greeting text is the only change.
Run the personalized program
A run configuration connects a CMake target to an executable launch. The existing hello configuration already uses Build before launch.
- In CLion's main toolbar, select hello from the run configuration menu.
Before you run it, do you expect CLion to use the saved greeting or the earlier build?
- Click Run in the main toolbar.
CLion rebuilds the hello target before launch. In the Run tool window, you'll see Hello from my M4 Mac!.
You have closed the edit-build-run loop. Your saved source now reaches the personalized executable through one click.
Still seeing the earlier greeting?
- Save main.cpp if the editor tab still shows an unsaved-change marker.
- Select hello in the main toolbar if a different run configuration is active.
Help me diagnose why CLion still prints the earlier greeting.
Compare the two runs
The two outputs differ in one visible place: the greeting text. That comparison separates the code you changed from the work the IDE automated.
- Compare Hello World! from the first run with Hello from my Mac! in the current Run tool window.
- Confirm that the greeting text is the only visible output change.
What did the Run button handle?
CLion's run configuration performs its Build step before launch. This turns saved source into the program you see running.
You reached personalized output without typing an Apple Clang compiler command. The source-to-executable command remains hidden behind Run.
The current Run output is your final proof that the edit reached the executable.
- Return to the Run tool window from earlier.
You should still see Hello from my Mac!. The original greeting no longer appears in the current run.
Secret mission
Compile and Run from CLion's Terminal
Create a separate hello.cpp file outside the CMake target. Compile it with Apple Clang in CLion's embedded Terminal. Launch the executable to print your personalized greeting without using the Run button.
Clean Up Your Resources
Clean Up Your Resources
Your hello project stays entirely on your Mac. It has no ongoing cloud or service costs.
Resources you used:
- Local files: The hello project folder containing main.cpp, hello.cpp, CMakeLists.txt, CMake build output, and the manually compiled hello executable.
Keep everything running
No action is needed. This option fits active work in CLion.
- Keep the hello project folder on your Mac.
- Continue using the hello run configuration for future experiments.
- Keep Apple Command Line Tools installed for future C++ projects.
Pause - I'll come back to this later
Closing CLion stops your current session. The local project remains ready for your next session.
- Close CLion.
- Leave the hello project folder in its current location.
Delete - I don't want to use this again
Deleting the project folder removes both versions of your source code. It also removes the generated build output and manual executable.
This cleanup is final once you empty Trash. Your Apple Command Line Tools stay installed for future development.
- Use Finder to locate the hello project folder.
- Close CLion.
- Move the hello project folder to Trash.
- Keep Apple Command Line Tools installed.
- Check the original project location in Finder.
You should no longer see the hello folder in its original location. Its source files and generated artifacts now sit in Trash.
Nice Work!
Nice Work!
You did it! Your personalized C++ greeting now runs through CLion's CMake workflow. You also exposed the hidden build step by compiling a separate source file with Apple Clang in CLion's embedded Terminal.
You've learned how to:
- Installed Apple Command Line Tools on macOS. Confirmed that CLion detected Apple Clang as its default compiler toolchain.
- Created a C++ executable project named hello with the language standard set to C++20. Ran main.cpp through the hello configuration to see Hello Cruel World! in the Run tool window.
- Changed the personalized greeting in main.cpp to Hello from my M4 Mac!. Confirmed that CLion rebuilt the target before showing the new message.
- Secret Mission: Created hello.cpp outside the CMake target. Used manual compilation to produce the local hello executable. Ran that executable in CLion's embedded Terminal to print Hello from my M4 Mac!.
Ready to quiz yourself?