This guide shows you how to locate the full path to the g++ compiler on Linux, macOS, and Windows. You’ll need this path when configuring IDEs, build scripts, Makefiles, or when you want to invoke g++ directly without relying on your shell’s PATH environment variable.
What You’ll Need
- A computer with g++ installed (if not installed, see Troubleshooting below).
- Access to a terminal or command prompt.
- Basic familiarity with typing commands.
Step-by-Step
- Open a terminal (Linux/macOS) or Command Prompt/PowerShell (Windows).
- Check if g++ is in your PATH by typing
which g++(Linux/macOS) orwhere g++(Windows). This returns the first location found in your PATH. If you get no output or an error, g++ is not in your PATH. - If the above fails, try common default locations:
Linux:/usr/bin/g++,/usr/local/bin/g++
macOS (Homebrew):/opt/homebrew/bin/g++(Apple Silicon) or/usr/local/bin/g++(Intel)
Windows (MinGW):C:\MinGW\bin\g++.exeorC:\msys64\mingw64\bin\g++.exe(MSYS2). - Use the compiler itself to report its location. Run
g++ -print-prog-name=cc1plus– this prints the path to the internal compiler component, which is usually in the same directory as g++. For example, if it prints/usr/lib/gcc/x86_64-linux-gnu/11/cc1plus, then g++ is likely at/usr/bin/g++or/usr/bin/x86_64-linux-gnu-g++-11. - Search the filesystem if the above still doesn’t work. On Linux/macOS, use
find / -name g++ -type f 2>/dev/null(this may take a while). On Windows, usedir /s /b C:\g++.exefrom the root of the drive where you think it’s installed. - For IDE configuration, once you have the full path, copy it exactly. For example, in VS Code’s
c_cpp_properties.jsonor in Eclipse’s toolchain settings, paste the full path as the compiler path. - Verify the path works by running
/full/path/to/g++ --version. You should see the version information.
Common Mistakes
- Using
whichon Windows –whichis not a standard Windows command; usewhereinstead. - Forgetting that PATH may have multiple g++ entries –
whichonly shows the first one. Usetype -a g++(Linux/macOS) to list all. - Assuming g++ is in /usr/bin – on many systems it’s a symlink to a versioned executable elsewhere. The path you find with
whichis fine for most purposes. - Copying the path with a trailing newline – when copying from terminal output, ensure you don’t include extra whitespace.
- On Windows, using the wrong slash direction – Windows accepts both
\and/, but some tools require backslashes. Use the path exactly as returned bywhere.
Tips
- If you need the path for a build script, use
$(which g++)in shell scripts to automatically get the path at runtime. - On Linux, you can also check
g++ -v– the last line shows the directory containing the compiler’s own libraries, which is often the same as the executable’s directory. - If you have multiple versions installed, you can specify the version in the command, e.g.,
g++-12, and then find its path withwhich g++-12. - For cross-compilers, the path will include the target triple, e.g.,
/usr/bin/aarch64-linux-gnu-g++. - Consider adding the directory to your PATH if you need to use g++ frequently from different terminals.
Troubleshooting
g++ not found at all – Install it first. On Debian/Ubuntu: sudo apt install g++. On Fedora: sudo dnf install gcc-c++. On macOS: install Xcode Command Line Tools with xcode-select --install, or install Homebrew and then brew install gcc. On Windows: install MinGW-w64 or MSYS2 and add its bin directory to your PATH.
Permission denied when running the path – Ensure the executable has execute permissions. On Linux/macOS, run chmod +x /path/to/g++ if needed.
The path you find is a symlink – To see the real target, use readlink -f $(which g++) on Linux/macOS. The symlink path usually works fine, but if you need the actual binary, use the resolved path.
Windows: where returns nothing – g++ might be installed but not in PATH. Look in common installation directories like C:\MinGW, C:\msys64, or C:\Program Files\mingw-w64. If you still can’t find it, reinstall and make sure to check the option to add to PATH.
Multiple g++ installations – Use type -a g++ (Linux/macOS) to list all. Choose the one that matches the version you need. If you’re unsure, run g++ --version for each path to see which is which.
Frequently Asked Questions
What is the default g++ path on Ubuntu?
Typically /usr/bin/g++, which is a symlink to a versioned binary like /usr/bin/g++-11.
How do I find the g++ path on macOS?
If you have Xcode Command Line Tools, it’s usually /usr/bin/g++. If you installed via Homebrew, it’s /opt/homebrew/bin/g++ (Apple Silicon) or /usr/local/bin/g++ (Intel).
Why does which g++ not work on Windows?
which is a Unix command. On Windows, use where g++ in Command Prompt or PowerShell.
Can I use the g++ path in a Makefile?
Yes, you can set CXX = /full/path/to/g++ in your Makefile to use that specific compiler.
How do I set the g++ path permanently on Linux?
Add the directory containing g++ to your PATH environment variable in your shell profile (e.g., export PATH="/path/to/g++/dir:$PATH" in ~/.bashrc).
What if I have multiple g++ versions? How do I choose?
Use the update-alternatives command on Debian/Ubuntu to set the default, or call the specific version directly, e.g., g++-12.
Related
General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.
More in this section
- ug to ppb Calculator: Convert Micrograms per Liter1.00 ppb
- Grams to Moles Chemistry: How to Convert0.2000 mol/L
- Convert ug/l to ppb Calculator
- CPP Calculator Ontario: How CPP Is Calculated162.12 $/month
- Grams to Moles to Molecules: How to Convert0.2000 mol/L
- Organic Chemistry Reaction Calculator: How It Works