clang++ link failure: error: source file is not valid UTF-8?

clang++ link failure: error: source file is not valid UTF-8?

我遇到了一个有趣的问题。我正在用

编译一个玩具编译器
clang++ -g -x c++ y.tab.c lex.yy.c semantic_actions.cpp -o parser -lfl

y.tab.c中我包含了semantic_actios.hppsemantic_actions.hpp中的内容是一些在y.tab.c中使用的方法声明。编译得很好。

但是,如果我将其更改为

clang++ -c -g -x c++ semantic_actions.cpp -o semantic_actions.o
clang++ -g -x c++ y.tab.c lex.yy.c -o parser semantic_actions.o -lfl

明白了

semantic_actions.o:1:1: error: source file is not valid UTF-8
<CF><FA><ED><FE><U+0007>
^
semantic_actions.o:1:2: error: source file is not valid UTF-8
<CF><FA><ED><FE><U+0007>
    ^
semantic_actions.o:1:3: error: source file is not valid UTF-8
<CF><FA><ED><FE><U+0007>
         ^
semantic_actions.o:1:4: error: source file is not valid UTF-8
<CF><FA><ED><FE><U+0007>
            ^
semantic_actions.o:1:5: error: expected unqualified-id
<CF><FA><ED><FE><U+0007>
                ^
semantic_actions.o:1:6: warning: null character ignored [-Wnull-    character]
<CF><FA><ED><FE><U+0007>
// and the output goes on and on

我一定错过了一些非常基本的东西。我在 Mac OS Yosemite

$ clang++ --version
Apple LLVM version 6.0 (clang-600.0.56) (based on LLVM 3.5svn)
Target: x86_64-apple-darwin14.1.0
Thread model: posix

有人可以看看吗?谢谢!

clang++ -g -x c++ y.tab.c lex.yy.c -o parser semantic_actions.o -lfl

你告诉 Clang 它的所有输入都是 C++ 源 (-x c++),然后你给它一个目标文件 (semantic_actions.o)。 Clang 告诉您 semantic_actions.o 不是 UTF-8 编码的 C++ 源文件。