简体   繁体   中英

cpp file not compiling in clang++ and g++

I have been working on C++ for few years now and have compiled stuff several times but the following issue is totally new to me and it just doesn't make sense.

Following are the steps i am following:

  • using cygwin setup with g++ version:6.4.0 and clang++ version:4.0.1
  • created a new cpp fie using sublime text added simple cout and compiled
    with the command: clang++ -g -Wall -std=c++14 thread1.cpp -o thread, works fine.
  • added new contents maybe another cout, this time upon compilation i get a ton of errors stating its not utf-8 file.
  • saved the file using utf-8 encoding in sublime text and also tried with utf-8 BOM encoding, still getting same not utf-8 file error.
  • ran the file command in cygwin to check file encoding , file -i thread1.cpp, got output as thread1.cpp: text/xc; charset=utf-8.

Any pointers to what might be going wrong here?

Following is the code which compiles:

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

class threadFunctor{
        public:

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

Following code gives error:

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

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

Following is a snippet of the errors generated:

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

By doing #include "thread" , you've said (by convention at least) to check the current directory for a thread file first. The problem is that you have a thread file in your current directory. Presumably, it's your executable from the first time you compiled. This is apparent by the errors, which explicitly say ./thread .

You should include all standard headers via #include <thread> rather than #include "thread" - you never want to search elsewhere first for standard headers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM