首页 > 解决方案 > 在 FreeBSD 中,clang 找不到标准 c++ 库头文件

问题描述

我对 c++ 很陌生,但是在 Google 的帮助下,在 Windows/Linux 中编写、编译和运行简单的 c++ 程序就像是一种魅力。

然而,尝试在 FreeBSD 中做同样的事情并不是那么简单。

我目前正在尝试编译以下程序:

#include <iostream>
int main(){
  std::cout << "Hello.\n";
}

cpp -v test.cpp -o test

这导致

FreeBSD clang version 10.0.1 (git@github.com:llvm/llvm-project.git llvmorg-10.0.1-0-gef32c611aa2)
Target: x86_64-unknown-freebsd12.2
Thread model: posix
InstalledDir: /usr/bin
 (in-process)
 "/usr/bin/cpp" -cc1 -triple x86_64-unknown-freebsd12.2 -E -disable-free -disable-llvm-verifier -discard-value-names -main-file-name test.cpp -mrelocation-model static -mthread-model posix -mframe-pointer=all -fno-rounding-math -masm-verbose -mconstructor-aliases -munwind-tables -target-cpu x86-64 -dwarf-column-info -fno-split-dwarf-inlining -debugger-tuning=gdb -v -resource-dir /usr/lib/clang/10.0.1 -internal-isystem /usr/include/c++/v1 -fdeprecated-macro -fdebug-compilation-dir /home/Administrator -ferror-limit 19 -fmessage-length 120 -fgnuc-version=4.2.1 -fobjc-runtime=gnustep -fcxx-exceptions -fexceptions -fdiagnostics-show-option -fcolor-diagnostics -faddrsig -o test -x c++ test.cpp
clang -cc1 version 10.0.1 based upon LLVM 10.0.1 default target x86_64-unknown-freebsd12.2
#include "..." search starts here:
#include <...> search starts here:
 /usr/include/c++/v1
 /usr/lib/clang/10.0.1/include
 /usr/include
End of search list.
test.cpp:1:10: fatal error: 'iostream' file not found
#include <iostream>
         ^~~~~~~~~~
1 error generated.

试图找到 iostream 文件

locate iostrem

没有结果。

仅供参考,操作系统是用于工业 PC 的 TwinCAT/BSD。根据我在谷歌上搜索的结果,我觉得这很相关。

有没有人愿意在正确的方向上推动我解决这个问题?

标签: c++clangfreebsd

解决方案


您正在对 C++ 文件使用 C 预处理器。如果您只想进行预处理,则需要使用带有 -E 标志的 C++ 编译器:

c++ -E -o test test.cpp

删除-E以编译为二进制文件。

另外,我看到您正在使用 Clang 10.x 并注意 Clang 12 显示出不同的行为。当我使用

cpp test.cpp -o test

使用 Clang 12,它确实将其识别为 C++ 并对其进行编译,因此看起来行为在这里发生了变化。尽管如此,这c++是用于 C++ 文件的正确命令。


推荐阅读