12

So I tried to install clang + cmake to compile a simple C++ program and I'm getting the following error:

-- The C compiler identification is GNU 4.8.3
-- The CXX compiler identification is Clang 3.5.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: /usr/local/bin/clang++
-- Check for working CXX compiler: /usr/local/bin/clang++ -- broken
CMake Error at /usr/share/cmake/Modules/CMakeTestCXXCompiler.cmake:54 (message):
  The C++ compiler "/usr/local/bin/clang++" is not able to compile a simple
  test program.

  It fails with the following output:

   Change Dir: /home/jtcwang/tmp/CMake/CMake/CMakeFiles/CMakeTmp



  Run Build Command:/usr/bin/gmake "cmTryCompileExec697180971/fast"

  /usr/bin/gmake -f CMakeFiles/cmTryCompileExec697180971.dir/build.make
  CMakeFiles/cmTryCompileExec697180971.dir/build

  gmake[1]: Entering directory
  `/home/jtcwang/tmp/CMake/CMake/CMakeFiles/CMakeTmp'

  /usr/bin/cmake -E cmake_progress_report
  /home/jtcwang/tmp/CMake/CMake/CMakeFiles/CMakeTmp/CMakeFiles 1

  Building CXX object
  CMakeFiles/cmTryCompileExec697180971.dir/testCXXCompiler.cxx.o

  /usr/local/bin/clang++ -o
  CMakeFiles/cmTryCompileExec697180971.dir/testCXXCompiler.cxx.o -c
  /home/jtcwang/tmp/CMake/CMake/CMakeFiles/CMakeTmp/testCXXCompiler.cxx

  Linking CXX executable cmTryCompileExec697180971

  /usr/bin/cmake -E cmake_link_script
  CMakeFiles/cmTryCompileExec697180971.dir/link.txt --verbose=1

  /usr/local/bin/clang++
  CMakeFiles/cmTryCompileExec697180971.dir/testCXXCompiler.cxx.o -o
  cmTryCompileExec697180971 -rdynamic

  /usr/bin/ld: cannot find -lstdc++

  clang: error: linker command failed with exit code 1 (use -v to see
  invocation)

  gmake[1]: Leaving directory
  `/home/jtcwang/tmp/CMake/CMake/CMakeFiles/CMakeTmp'

  gmake[1]: *** [cmTryCompileExec697180971] Error 1

  gmake: *** [cmTryCompileExec697180971/fast] Error 2

It's not even compiling my program because it fails to compile a test program.

Looks like the important line is here:

 /usr/bin/ld: cannot find -lstdc++

However, I have checked that libstdc++ is installed and up to date, so at this point I'm quite lost.

Other things I've tried:

  1. Using prebuilt binaries instead of sudo yum install clang
  2. remove and reinstall
  3. Tried clang++ hello.cpp (hello world program). It says <iostreams> is not found. Is clang missing a standard library? EDIT: changing to <iostream> gives me the same linker error above.

I'm not familiar with the clang, cmake and C++ scene in general, so I'd appreciate any pointers. Thanks!

Jacob Wang
  • 4,411
  • 5
  • 29
  • 43
  • 1
    There is no ``. Use ``. – HolyBlackCat Oct 15 '14 at 10:39
  • @nos, I tried both (distro and prebuilt binary) but neither of them worked. – Jacob Wang Oct 15 '14 at 18:07
  • @HolyBlackCat, cool, now I get `/usr/bin/ld: cannot find -lstdc++` which is similar to the linker error when I try to run cmake. I tried `clang++ hello.cpp`, what other flags should I use? – Jacob Wang Oct 15 '14 at 18:09
  • I have no idea, sorry. – HolyBlackCat Oct 15 '14 at 18:23
  • The fact that you can't manually build a hello world program indicates that you're dealing with a compiler issue, and not a CMake issue. If your compiler isn't set up properly, then CMake can't help you with that. – jmstoker Oct 15 '14 at 22:47
  • Could You please show relevant piece of `CMakeLists.txt` of Yours? Also You should point a `CMAKE_LINKER` to `llvm-ld` and other `CMake` core vars related to linkage. Btw, what's the version of CMake? – Kamiccolo Oct 16 '14 at 16:30

2 Answers2

6

You need the development libraries and headers for C++ library, try

yum install libstdc++-devel
ismail
  • 46,010
  • 9
  • 86
  • 95
  • The error is a linker error, the headers were found, but the linker failed to find the binaries. – David Rodríguez - dribeas Oct 15 '14 at 13:23
  • @DavidRodríguez-dribeas this package also contains the *.so files which are needed for linking. – ismail Oct 15 '14 at 14:03
  • The .so should be there without the devel packages, otherwise you would not be able to run the application in any machine that does not have the devel packages installed. Considering that this is the core c++ library, you could not run basically any programs built with C++ in your linux box, unless every program statically linked the standard library (which is not the case) – David Rodríguez - dribeas Oct 15 '14 at 16:21
  • 1
    Seems like I already have this installed and up to date...so unfortunately this isn't the solution. TBH, I feel like I already have the `.so` files, I just need to point to them properly. – Jacob Wang Oct 15 '14 at 18:07
  • 1
    @DavidRodríguez-dribeas *.so and *.so.x.y are different, only the latter is needed for runtime. – ismail Oct 16 '14 at 04:28
  • Similar solution for Ubuntu: `sudo apt install libstdc++-12-dev` (your version may differ, I used just used the latest on my system) – Victor Aug 22 '23 at 07:05
-2

Your /home/gnu/bin/c++ seem to require additional flag to link things properly and CMake doesn't know about that.

To use /usr/bin/c++ as your compiler run cmake with -DCMAKE_CXX_COMPILER=/usr/bin/c++.

Also, CMAKE_PREFIX_PATH variable sets destination dir where your project' files should be installed. It has nothing to do with CMake installation prefix and CMake itself already know this.