CMAKE_CXX_COMPILER could be found in Ubuntu

If you are seeing an error message that says “CMAKE_CXX_COMPILER could not be found” when trying to build a C++ project on Ubuntu 22.04, it means that the C++ compiler is not installed or is not in the system’s PATH environment variable.

CMake Error at CMakeLists.txt:3 (project):
  No CMAKE_CXX_COMPILER could be found.
 
  Tell CMake where to find the compiler by setting either the environment
  variable "CXX" or the CMake cache entry CMAKE_CXX_COMPILER to the full path
  to the compiler, or to the compiler name if it is in the PATH.

The The “compiler” is a separate package that needs to be installed in your machine, g++ and build-essential.

$ sudo apt-get install g++
$ sudo apt-get install build-essential

This will install the GNU C++ compiler (g++) along with any necessary dependencies. Once the installation is complete, you should be able to run the C++ compiler and the “whereis g++” command should show its location.

If the compiler is already installed and you’re still getting the error, it could be that the C++ compiler binary is not in your system’s PATH environment variable. You can check if the C++ compiler binary is in your system’s PATH by running the following command:

$ echo $PATH

This will display a list of directories separated by colons. Check if the directory that contains the C++ compiler binary (usually “/usr/bin”) is in the list. If it’s not, you can add it to the PATH environment variable by editing the “~/.bashrc” file and adding the following line at the end:

$ export PATH=$PATH:/usr/bin

Save the file and run the following command to apply the changes:

$ source ~/.bashrc
Related Post