我在 Visual Studio 中测试了我的 C++ 文件,我也尝试在 Cygwin 中编译它以进行练习。
如您在第一张图片中所见,在 Visual Studio 中,它是成功的。但在 Cygwin 中,无法编译相同的文件。这是我试图编译的:
g++ Coursera.cpp
结果是这些错误:
包括一堆警告,有很多消息。 主要错误是:
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
{
^
如何修复这些错误?这是什么原因?
我还尝试编译转换为 UTF-8 的相同 C++ 文件:
cd ~
g++ Coursera_UTF.cpp
我得到了这些错误:
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.
在菜单中,选择另存为。在保存对话框中将是一个带有向下箭头的Save按钮。选择 Save with encoding 并选择 ASCII.
Visual Studio 在创建文件时默认使用宽字符。
并非所有其他编译器都可以编译使用此编码保存的文件。