Keywords: CMake | Cross-Platform Compilation | x86/x64 Architecture
Abstract: This paper provides an in-depth exploration of techniques for building x86 and x64 architecture applications using CMake from the command line in Windows environments. By analyzing CMake generator options, platform parameters, and build workflows, it details how to create separate build directories for different architectures and leverage Visual Studio generators for efficient compilation. The article compares command variations across CMake versions and supplements with CMAKE_GENERATOR_PLATFORM usage scenarios, offering a comprehensive cross-architecture build solution for developers.
Fundamental Principles of Cross-Architecture Building with CMake
When using CMake for cross-architecture compilation on Windows platforms, the core challenge lies in generating appropriate build systems for different target architectures (x86 and x64). CMake does not natively support generating multi-architecture configurations within a single build directory, which differs from Visual Studio IDE's built-in multi-platform support mechanism. Therefore, the standard approach involves creating separate build directories for each target architecture, configured and compiled independently.
Modern Approach Using Visual Studio Generators (CMake 3.13+)
For CMake version 3.13 and above, it is recommended to use Visual Studio generators with the -A parameter to specify the target architecture. This method eliminates the need to start Visual Studio command prompts, allowing all operations to be performed directly from a regular command line. The specific steps are as follows:
- Generate build system for x86 architecture:
cmake -G "Visual Studio 17 2022" -A Win32 -S \path_to_source\ -B "build32" - Generate build system for x64 architecture:
cmake -G "Visual Studio 17 2022" -A x64 -S \path_to_source\ -B "build64" - Compile x86 version:
cmake --build build32 --config Release - Compile x64 version:
cmake --build build64 --config Release
Here, the -G parameter specifies the generator type, -A specifies the target architecture (Win32 for x86, x64 for 64-bit), -S specifies the source path, and -B specifies the build directory. The --build parameter triggers the compilation process, while --config specifies the build configuration (e.g., Debug or Release).
Compatibility Solutions for Earlier CMake Versions
For CMake versions prior to 3.13, different generator names must be used to distinguish architectures. The specific operations are as follows:
mkdir build32 & pushd build32
cmake -G "Visual Studio 15 2017" \path_to_source\
popd
mkdir build64 & pushd build64
cmake -G "Visual Studio 15 2017 Win64" \path_to_source\
popd
cmake --build build32 --config Release
cmake --build build64 --config Release
The key distinction here is that x86 architecture uses the standard generator name, while x64 architecture requires appending the "Win64" suffix to the generator name. This naming convention was used in older CMake versions to differentiate architecture targets.
Traditional Approach with NMake Generators
Another traditional method involves using NMake generators, which require the Visual Studio command prompt environment. Although less common in modern development, this approach remains relevant in specific scenarios:
- For x86 builds: Launch "Visual Studio Command prompt for x86", then execute:
cmake -G "NMake Makefiles" \path_to_source\nmake - For x64 builds: Launch "Visual Studio Command prompt for x64", then execute the same command sequence.
This method relies on the command prompt environment pre-configured with architecture-specific toolchains, where CMake only generates the corresponding Makefile, and nmake performs the actual compilation.
Supplementary Notes on CMAKE_GENERATOR_PLATFORM Parameter
In addition to the methods above, CMake provides the CMAKE_GENERATOR_PLATFORM parameter as an alternative approach. This parameter can directly specify the target platform during the configuration phase:
- For x86 architecture:
cmake -DCMAKE_GENERATOR_PLATFORM=x86 . - For x64 architecture:
cmake -DCMAKE_GENERATOR_PLATFORM=x64 .
It should be noted that this method typically works with specific generator types and may have varying levels of support across different CMake versions. In practical applications, it is advisable to prioritize the -A parameter approach due to its better readability and version compatibility.
Best Practice Recommendations for Build Systems
Based on the above analysis, we summarize the following best practices:
- Directory Isolation Principle: Always create separate build directories for different architectures (e.g., build32, build64) to avoid configuration conflicts.
- Generator Selection: Prefer Visual Studio generators over NMake for better IDE integration and build performance.
- Parameter Standardization: Consistently use the
-Aparameter to specify architectures in CMake 3.13+ environments, enhancing command maintainability. - Build Automation: Encapsulate architecture-specific build commands into scripts or CI/CD pipelines to enable one-click multi-architecture compilation.
- Version Compatibility Verification: Clearly define minimum CMake version requirements in collaborative projects to ensure cross-version consistency of build commands.
By adhering to these practices, developers can establish stable and reliable cross-architecture build workflows, fully leveraging CMake's cross-platform compilation capabilities on Windows.