cpp文件没有在clang ++和g ++中编译

时间:2017-09-28 13:28:50

标签: c++ c++11 encoding utf-8 clang

我已经在C ++上工作了几年,并且已经多次编译了这些东西,但是以下问题对我来说是全新的,它只是没有意义。

以下是我要遵循的步骤:

  • 使用带有g ++版本的cygwin设置:6.4.0和clang ++版本:4.0.1
  • 使用sublime文本创建了一个新的cpp fie,添加了简单的cout并编译了
    使用命令:clang ++ -g -Wall -std = c ++ 14 thread1.cpp -o thread,工作正常。
  • 添加新内容可能是另一个cout,这次是在编译时我 收到大量错误,说明它不是utf-8文件。
  • 在sublime文本中使用utf-8编码保存文件,并尝试过 使用utf-8 BOM编码,仍然相同而不是utf-8文件错误。
  • 在cygwin中运行文件命令以检查文件编码,文件-i thread1.cpp,输出为thread1.cpp:text / x-c;字符集= UTF-8。

这里有什么可能出错的指针?

以下是编译的代码:

#include "iostream"
#include "thread"
#include "mutex"
using namespace std;

class threadFunctor{
        public:

};
int main(int argc , char** argv){
   cout << "Hello";
   return 0;
}

以下代码给出错误:

#include "iostream"
#include "thread"
#include "mutex"
using namespace std;

class threadFunctor{
        public:
};
int main(int argc , char** argv){
   cout << "Hello World";
   return 0;
}

以下是生成错误的片段:

./thread:3:29: error: source file is not valid UTF-8
$<U+0000><U+0000><U+0000><U+0000><U+0000><U+0000><U+0000>PE<U+0000><U+0000>d<86><U+0014><U+0000><87><F5><CC>Y<U+0000><U+0014><U+0001><U+0000><A9><U+0002><U+0000><U+0000><F0><U+0000>'...
                                                                                                                                                                         ^
./thread:3:30: warning: null character ignored [-Wnull-character]
$<U+0000><U+0000><U+0000><U+0000><U+0000><U+0000><U+0000>PE<U+0000><U+0000>d<86><U+0014><U+0000><87><F5><CC>Y<U+0000><U+0014><U+0001><U+0000><A9><U+0002><U+0000><U+0000><F0><U+0000>'...
                                                                                                                                                                             ^
./thread:3:31: warning: missing terminating ' character [-Winvalid-pp-token]
$<U+0000><U+0000><U+0000><U+0000><U+0000><U+0000><U+0000>PE<U+0000><U+0000>d<86><U+0014><U+0000><87><F5><CC>Y<U+0000><U+0014><U+0001><U+0000><A9><U+0002><U+0000><U+0000><F0><U+0000>'...
                                                                                                                                                                                     ^
./thread:4:3: warning: null character ignored [-Wnull-character]
5<U+0001><U+0000><U+0000><E8><B0><U+0001><U+0000><U+0000>E1<C0>1<D2>1<C9><E8><C4><U+0001><U+0000><U+0000>E1<C0>1<D2>1<C9><E8><C8><U+0001><U+0000><U+0000>E1<C0>1<D2>1<C9><E8><CC>...
         ^
./thread:4:4: warning: null character ignored [-Wnull-character]
5<U+0001><U+0000><U+0000><E8><B0><U+0001><U+0000><U+0000>E1<C0>1<D2>1<C9><E8><C4><U+0001><U+0000><U+0000>E1<C0>1<D2>1<C9><E8><C8><U+0001><U+0000><U+0000>E1<C0>1<D2>1<C9><E8><CC>...
                 ^
./thread:4:5: error: source file is not valid UTF-8
5<U+0001><U+0000><U+0000><E8><B0><U+0001><U+0000><U+0000>E1<C0>1<D2>1<C9><E8><C4><U+0001><U+0000><U+0000>E1<C0>1<D2>1<C9><E8><C8><U+0001><U+0000><U+0000>E1<C0>1<D2>1<C9><E8><CC>.

1 个答案:

答案 0 :(得分:6)

通过执行#include "thread",您已经(至少按照惯例)说明了thread文件的当前目录。问题是您当前目录中有thread个文件。据推测,它是您第一次编译时的可执行文件。错误显而易见,明确说明./thread

您应该通过#include <thread>而非#include "thread"包含所有标准标头 - 您绝不想先在其他地方搜索标准标头。