在 Windows 操作系统下编译 LibreOffice 的技巧及提示

From The Document Foundation Wiki
Jump to navigation Jump to search
This page is a translated version of the page Development/BuildingOnWindows and the translation is 41% complete.
Outdated translations are marked like this.
Other languages:
Deutsch • ‎English • ‎español • ‎italiano • ‎português do Brasil • ‎русский • ‎العربية • ‎中文(中国大陆)‎ • ‎日本語 • ‎한국어

Selecting the host / target system

Since core commit 114ed73a7ba56e013e6d7f886798915fb20c0946 (for more information: https://lists.freedesktop.org/archives/libreoffice/2020-September/085869.html), which basically translates to since LibreOffice 7.1, the old configure switch --enable-64-bit was dropped. To explicitly select your architecture, you have to supply the correct configure host triplet:

  • --host=i686-pc-cygwin: target 32-bit Intel / AMD host
  • --host=x86_64-pc-cygwin: target 64-bit AMD / Intel host
  • --host=aarch64-pc-cygwin: target 64-bit ARM host (WIP, but should run in general - see bug tdf#137143)

For all versions before LibreOffice 7.1, the default build is always 32-bit on Windows and you must explicitly call autogen.sh/configure with --enable-64-bit to get a 64-bit build.

Otherwise your build and host system will be the same, detected by the configure script, which is probably what you want anyway.

Depending on your decision, you must install a Java version, matching the target architecture and bit count.

Example for Windows Arm64 cross-compile including Java

  • --host=aarch64-pc-cygwin or --with-distro=LibreOfficeWinArm64
  • --with-build-platform-configure-options=--with-jdk-home=C:/PROGRA~1/Java/JDK-14~1.1
  • --with-jdk-home=/cygdrive/c/lode/jdk-16-ea+25-windows-aarch64

The first is the installed Oracle x86_64 Java, the 2nd is the unpacked pre-release of the Windows Arm64 OpenJDK Java.

编译依赖项

在 Windows 操作系统上编译 LibreOffice 之前,您需要先按照以下步骤设置好编译环境。同时,请务必阅读一般性的编译提示

安装 Visual Studio

您需要安装微软的编译/开发工具包 Visual Studio,可从以下位置获取: https://visualstudio.microsoft.com/downloads/

该工具包有一个免费的“社区版”,包含了编译 LibreOffice 所需的一切。 https://visualstudio.microsoft.com/vs/community/

当前的 master 分支 (从 LibreOffice 7.0 开始) 需要 Visual Studio 2019 (至少 v16.4 版本) (或者相应的 Visual Studio Build Tools)。

从 VS 2017 开始,有一个单独的 Build Tools 包,它包含了编译器以及多数的工具链和库,可以不需要安装Visual Studio IDE: https://visualstudio.microsoft.com/downloads/#build-tools-for-visual-studio-2019

对于较老的 LibreOffice 版本,需要的 Visual Studio 版本为:

  • LibreOffice 4.4 需要 Visual Studio 2012
  • LibreOffice 5.0 需要 Visual Studio 2013
  • LibreOffice 5.4 需要 Visual Studio 2015
  • LibreOffice 6.2 需要 Visual Studio 2017
  • LibreOffice 6.3 可使用 Visual Studio 2017 或者 2019 编译。

Visual Studio 2019 作为备选可以安装 2017 和 2015 编译器 (VS 2015 / 2017 C++ Buildtools)。唯一缺少的是较老的 C++ 可分发 MSM 包,因此较老的 configure.ac 脚本需要一个小补丁才能编译 MSI 包。

老版本的 Visual Studio 可从以下位置获取:https://visualstudio.microsoft.com/vs/older-downloads/

Visual Studio 2019

Visual Studio (以及 Visual Studio Build Tools) 2019 至少需要 v16.4 才能用来编译当前的 master 分支。使用 Visual Studio 安装 "Desktop development with C++" workload', 这包括了以下编译所需的大多数工具:

  • MSVC v142 - VS 2019 C++ x64/x86 build tools (v14.2x)
  • C++ core features
  • Windows 10 SDK (10.0.xxxxx.x)
  • Windows Universal C Runtime
  • C++ ATL for v142 build tools (x86 & x64)
  • .NET Framework 4.x.x SDK (.NET Framework 5.x SDK and .NET Framework 6.0 SDK pre-release versions are currently not supported. These don't register their information to registry, doesn't have csc.exe and they use dotnet command with csc.dll instead for compiling.)
  • C++ 2019 Redistributable MSMs (仅在编译 MSI 安装包时需要,不是 "Desktop development with C++" workload 的一部分。)
  • C++ Clang Compiler for Windows (x.x.x)

If you want to cross-build for ARM64, you have to select the following additional components:

  • MSVC v142 - VS 2019 C++ ARM64 build tools (v14.2x)
  • C++ ATL for v142 build tools (ARM64)

您可以在 VS 2019 安装包中选择以下选项。

这些可分发的 MSMs (仅在编译 MSI 安装包时需要) 不是 2019 中 workload 选项的一部分,需要单独从清单中选择。详细信息,请见对 VS 开发者社区中问题的回答: Where MergeModules for 14.20.27404.0 and 14.20.27508?Deprecation of CRT redist MSMs?

Template:注意

安装 Java

不管您使用的是哪个版本的 Visual Studio,您都需要一个

  • Java Development Kit (JDK) 版本 9 或更新的。如果是位32位 Windows 编译,请确保安装的是32位 SDK;如果是编译64位版本,请确保安装的是64位SDK。(使用 --enable-64-bit 选项)从以下位置下载:adoptopenjdk.net 或者 oracle.com(Oracle 需要登录才能下载)。

需要注意的是,Oracle JDK 仅有64位版本。

需要说明的是,AdoptOpenJDK 安装包不会自动被检测到,您需要手动为此启用 "JavaSoft (Oracle) 注册表项" 选项。

If you're cross compiling to 64-bit ARM, you need to provide a different JDK for the build-tools and the target host. While the build-tools will detect the correct JDK eventually, you must use --with-jdk-home=<Arm64 JDK directory> to provide the host JDK. You can override the build JDK by using --with-build-platform-configure-options=--with-jdk-home=<x64 JDK directory>.

安装 Cygwin 以及其他依赖项

Template:注意

安装 Cygwin 的要求

在 Windows 下,您必须从 www.cygwin.com 安装 Cygwin. 推荐的版本:2.11 或更高。

完成这项任务的最简单方式是下载 Cygwin 的 setup-x86.exe (32位) 或者 setup-x86_64.exe (64-位)。

  • 您应当尽可能地使用64位的 Cygwin, 因为这样才不会容易导致 fork() 出现故障(参见 Cygwin FAQ for details)。

以64位为例:运行 cmd 并切换到下载目录(如果切换目录存在困难,可将 setup-x86_64.exe 从文件管理器拖动到 cmd 命令提示符中,并 复制粘贴下面的命令,从而运行 setup-x86_64.exe 并预先勾选需要的包

  setup-x86_64.exe -P autoconf -P automake -P bison -P cabextract -P doxygen -P flex -P gcc-g++ ^
                -P gettext-devel git -P gnupg -P gperf -P make -P mintty ^
                -P nasm -P openssh -P openssl -P patch -P perl -P python -P python3 ^
                -P pkg-config -P rsync -P unzip -P vim -P wget -P zip -P perl-Archive-Zip ^
                -P perl-Font-TTF -P perl-IO-String

这将启动图形用户界面,并要求您手动选择安装路径等选项。但是,通过命令行传入的这些包会被预先勾选,因此您不必再去手动勾选,只需接受即可。

注意:Cygwin 的安装路径中不要包含非 ASCII 字符,以避免编译时autogen.sh/configure 出现问题(如果路径中有非 ASCII 字符,请将 Cygwin 安装文件夹移动到其他地方,并更新 Windows 的桌面及开始菜单快捷方式指向)。

另一种方式是,运行 setup-x86.exe 或者 setup-x86_64.exe,点击 'View' 直到其旁边显示 'Full',从而将包视图模式切换为'Full' 模式,然后手动勾选上述命令中的包。

如果您升级了 Cygwin, 那么您可能需要运行 rebaseall 命令(如果是首次安装 Cygwin 的话就不需要):

关闭所有的 Cygwin 终端,运行 c:\cygwin\bin\dash.exe,然后在终端中启动 /bin/rebaseall

其他依赖项

安装完 Cygwin 之后,双击桌面上的 Cygwin64 Terminal 快捷方式来打开 Cygwin 的命令行界面,然后执行下述步骤:

安装 GNU make 编译工具

Win32 版本的 GNU make 4.0+ 是在编译 LibreOffice 时必须的。

最简单的是将其放到 /opt/lo/bin 目录中,这样的话 configure 能自动找到它;您也可以使用其他位置,但是需要设置 MAKE 变量。 您可以将 /opt/lo/bin 附加到 PATH 路径中;或者,在每次编译 LibreOffice 时,使用绝对路径 /opt/lo/bin/make (或者其他的您自己选择的位置)来运行 "make"。LibreOffice 无法(而且也不应当)使用 Cygwin 的 /usr/bin/make。

mkdir -p /opt/lo/bin
cd /opt/lo/bin
wget https://dev-www.libreoffice.org/bin/cygwin/make-4.2.1-msvc.exe
cp make-4.2.1-msvc.exe make
chmod +x make


安装 ant 和 junit

您可以将其解压缩到任意位置,不需要安装它。如果您不需要 Junit,那么可以通过 autogen.sh 的 --without-junit 编译选项来禁用它。

  mkdir -p /cygdrive/c/sources
  cd /cygdrive/c/sources
  wget https://archive.apache.org/dist/ant/binaries/apache-ant-1.9.5-bin.tar.bz2
  tar -xjvf apache-ant-1.9.5-bin.tar.bz2
  wget http://downloads.sourceforge.net/project/junit/junit/4.10/junit-4.10.jar

Also works with the combination of junit 4.13,hamcrest 2.2 and Apache Ant 1.10.8

克隆 LibreOffice 源代码仓库

注意:源代码应当放到相对较短的路径中,以避免超过文件/路径长度限制,并且目标路径中不能包含空格。

  cd /cygdrive/c/sources
  git clone https://gerrit.libreoffice.org/core libo-core
可选的:创建编译文件夹

可以 将 LibreOffice 编译到一个单独的文件夹中(而不是您检出的源代码文件夹中)。这样能使得您的源代码目录树保持干净,并使得编译多个版本变得容易。

如果您想编译到单独的文件夹中,例如 /cygdrive/c/build,那么输入以下命令:

  mkdir /cygdrive/c/build
  cd /cygdrive/c/build

否则,切换到 /cygdrive/c/sources/libo-core 文件夹:

  cd /cygdrive/c/sources/libo-core
配置并编译代码

现在运行 autugen.sh 以自动生成编译过程中的配置文件:

  /cygdrive/c/sources/libo-core/autogen.sh \
               --with-external-tar=/cygdrive/c/sources/lo-externalsrc \
               --with-junit=/cygdrive/c/sources/junit-4.10.jar \
               --with-ant-home=/cygdrive/c/sources/apache-ant-1.9.5 \
               --enable-pch --disable-ccache \
               [可以使用其他的选项,如果您知道自己在做什么 :-) ...]

注意,在 Windows 上不推荐使用 ccache 进行编译缓存。如果您已经在 Cygwin 中安装了 ccache,那么可以在 autogen.sh 中使用 --disable-ccache 来安全地禁用它。

如果运行 autogen.sh 时它提示找不到某个头文件,那么很有可能是它无法找到您系统中的 Windows SDK,或者找到的是一个损坏的版本。在您安装了多个版本的Visual Studio 的情况下,就可能会发生这种情况,尤其是当您安装了一个 Beta 测试版本的情况下,即便是您已经卸载了它。如果出现这种情况,您可以使用 --with-windows-sdk=7.1A 编译选项(将 7.1A 改为SDK对应的版本),以手动指定未损坏的版本。

您可以将 autogen.sh 后的编译选项保存到 autogen.input 文件中。如果 LibreOffice 源代码的根目录下存在 autogen.input 文件,那么在不带参数运行 autogen.sh 的情况下它会自动去 autogen.input 文件中获取编译选项;在带任何参数运行 autogen.sh 的情况下,就会使用传递给 autogen.sh 的参数,不会使用 autogen.input 中的选项。

阅读 autogen.sh 运行完成后的输出提示。如果没有任何的错误或相关警告,那么接下来只需要运行 "make" 就能进行编译了。但在执行 make 之前,确保您的杀毒软件不会带来任何的问题

   /opt/lo/bin/make gb_COLOR=1  # gb_COLOR=1 启用彩色输出。

如果运行 autogen.sh 之后出现了 error C3861: 'InetPtonW': identifier not found 错误,那么很可能是因为使用 Windows SDK 8 编译时的一个 bug 导致的。作为变通办法,您可以使用 --with-windows-sdk=7.1A 选项。

运行编译好的 LibreOffice

当编译完成、并且没有错误时,您就可以运行刚刚编译好的全新的 LibreOffice 了。

   instdir/program/soffice.exe

与微软 Visual Studio 项目集成

The LibreOffice build system can generate project file for some IDEs including -- thanks to Honza Havlíček's excellent work -- Microsoft Visual Studio: (note that this does not allow a full build so you still need to use Cygwin to do full builds)

生成 Visual Studio 的项目文件

The IDE integration script is written in python3, which is not installed by default (using the lode method), please install it in cygwin before running the make command. After a complete build of LibreOffice was successful, execute the following command to generate a Visual Studio project for Visual Studio 2019:

  $ /opt/lo/bin/make vs2019-ide-integration

For Visual Studio 2017 use:

  $ /opt/lo/bin/make vs2017-ide-integration

If the generation is successful, the resulting solution file LibreOffice.sln will be placed in the root directory of the build and can be opened with Microsoft Visual Studio.

Note that if you use --enable-mergelibs, the generated solution will for unclear reason contain less projects, and have problems finding many symbols. Until that is fixed, it's advisable to avoid --enable-mergelibs when using IDE integration.

为什么要使用 MSVC?

这是一个经常被问到的问题,通常来自于自由软件热衷者:我们为什么要使用专有编译器,而不是类似 gcc 的自由编译器?

  • ABI backwards-compatibility for compiled extensions. There is some resistance to breaking that.
  • So far MSVC produces faster, smaller binaries.
  • There are some features in the code that don't compile with MinGW. They use API that MinGW does not provide headers for etc.
  • There are open questions around how we would run unit tests in a MinGW cross-compilation environment.
  • There are open questions around how well gdb works on Windows; the MSVC C++ debugger is really quite good.

Autogen 选项

Something like the following in the autogen.input file is known to produce a working build:

--disable-odk
--with-junit=/cygdrive/c/sources/junit-4.10.jar
--with-ant-home=/cygdrive/c/sources/apache-ant-1.9.5

Of course, change the information behind ='s to fit your needs; set the paths to what you have used when installing various prerequisites.

You also need to specify the path for ant with --with-ant-home=<cygwin path of ant>.

使用 Visual Studio 编译64位软件

A port for 64-bit Windows is working on master with all supported compilers. To build it, use the --enable-64-bit flag to configure before LibreOffice 7.1.

针对更老的以及非默认微软编译器的说明

切换到非默认编译器

Specify which Visual Studio version to use in case several are installed.

If not specified, the order of preference is 2015, 2017 for libreoffice-6-1 (and older branches), 2019 is used for master.

--with-visual-studio=2019

在 Windows 上运行 Glade

To edit dialogs (.ui files), you need Glade. There is an old version for Windows but it doesn't support all properties that are needed for LibreOffice dialogs.

So you need to install Glade in Cygwin:

  • Install Glade in Cygwin by running the Cygwin Setup, then install the following packages:
X11, X11 fonts, Gtk3, Glade, OpenGL
  • Add this to your .bashrc:
export GLADE_CATALOG_SEARCH_PATH=<path_to_core>/instdir/share/glade
  • Open one Cygwin console and run:
X -multiwindow
  • Open another Cygwin console and run:
DISPLAY=:0.0 glade.exe

That's it, you should now run Glade ≥ 3.18

已知的问题

Cygwin 和 git

The main problem is the cygwin-git is very slow.

The alternatives are:

Here are the times using the same computer and repo and not being the first call:

git status

git Version Time [sec]
cygwin 2.21.0 20
win 2.22.0 1
WSL 2.16.0 6

Git in WSL is slower than git-bash, but it has the advantage of working in a complete Linux environment with all other tools provided by the distribution. You should not mix tools from Cygwin and WSL (i.e. open separate terminals for Cygwin and for WSL).

NOTE: A mix between different git tools is not always compatible, when you change you should renew (clone) the git repositories.

Best to configure "autocrlf" before cloning master, to avoid the problem (see below for how to set "autocrlf" to a sensible value)

Executable permission on files

It is desirable to use filemode = false in the [core] section of .git/config, to avoid all files having executable permission in cygwin-git. This setting allows Git to ignore the differences between filemodes of files on disk and in index, which is useful with cygwin-git, which sees all files on disk as having executable permission (755 instead of typical 644). However, sometimes you need to add a script file with executable permission, and with the mentioned setting, you would need to use an explicit command to set the permission to the added file:

  git update-index --chmod=+x myfile.sh

Ref: git-update-index documentation.

Cygwin Shell

You will use Cygwin Bash Shell for the whole build process. You must install all the build dependencies above before starting the shell to run autogen because autogen needs some environment variables to find the tools.

If you see an error similar to the following when running autogen.sh

   $'\r': command not found

This means that that Windows style newline characters are creating issues with the Cygwin Shell. The good way to solve this is to configure git to clone with UNIX line ending. To do so, give the following command in Cygwin Shell:

   git config --global core.autocrlf input


Big List Of Dodgy Apps

If you get mysterious build breaks, building is very slow or your systems hangs totally, check if you are running an application which interferes with Cygwin. For a list of known offenders, see this point in the Cygwin FAQ.

'master' 的可编译性

If you are building the Windows build for the first time, it is recommended that you check the current state of https://tinderbox.libreoffice.org/MASTER/status.html . If you see recent "green" builds, it is a good time to try - otherwise it might happen to you that you'll have to fix the build yourself. The buildability on Windows has improved recently with the introduction of the MinGW and more stable native Windows tinderboxes, but still there might be a day or two when 'master' is not buildable on Windows.

The usual recommendation is to just pull and try again, and once you have a build, don't pull in the tree any more, but do your hacking in it. Clone a different tree and iterate pulling and building in that until it again works, then switch. Repeat.

Alternatively, if you find a known good state (a green box) for Windows on the Tinderbox side back in history, you can do:

git checkout -b my_workbranchname ${last_good_commit}

to start working on a branch starting at the last good state.

BitDefender(比特梵德)及其他反病毒及安全工具对编译过程的破坏

If your build fails with a FileNotFoundError: such as The system cannot find the file specified: '../../../dist/out/bin\\example.exe', there's a good chance your Anti-Virus is quarantining or deleting important files.

Another potential source of problems are the sample documents (those labeled CVE) that check for known vulnerabilities to ensure they don't reappear again. In the source they are stored encrypted to avoid triggering the warnings, but when the corresponding test is run, the original is created in the temp directory and tools such as BitDefender can flag it.

If you want to run these tests, disable monitoring of the temporary build-directories <root of repo>\*\pass and C:\cygwin\tmp in your Anti-Virus tool. For Windows 8 and 10 you will need to disable Windows Defender launch - search for and launch 'defender' from the shell.

You can also use the --disable-cve-tests autogen flag to skip the tests that are known to cause trouble.

Also note that Anti-Virus tools can slow down your build quite a bit, if you want to build fast you should disable real time monitoring of the source tree and the temporary files directory C:\cygwin\tmp.

Norton(诺顿)反病毒软件对编译过程的破坏

Norton Antivirus and Symantec Endpoint Protection complain about certain files that the build produces. It finds the following files (in a build without "--enable-dbgutil" )

Malware: Suspicious.Cloud
D:\bld\rel\workdir\UnpackedTarball\icu\source\bin\genrb.exe*
Malware: Suspicious.Cloud.5
D:\bld\rel\workdir\UnpackedTarball\nss\nss\lib\zlib\out\example.exe*
D:\bld\rel\workdir\UnpackedTarball\nss\dist\out\bin\example.exe* 
D:\bld\rel\instdir\program\reg4allmsdoc.dll
D:\bld\rel\instdir\program\sdqsmsi.dll
D:\bld\rel\instdir\program\sellangmsi.dll

Files with * are needed for the build - exclude them from the virus scan!

The following files appear with varying names (????) in a release build, so you cannot exclude them from the scan.

D:\bld\rel\instdir\program\RCX????.tmp
D:\bld\rel\instdir\program\RCX????.tmp
D:\bld\rel\instdir\program\RCX????.tmp

Current workaround: Don't do release builds (unless you are allowed/willing to switch off the virus scanner).

系统 PATH 路径造成的奇怪的编译损坏问题

If you experience weird build breakage that no one else seems to experience, check your PATH value and try cleaning it. Note that you need to open a new shell and re-run autogen.sh in order for the new system PATH value to take effect in your build. Some people have experienced a build failure in the shell module due to their system PATH including a path to an older OOo installation which had a DLL of the same name as the one that the build uses.

Windows 自动更新在编译时自动重启系统

Sometimes Microsoft Windows reboot during compilation. It happen because Windows Update updates operating system and reboot. To stop windows update from command line, you can enter following command in command box:

C:> net stop wuauserv

It stops a Windows Update service process.

PDF 数字签名无法通过测试

On Windows, it can happen, that PDF signing tests are failing:

[build CUT] xmlsecurity_pdfsigning
warn:sfx.appl:2956:6324:sfx2/source/appl/app.cxx:193: No DDE-Service possible. Error: 16399
warn:svl.crypto:2956:6324:svl/source/crypto/cryptosign.cxx:1415: CryptAcquireCertificatePrivateKey failed: Das Zertifikat und der private Schlüssel für die Entschlüsselung wurden nicht gefunden.
warn:vcl.filter:2956:6324:vcl/source/filter/ipdf/pdfdocument.cxx:846: PDFDocument::Sign: PDFWriter::Sign() failed
C:/Users/davido/projects/libo/xmlsecurity/qa/unit/pdfsigning/pdfsigning.cxx:174:PDFSigningTest::testPDFAdd assertion failed
- Expression: aDocument.Sign(aCertificates[0], "test", true)

Solution: Remove expired Own Certificate: http://imgur.com/a/NdCxT

  1. Start certmgr
  2. proceed to Own Certificates
  3. Remove expired certificate
  4. Repeat the tests
  5. Done

短文件名 (8.3 文件名)

If you get an error like the following one:

configure: error: converted path "C:/PROG~1/Microsoft SDKs/Windows/v8.1A/bin/NETFX4~1.1TO/" still contains spaces. Short filenames (8.3 filenames) support was disabled on this system?
Error running configure at ./autogen.sh line 300.

Then ensure 8.3 name creation is enabled. Open the Command Prompt as administrator and run:

fsutil 8dot3name query c:

Also, check that the conflicting folder (in this case, "Microsoft SDKs") has a corresponding 8.3 filename. For example:

cd C:\Program Files (x86)\
dir /x M*

This will print a list of folders beginning with "M" and their associated 8.3 filenames. If "Microsoft SDKs" does not have a short filename, you can rename it to "tmp", then create a new folder "Microsoft SDKs", check that it has a short filename and move the contents of "tmp" to "Microsoft SDKs".

When choosing a directory in which to install LODE, please use the 8.3 rules. Windows does not correct environment variables, therefore e.g. LODE_HOME will be wrong and will cause problems. Therefore, always clone lode in a 8.3 path such as:

C:\source\LO\lode

autogen.sh might report that it cannot find mscvc.exe.

This is caused by the long filename problem as well, but the solution is a bit more work:

Shift+reset, go to the safe mode, do this:

fsutil 8dot3name set c: 0
fsutil file setshortname "Program Files (x86)" PROGS
fsutil file setshortname "Microsoft Visual Studio 12.0" msvc-12
fsutil file setshortname "Windows Kits" W~Kits

Correct your PATH variable

Windows 权限问题

If you receive the following error :

ALINK : error AL1078: Error signing assembly -- Access is denied.

Then open Developer Command Prompt for VS as an administrator, and use sn tool to set `AL.EXE` to use current user key set to sign assemblies instead of using machine key set:

sn -m n


This modifies the following registry keys:

for 64-bit systems:

HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\StrongName

for 32-bit systems:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\StrongName

See this stack overflow question and mail thread starting from this message for more information.

其他已知问题

  • Paths that include /opt cause problems with some external modules; it is best to avoid having your source tree in /opt; or use --disable-orcus to overcome this.
  • Asian locale builds sometimes fail due to encoding issues in the source code; in these cases, patches to fix those problems are appreciated. Alternatively, configure Windows for a US locale.

使用 Windows 程序作为您的 git 文本编辑器

Paths under Cygwin are an endless source of nightmares. This section documents using Notepad++ as your git editor under Cygwin. It assumes you are using the 64-bit version of Notepad++ (adjust the path, if using the 32-bit version).

Below you will find a wrapper script written by Gene Pavlovsky (original gist in GitHub).

  1. Save the script as cygrun.sh into the bin directory in Cygwin's root (so it is in the PATH). When saving the cygrun.sh using Notepad++, do this first to make sure it has Unix line endings: from the "Edit" menu, select "EOL Conversion" -> "UNIX (LF)".
  2. In a Cygwin shell inside the bin directory, give this command to create a symbolic link:
    ln -s cygrun.sh cygrun
    
  3. Give this command in your home directory:
    git config --global core.editor "cygrun -w 'C:/Program Files/Notepad++/notepad++.exe' --wait"
    
    or alternatively edit your .gitconfig to contain this editor block:
    editor = cygrun -w 'C:/Program Files/Notepad++/notepad++.exe' --wait
    
    The --wait is important, as it forces git to wait until you close the file.

Bonus: if you want to define Notepad++ as the default editor of your shell and also access it through a handy "np" alias, add these to the .bashrc file in your home directory:

export EDITOR="'/cygdrive/c/Program Files/Notepad++/notepad++.exe'"
alias np="'/cygdrive/c/Program Files/Notepad++/notepad++.exe'"

Remember to be very careful with the line endings, when creating new files and use the Edit -> EOL Conversion.

The cygrun script:

#!/bin/bash
#
# Run a program, converting UNIX and Windows format path arguments.
#
# Install in Cygwin's `bin` dir or elsewhere in your path. 
# Create a symlink for convenience: `ln -s cygrun.sh cygrun`.
#
# Use `cygrun -w windows-program unix-path ...` to run Windows programs (e.g. from UNIX software).
# E.g., in my .gitconfig core.editor is set to `cygrun -w 'C:/Program Files/Notepad2/Notepad2.exe'`.
#
# Use `cygrun [-u] unix-program windows-path ...` to run Cygwin programs (e.g. from Windows software).
# Usually not needed since Cygwin translates Windows paths automatically (e.g. `ls 'C:\Windows'` works).
# Still it can be useful in case a Windows program might produce paths that mix backslashes and forward slashes,
# e.g. in Apache Ant `${dist.dir}/file` evaluates to `C:\path\to\project\dist/file`, which Cygwin won't
# translate automatically. So, to deploy the file, I use `cygrun -u scp ${dist.dir}/file ${deploy.url}`.
#
# Use the `-b` option to detach from tty, run the process in background, and return immediately.
#
# There is currently a limitation on passing non-path arguments to a program. Since there is no way
# to automatically distinguish path arguments from non-path arguments, all of the arguments are passed
# to `cygpath`. As long as non-path arguments don't contain `/` or `\` characters, and `-a` or `-C`
# options are not used, `cygpath` will return the arguments unmodified - no problem.
# Due to this, the `-a` and `-C` options are disabled at the moment.
# TODO: Add an option allowing to specify index positions of path arguments (comma-separated, support ranges).
#
# Most of the options are passed as is to `cygpath`, which is used for path conversion.

set -e -o pipefail

usage() {
  local code=${1:-2}
  test $code -ne 0 && exec >&2
  
  echo "Usage: $(basename "$0" .sh) [options] program [argument]..."
  echo
  echo 'Run a program, converting UNIX and Windows format path arguments.'
  echo
  echo 'Output type options:'
  echo
  echo '  -d, --dos             print DOS (short) form of NAMEs (C:\PROGRA~1\)'
  echo '  -m, --mixed           like --windows, but with regular slashes (C:/WINNT)'
  echo '  -u, --unix            (default) print Unix form of NAMEs (/cygdrive/c/winnt)'
  echo '  -w, --windows         print Windows form of NAMEs (C:\WINNT)'
  echo
  echo 'Path conversion options:' 
  echo
  echo '  -U, --proc-cygdrive   Emit /proc/cygdrive path instead of cygdrive prefix'
  echo '                        when converting Windows path to UNIX path.'
  echo '  -c, --codepage CP     print DOS, Windows, or mixed pathname in Windows'
  echo '                        codepage CP.  CP can be a numeric codepage identifier,'
  echo '                        or one of the reserved words ANSI, OEM, or UTF8.'
  echo '                        If this option is missing, cygrun defaults to the'
  echo '                        character set defined by the current locale.'
  echo
  echo "Other 'cygpath' options:"
  echo
  echo '  -f, --file FILE       read FILE for input; use - to read from STDIN'
  echo "  -o, --option          read 'cygpath' options from FILE as well (for use with --file)"
  echo '  -h, --help            output usage information and exit'
  echo
  echo 'Run options:'
  echo
  echo '  -b, --background      run in a new session'
  echo '  -n, --dry-run         print the command that would be executed, and exit'
  
  exit $code
}

run() {
  local -n _run_args=$2
  if test "$dry_run"; then
    test "$chdir" && printf 'cd %s\n' "$chdir"
    printf '%s\n' "$1 ${_run_args[*]}"
  else
    test "$chdir" && cd -- "$chdir"
    if test "$background"; then
      setsid "$1" "${_run_args[@]}" </dev/null &>/dev/null &
    else
      exec "$1" "${_run_args[@]}"
    fi
  fi
}

declare opts fileopts chdir background dry_run
while test $# -gt 0; do
  case $1 in
    -d|--dos|-u|--unix|-m|--mixed|-w|--windows|-U|--proc-cygdrive|-o|--option)
      opts="${opts} $1"
    ;;
    -C|--codepage|-f|--file)
      opts="${opts} $1 $2"
      shift
    ;;
    -f|--file)
      fileopts=1
      opts="${opts} -i $1 $2"
      shift
    ;;
    -b|--background)
      background=1
    ;;
    -n|--dry-run)
      dry_run=1
    ;;
    -h|--help)
      usage 0
    ;;
    --)
      shift
      break
    ;;
    -*)
      script="$(basename "$0" .sh)"
      echo "$script: Unknown option: $1" >&2
      echo "Try '$script --help' for more information." >&2
      exit 2
    ;;
    *)
      break
    ;;
  esac
  shift
done
test "$chdir" && opts="$opts -a"

program=$1
test "$program" || usage
shift

declare -a args
test $# -gt 0 -o "$fileopts" && mapfile -t args < <(cygpath $opts -- "$@" || exit 1)
run "$program" args

另请参考