本页面将逐步讲解如何在 Windows 系统上编译 FreeCAD 0.19 或更新版本,使用的编译器是微软的 MSVC。若需了解使用 MSYS2/MinGW 进行编译的方法,请参阅MinGW 编译指南。其他平台的编译说明详见编译指南。
在 Windows 上编译 FreeCAD 需要多个工具和库。
注意:强烈建议使用 LibPack 设计对应的编译器版本来编译 FreeCAD。例如,如果您使用 MSVC 2017 编译 FreeCAD 0.20,可能会遇到问题,因为 LibPack 是为 MSVC 2019 或更高版本设计的。
如需后续更新 LibPack,请参阅更新 LibPack章节。
下文中我们将使用 TortoiseGit 前端来说明源代码管理操作。该前端直接集成到 Windows 文件资源管理器中,并拥有庞大的用户社区,遇到问题时可以方便地获取帮助。
现在您可以获取 FreeCAD 的源代码了:
Git 前端的使用方法
TortoiseGit:
最新源代码将从 FreeCAD 的 Git 仓库下载,同时该文件夹将被 Git 纳入跟踪。
要创建本地跟踪分支并下载源代码,请打开终端(命令提示符),切换到您希望存放源代码的目录,然后输入:
git clone --recurse-submodules https://github.com/FreeCAD/FreeCAD.git
默认的(推荐的)编译器是 MS Visual Studio (MSVC)。虽然理论上可以使用其他编译器(例如通过 Cygwin 或 MinGW 使用 gcc),但这些编译方式未经测试,本文档也不会对其进行说明。
您可以下载 MS Visual Studio 的 Community 版来免费获取 MSVC(供个人使用)。
若仅需编译器而不想安装庞大的 MSVC,请参阅 CompileOnWindows - Reducing Disk Footprint。
注意:虽然 MSVC 的 Community 版是免费的,但如果要使用其 IDE 超过 30 天试用期,则必须注册微软账户。若仅通过命令行编译,则无需使用 IDE,因此也不需要微软账户。
如果想要自由开源的替代 IDE,您可以使用 KDevelop。该工具可用于编写和修改 C++ 代码,但编译仍需通过命令行完成。
您可以选择将某些文件夹的路径添加到系统 PATH 变量中。这样做的好处是:既可以直接通过命令行/PowerShell 访问这些文件夹中的程序,也能让编译器或 CMake 找到特定的程序。此外,如果在安装程序时没有勾选相关选项,可能也需要手动添加路径到 PATH 变量。
要将文件夹路径添加到 PATH 环境变量中,请按以下步骤操作:
当你准备好所有必需的工具、库和 FreeCAD 源代码后,就可以开始配置和编译流程了。该过程将分为五个步骤进行:
首先,使用 CMake 配置构建环境:
注意:必须指定正确的位数版本。如果使用 64 位的 LibPack,则必须同样选择 x64 编译器。
这将开始配置过程,而且会失败,因为缺少相关设置。这是正常现象,因为你尚未指定 LibPack 的位置。不过,也可能会出现其他错误,需要你进一步处理。
如果配置失败并提示找不到 Visual Studio,则说明尚未安装 MSVC 的 CMake 支持组件。请按以下步骤操作:
若未出现关于 Visual Studio 或 Python 的错误,则一切正常,但 CMake 尚未获取所有必要配置。此时需进行以下操作:
此时应该不再报错。如果仍遇到无法解决的问题,请访问 FreeCAD 论坛的 Install/Compile forum。若 CMake 运行无误,点击 Generate。完成后即可关闭 CMake,并使用 Visual Studio 开始编译 FreeCAD。不过,首次编译时建议保持 CMake 开启,以便随时调整构建选项。
CMake 构建系统允许您控制构建过程的某些方面。具体来说,您可以通过 CMake 变量来启用或禁用某些功能和模块。
以下是其中部分变量的描述:
Variable name | Description | Default |
---|---|---|
BUILD_XXX | Build FreeCAD with the component XXX. If you don't want/need to compile e.g. the workbench OpenSCAD, disable the variable BUILD_OPENSCAD. FreeCAD will then not have this workbench.
Note: Some components are required for other components. If you for example uncheck BUILD_ROBOT CMake will inform you that then the component Path cannot be compiled correctly. Therefore check the CMake output after you changed a BUILD_XXX option! |
depends |
BUILD_ENABLE_CXX_STD | The version of the C++ language standard. C++14 is the highest possible for FreeCAD 0.19 while at least C++17 is required for FreeCAD 0.20. See also the note in the section Building with Visual Studio 15 (2017) and 16 (2019) | depends |
BUILD_DESIGNER_PLUGIN | To build the Qt Designer plugin, see this section below | OFF |
BUILD_FLAT_MESH | Necessary to have a build that includes the CreateFlatMesh feature | OFF |
CMAKE_INSTALL_PREFIX | The output folder when building the target INSTALL, see also the section Running and installing FreeCAD | Windows default program installation folder |
FREECAD_COPY_DEPEND_DIRS_TO_BUILD | Copies depending libraries needed to execute the FreeCAD.exe to the build folder. See also the section Running and installing FreeCAD. Note: the options FREECAD_COPY_XXX only appear if the libraries were not already copied. If you only need to upgrade/change to another LibPack version, see the section Updating the LibPack. If you want to bring back the options for some reason, you need to delete all folders in your build folder, except for the LibPack folder. In CMake delete the cache and start as if you compile for the first time. |
OFF |
FREECAD_COPY_LIBPACK_BIN_TO_BUILD | Copies the LibPack binaries needed to execute the FreeCAD.exe to the build folder. See also the section Running and installing FreeCAD. | OFF |
FREECAD_COPY_PLUGINS_BIN_TO_BUILD | Copies Qt's plugin files needed to execute the FreeCAD.exe to the build folder. See also the section Running and installing FreeCAD. | OFF |
FREECAD_LIBPACK_USE | Switch the usage of the FreeCAD LibPack on or off | ON |
FREECAD_LIBPACK_DIR | Directory where the LibPack is | FreeCAD's source code folder |
FREECAD_RELEASE_PDB | Create debug libraries (*.pdb) also for release builds. It doesn't affect the speed (like a real debug build would do) and can be very useful to locate crashes in FreeCAD code. In case FreeCAD crashes a crash.dmp file will be created that can be loaded with MSVC and if you have the corresponding PDB files plus the source code of that version you can debug through the code. Without the PDB files it's not possible to debug the code and all what the debugger shows is the name of the DLL where the crash has occurred. | ON |
FREECAD_USE_MP_COMPILE_FLAG | Adds the /MP (multiprocessor) option to the Visual Studio projects, enabling speedups on multi-core CPUs. This can greatly accelerate builds on modern processors. Note: If you turn off FREECAD_USE_PCH, the compilation can quickly overload your heap space, even if you have 16 GB RAM. |
ON |
FREECAD_USE_PCH | Precompiles the headers in order to save compilation time. | ON |
FREECAD_USE_PYBIND11 | Includes the PyBind11 library. Necessary to have a build that includes the CreateFlatMesh feature. Note: after turning it on you might get a configuration error. Just configure again and the problem should go away. |
OFF |
根据您使用的编译器不同,构建 FreeCAD 的过程会略有差异。本章节将描述已知的工作流程。如果您使用 Qt Creator 进行构建,请跳转至使用 Qt Creator 构建(已过时),否则请直接继续:
如果您想要用命令行构建,CMake 的输出会显示相应的命令(具体命令取决于配置的发布目录)。但该命令会生成一个 Debug 版本,此版本在 Windows 上无法运行,并会导致 FreeCAD 中出现 Numpy 导入错误(这是一个已知问题但难以修复)。您需要指定 --config Release 选项来强制生成 Release 版本:
cmake --build E:/release --config Release
请注意,设置像 CMAKE_BUILD_TYPE 这样的 CMake 变量不会产生任何效果,只有按照上述方式指定 --config 选项才有效。
这一过程将会耗费较长时间。
要编译可直接使用的 FreeCAD 版本,请编译 INSTALL 目标,具体操作请参阅运行与安装 FreeCAD 章节。
如果您没有收到任何错误,那么就大功告成了。恭喜!您现在可以退出 MSVC 或保持其打开状态。
重要:从 Visual Studio 17.4 版本起,对于 SketcherGui 目标,不能启用默认启用的代码优化功能。若启用该优化,草图中的角度约束将会错位。解决方法如下:在 MSVC 解决方案资源管理器中右键点击该目标,选择上下文菜单最后一项属性,在弹出的对话框中依次选择 C/C++ → 优化,然后禁用优化设置。最后请重新编译 ALL_BUILD 目标。
以下是构建 Debug 版本的必要准备工作:
现在可以开始编译:
这一过程将会耗费较长时间。
如果没有编译错误,并且如果上面提到的 CMake配置步骤中的 FREECAD_COPY_* 选项已启用,您就可以启动 Debug 构建:
这将启动 FreeCAD 的 Debug 版本,您可以使用 MSVC IDE 对其进行调试。
YouTube 上有一个英文教程:Tutorial: Build FreeCAD from source on Windows 10,该教程从 CMake Gui 配置开始讲解,直到至 Visual Studio 16 2019 中的 `Build` 命令操作。
Now FreeCAD can be built
Once complete, it can be run: There are 2 green triangles at the bottom left. One is debug. The other is run. Pick whichever you want.
The steps how to compile from the command line depends on the compiler. For MSVC 2017 the steps are:
msbuild ALL_BUILD.vcxproj /p:Configuration=Release
or
msbuild INSTALL.vcxproj /p:Configuration=Release
These steps can also be automaized. Here is for example a solution for MSVC 2017:
compile-FC install
Instead of calling compile-FC with the option install you can also use debug or release:
debug - compile FreeCAD in debug configuration
release - compile FreeCAD in release configuration
install - compile FreeCAD in release configuration and create an install setup
有 2 种方法来运行编译好的 FreeCAD:
方法 1:直接执行构建目录的子目录 bin 中的 FreeCAD.exe。
方法 2:构建 INSTALL 目标。
方法 2 更为简单,因为它能自动确保运行 FreeCAD.exe 所需的所有库文件都位于正确文件夹中。FreeCAD.exe 及相关库文件将输出到 CMake 变量 CMAKE_INSTALL_PREFIX 所指定的文件夹内。
而对于方法 1,您需要启用上文 CMake 配置步骤中提到的 FREECAD_COPY_* 相关选项。
在使用某些工作台或功能时,FreeCAD 可能会出现 DLL 缺失错误。FreeCAD 控制台的错误信息不会直接显示缺失的 DLL 名称。要查明具体缺失的文件,您需要使用外部工具进行检测:
import os os.system(r"~\DependenciesGui.exe")
注意:需要将 ~ 替换为您系统中 DependenciesGui.exe 的实际存放路径。
FreeCAD 的开发非常活跃,因此,它的源代码几乎每天都在更新——添加新功能并修复错误。要从这些源代码更改中受益,您必须重新构建 FreeCAD,这一过程分为两个步骤:
使用 Git frontend TortoiseGit 时:
最后,点击 OK。
打开终端(命令提示符),切换到源代码目录后输入以下命令:
git pull https://github.com/FreeCAD/FreeCAD.git main
其中 main 是主开发分支的名称。如果您想从另一个分支获取代码,请使用它自己的名称而不是 main 。
当第三方依赖库(如 Open Cascade)发布新的主版本、或修复了重要 bug 时,将会发布新的 LibPack。您可以在此处获取最新版本。
更新 LibPack 的最佳实践如下:
In order to join the FreeCAD development you should compile and install the following tools:
FreeCAD uses Qt as toolkit for its user interface. All dialogs are setup in UI-files that can be edited using the program Qt Designer that is part of any Qt installation and also included in the LibPack. FreeCAD has its own set of Qt widgets to provide special features like adding a unit to input fields and to set preferences properties.
The plugin cannot be loaded by the Qt Designer if it was compiled using another Qt version than the one your Qt Designer/Qt Creator is based on. Therefore the plugin must be compiled together with FreeCAD:
As result you will get the plugin file 'FreeCAD_widgets.dll in the folder
~\src\Tools\plugins\widget\Release
To install the plugin, copy the DLL either to:
Finally (re)start Qt Designer and check its menu Help → Plugins. If the plugin FreeCAD_widgets.dll is listed as being loaded, you can now design and change FreeCAD's .ui files. If not, you must compile the DLL by yourself.
If you prefer using Qt Creator instead of Qt Designer, the plugin file must be placed in this folder:
C:\Qt\Qt5.15.2\Tools\QtCreator\bin\plugins\designer
Then (re)start Qt Creator, switch to the mode Design and then check the menu Tools → Form Editor → About Qt Designer Plugins. If the plugin FreeCAD_widgets.dll is listed as being loaded, you can now design and change FreeCAD's .ui files. If not, you must compile the DLL by yourself.
FreeCAD has the feature to provide preview thumbnails for *.FCStd files. That means that in the Windows file explorer *.FCStd files are shown with a screenshot of the model it contains. To provide this feature, FreeCAD needs to have the file FCStdThumbnail.dll installed to Windows.
The DLL is installed this way:
regsvr32 FCStdThumbnail.dll
So check if it works, assure that in FreeCAD the preferences option Save thumbnail into project file when saving document is enabled and save a model. Then view in Windows Explorer the folder of the saved model using a symbol view. You should now see a screenshot of the model in the folder view.
To compile the FCStdThumbnail.dll
The LibPack comes with a version of Open Cascade that is suitable for general use. However, under some circumstances you may wish to compile against an alternate version of Open Cascade, such as one of their official releases, or a patched fork.
When compiling Open Cascade for FreeCAD note that there is no guarantee that FreeCAD will work with all versions of Open Cascade. Note also that when you are using the Netgen library, you must use the a NetGen version that it approved to compile with the Open Cascade version you like to compile.
To compile:
Variable name | Description | Default |
---|---|---|
3RDPARTY_DIR | The path to 3rdparty components. It is recommended to use the folder as input where your used LibPack is. Explicitly leave this field empty. | empty |
3RDPARTY_DOXYGEN_EXECUTABLE | The path to the executable of the 3rdparty component Doxygen. It is recommended to install Doxygen. CMake will then find it automatically. | empty |
3RDPARTY_FREETYPE_DIR | The path to the necessary 3rdparty component Freetype. It is recommended to use the folder as input where your used LibPack is. | empty |
3RDPARTY_RAPIDJSON_DIR | Only available if USE_RAPIDJSON is used. The path to the 3rdparty component RapidJSON. It is recommended NOT to use an existing LibPack folder as input. You can use the RapidJSOn folder from a LibPack, but copy it to a new folder and use this new folder as input. | empty |
3RDPARTY_TCL_DIR | The path to the necessary 3rdparty component TCL. It is recommended NOT to use an existing LibPack folder as input. Take for example one of these releases, extract it and take this as input folder for CMake. | empty |
3RDPARTY_TK_DIR | The path to the necessary 3rdparty component TK. It is recommended NOT to use an existing LibPack folder as input. Take for example one of these releases, extract it and take this as input folder for CMake. | empty |
3RDPARTY_VTK_DIR | Only available if USE_VTK is used. The path to the necessary 3rdparty component VTK. It is recommended to use the folder as input where your used LibPack is. If you use another folder please assure that you don't use VTK 9.x or newer. | empty |
BUILD_RELEASE_DISABLE_EXCEPTIONS | Disables exception handling for release builds. For FreeCAD you must set it to OFF. | ON |
INSTALL_DIR | The output folder when building the target INSTALL. If the build was successful, take the files from this folder to update your LibPack. | Windows default program installation folder |
INSTALL_DIR_BIN | The output subfolder for the DLL when building the target INSTALL. You must change it to bin | win64/vc14/bin |
INSTALL_DIR_LIB | The output subfolder for the .lib files when building the target INSTALL. You must change it to lib | win64/vc14/lib |
USE_RAPIDJSON | To compile Open Cascade with support for RapidJSON. Enabling this is mandatory in order to get support for the file format glTF. | OFF |
USE_VTK | To compile Open Cascade with support for VTK. Enabling this is optimal. You can use this to build Open Cascade's VTK bridge. | OFF |
To build FreeCAD using the self-compiled Open Cascade, you must do the following:
The LibPack comes with a version of Netgen that will was tested to be build with the Open Cascade version of the LibPack. The problem is that every new release of Netgen changes the API. Also every new release of Open Cascade does the same. Therefore one cannot just easily change the Netgen version.
However, you might build Netgen nevertheless. This is an easy task:
Variable name | Description | Default |
---|---|---|
CMAKE_INSTALL_PREFIX | The output folder when building the target INSTALL. If the build was successful, take the files from this folder to update your LibPack. | C:/netgen |
OpenCasCade_DIR | The path to the CMake files of Open Cascade. If you built Open Cascade as described in the section Compiling Open Cascade you can use the subfolder cmake of there folder you used as INSTALL_DIR. If not, use the subfolder cmake of your LibPack. Note hereby that the LibPack must then already contain a proper Open Cascade build. Independent what folder you use, you must now also create there a subfolder lib and copy in the files freetype.lib and freetyped.lib from your LibPack. | empty |
USE_GUI | set it to OFF | ON |
USE_NATIVE_ARCH | set it to OFF; this is only necessary important to support older CPU that don't have the AVX2 instruction set | ON |
USE_OCC | set it to ON | OFF |
USE_PYTHON | set it to OFF | ON |
USE_SUPERBUILD | set it to OFF | ON |
ZLIB_INCLUDE_DIR | The path to the necessary 3rdparty component zlib. It is recommended to use the folder as input where your used LibPack is. | empty |
ZLIB_LIBRARY_DEBUG | The path to the ZLib file zlibd.lib. It is located in the subfolder lib of your LibPack folder. | empty |
ZLIB_LIBRARY_RELEASE | The path to the ZLib file zlib.lib. It is located in the subfolder lib of your LibPack folder. | empty |
name: CMAKE_DEBUG_POSTFIX, type: string, content: _d
This assures that he file names of the debug libraries get another name than the release libraries and can later not be accidentally exchanged.
To build FreeCAD using the self-compiled Netgen, you must do the following:
See also