【问题标题】:CMake "clang++ is not able compile a simple test program" (Fedora 20)CMake“clang++ 无法编译简单的测试程序”(Fedora 20)
【发布时间】:2014-12-10 09:41:24
【问题描述】:

所以我尝试安装 clang + cmake 来编译一个简单的 C++ 程序,我得到了以下错误:

-- 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

它甚至没有编译我的程序,因为它无法编译测试程序。

看起来重要的一行在这里:

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

但是,我检查了libstdc++ 是否已安装并且是最新的,所以此时我很迷茫。

我尝试过的其他事情:

  1. 使用预构建的二进制文件而不是 sudo yum install clang
  2. 删除并重新安装
  3. 试过clang++ hello.cpp(你好世界程序)。它说<iostreams> 未找到。 clang 是否缺少标准库?编辑:更改为 <iostream> 给了我上面相同的链接器错误。

我一般不熟悉 clang、cmake 和 C++ 场景,所以我会很感激任何指点。谢谢!

【问题讨论】:

  • 没有<iostreams>。使用<iostream>
  • @nos,我都尝试了(发行版和预构建的二进制文件),但它们都不起作用。
  • @HolyBlackCat,很酷,现在我得到了/usr/bin/ld: cannot find -lstdc++,这类似于我尝试运行 cmake 时出现的链接器错误。我试过clang++ hello.cpp,我应该使用哪些其他标志?
  • 我不知道,对不起。
  • 您无法手动构建 hello world 程序这一事实表明您正在处理编译器问题,而不是 CMake 问题。如果您的编译器设置不正确,那么 CMake 将无法帮助您。

标签: c++ cmake clang fedora


【解决方案1】:

你需要C++库的开发库和头文件,试试

yum install libstdc++-devel

【讨论】:

  • 错误是链接器错误,找到了标头,但链接器找不到二进制文件。
  • @DavidRodríguez-dribeas 这个包还包含链接所需的 *.so 文件。
  • .so 应该在没有开发包的情况下存在,否则您将无法在任何未安装开发包的机器上运行应用程序。考虑到这是核心 c++ 库,你基本上不能在你的 linux 机器上运行任何用 C++ 构建的程序,除非每个程序都静态链接标准库(情况并非如此)
  • 好像我已经安装了这个并且是最新的......所以不幸的是这不是解决方案。 TBH,我觉得我已经有了.so 文件,我只需要正确指向它们。
  • @DavidRodríguez-dribeas *.so 和 *.so.x.y 是不同的,只有后者是运行时需要的。
【解决方案2】:

您的 /home/gnu/bin/c++ 似乎需要额外的标志才能正确链接,而 CMake 对此一无所知。

要将 /usr/bin/c++ 用作编译器,请使用 -DCMAKE_CXX_COMPILER=/usr/bin/c++ 运行 cmake。

此外,CMAKE_PREFIX_PATH 变量设置了项目文件应安装的目标目录。它与 CMake 安装前缀无关,CMake 本身已经知道这一点。

【讨论】:

  • 它已经在使用clang++ 进行编译。这是不正确的。
猜你喜欢
  • 2015-08-13
  • 1970-01-01
  • 2021-03-18
  • 1970-01-01
  • 2023-04-08
  • 1970-01-01
  • 2017-11-16
  • 2013-01-13
  • 1970-01-01
相关资源
最近更新 更多