Search code examples
c++visual-studiocygwin

Why can I not compile this C++ file written in Visual Studio in Cygwin?


I have written this C++ file.

I tested my C++ file in Visual Studio, and I tried to compile it in Cygwin too for practice.

As you can see in the first image, in Visual Studio, it was successful. But in Cygwin, the same file couldn't be compiled. This is what I tried to compile:

g++ Coursera.cpp

And these are the errors as a result:

Error1

Error2

Including a bunch of warnings, there are a lot of messages. The main errors are:

Coursera.cpp:1:1: error: stray ‘\377’ in program
 ▒▒
 ^

Coursera.cpp:1:2: error: stray ‘\376’ in program
 ▒▒
  ^

Coursera.cpp:17:4: error: invalid preprocessing directive #i; did you mean #if?
    c i n   > >   N ;
    ^
    if

Coursera.cpp:19:2: error: ‘u’ does not name a type
    i n t   n u m b 1   =   0 ,   n u m b 2   =   0 ;
  ^

Coursera.cpp:25:2: error: ‘i’ does not name a type
      {
  ^

How can I fix these errors? What is the reason for it?

I also tried compiling the same C++ file converted into UTF-8:

cd ~
g++ Coursera_UTF.cpp

And I got these errors:

In file included from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/exception_ptr.h:38:0,
                 from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/exception:142,
                 from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/new:40,
                 from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/ext/new_allocator.h:33,
                 from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/x86_64-pc-cygwin/bits/c++allocator.h:33,
                 from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/allocator.h:46,
                 from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/vector:61,
                 from Coursera_UTF.cpp:3:
/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/cxxabi_init_exception.h:38:10: fatal error: stddef.h: No such file or directory
 #include <stddef.h>
          ^~~~~~~~~~
compilation terminated.

Solution

  • In the menu, choose Save As. In the save dialog will be a Save button with down arrow. Select Save with encoding and choose ASCII.

    Visual Studio by default uses wide characters when files are created.

    Not all other compilers can compile files saved with this encoding.