21

I'm trying to compile a Git project, and I'm facing some problems with CMake. Initially, it didn't find the C++ compiler and prompted an error:

cmake ..

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.

So, I did:

CXX="gcc" cmake ..

But another error was prompted:

-- The CXX compiler identification is unknown
-- Check for working CXX compiler: /usr/bin/gcc
-- Check for working CXX compiler: /usr/bin/gcc -- broken
CMake Error at /usr/share/cmake-3.0/Modules/CMakeTestCXXCompiler.cmake:54 (message):
The C++ compiler "/usr/bin/gcc" is not able to compile a simple test program.

How can I solve this error and compile the project?

Tsyvarev
  • 60,011
  • 17
  • 110
  • 153
ptkato
  • 668
  • 1
  • 7
  • 14
  • Are you trying to use CMake on a Linux or Mac OS system? CMake should be able to detect the default compiler on your machine automatically. What does `g++ -v` command returns in a terminal? – Antwane Jul 15 '15 at 07:11
  • Linux system. About the `g++ -v`, it throws a "command not found". – ptkato Jul 15 '15 at 17:20

2 Answers2

50

You should try installing build-essential if you haven't done so.

Try this

sudo apt-get update
sudo apt-get install -y build-essential
Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
thiagoh
  • 7,098
  • 8
  • 51
  • 77
  • 4
    This resolved the issue for me. But why? What does the `build-essential` package do? – Dan Schnau Jan 18 '18 at 20:54
  • 3
    I ended up doing: `sudo apt purge gcc-7 gcc-8 gcc-9 gcc-10 gcc c-compiler && sudo apt autoremove && sudo apt install gcc-9 g++-9 gcc g++ build-essential` and that fixed my issue (which was `No CMAKE_CXX_COMPILER could be found. Tell CMake where,compiler name if it is in the PATH.`) – Roel Van de Paar Oct 26 '20 at 09:20
  • 1
    this works, but it's a bad answer because it doesn't explain why it works – endolith Dec 18 '22 at 13:14
  • build-essential worked for me also. Looks like build-essential is a package that contains a list of required Debian packages as dependencies, so when you install build-essential, you install all those packages in one single command. – Shaun Jul 29 '23 at 14:46
22

You are trying to use C compiler gcc as C++ one, which is wrong.

You need to install g++ or other C++ compiler.

Tsyvarev
  • 60,011
  • 17
  • 110
  • 153
  • Really? I read [this answer](http://stackoverflow.com/a/173007/3029543) and, in my understanding, it says that the gcc can compile both C and C++ programs. – ptkato Jul 15 '15 at 17:26
  • 2
    It can, but it requires at least stdc++ library, which comes with g++. Just install g++, and everything will work without intervention like setting CXX variable. – Tsyvarev Jul 15 '15 at 18:58