CMake 为 Win32 和 x64 生成 Visual Studio 2008 解决方案

Posted

技术标签:

【中文标题】CMake 为 Win32 和 x64 生成 Visual Studio 2008 解决方案【英文标题】:CMake generate Visual Studio 2008 solution for Win32 and x64 【发布时间】:2010-09-24 10:00:49 【问题描述】:

我在 Windows XP 下使用 CMake 2.8,并希望生成一个 Visual Studio 2008 解决方案文件,其中包含 Win32 和 x64 的发布和调试配置。

这可以通过在 CMakeLists.txt 文件中设置 CMake 配置变量来完成吗?

【问题讨论】:

cmake.org/cmake/help/latest/generator/… 【参考方案1】:

CMakeLists 不是您为项目指定生成器(makefile、XCode、Visual Studio)的地方。当用户在您的源代码上运行 CMake 时会指定生成器。

【讨论】:

我知道。但是有没有办法让 Visual Studio 生成器生成一个解决方案文件,允许在 Windows XP 下交叉编译 x64? 不,您需要单独的解决方案文件和 x32 和 x64 的构建目录。这两个都将包含发布和调试配置。 我认为你的意思是 x86 而不是 x32。【参考方案2】:

您应该使用 CMake.exe 在控制台下生成解决方案文件。

CMake -G "Visual Studio 9 2008"(包含您的 CMakeLists.txt 文件的目录)// 这是 X86 的。

CMake -G "Visual Studio 9 2008 Win64"(包含你的 CMakeLists.txt 文件的目录)// 这是 X64 的。

【讨论】:

【参考方案3】:

您绝对应该使用批处理脚本来自动化这些过程。我与您分享我自己的构建器脚本的简化版本。我自己的环境要复杂得多,所以我写了这个脚本供你轻松使用。

它的用法非常基本。

builder . -64bit -Debug

表示源码在当前目录,编译配置为64bit Debug版本。

您只需要在脚本中更改全局配置变量 visual studio目录和cmake目录。

SET VISUAL_STUDIO_9_HOME=

SET CMAKE_HOME=

@echo off

rem ===== Usage
rem builder c:\workspace\project1 -32bit -Release
rem builder . -64bit -Debug

rem Global configuration variables. Should be update based on your system
SET VISUAL_STUDIO_9_HOME=c:\Program Files\Microsoft Visual Studio 9.0
SET CMAKE_HOME=c:\Documents and Settings\stumk\My Documents\toolkit\cmake

rem First parameter is project folder path
SET PROJECT_DIR=%1

rem Add executables into path
rem Only add them once
if NOT DEFINED SETENV  SET SETENV=0
if %SETENV% EQU 0 SET PATH=%CMAKE_HOME%\bin;%VISUAL_STUDIO_9_HOME%\Common7\Tools;%PATH%
SET SETENV=1

rem Go to project director
cd %PROJECT_DIR%

rem Create build folder, don't mess the source code with visual studio project files
md build

rem Go to build folder
cd build\
   rem Set visual studio environment variables
   call vsvars32.bat 

   rem Second parameter defines 32 bit or 64 bit compilation
   if "%2"=="-32bit" (
       cmake.exe .. -G "Visual Studio 9 2008"
   )
   if "%2"=="-64bit" (
       cmake.exe .. -G "Visual Studio 9 2008 Win64"
   )

   rem Third parameter defines debug or release compilation
   if "%3"=="-Debug" (
           cmake.exe --build . --target ALL_BUILD --config Debug
   )
   if "%3"=="-Release" (
           cmake.exe --build . --target ALL_BUILD --config Release
   )

   rem Go to source code directory and finalize script
   cd ..

@echo on

【讨论】:

以上是关于CMake 为 Win32 和 x64 生成 Visual Studio 2008 解决方案的主要内容,如果未能解决你的问题,请参考以下文章

强制 CMake 寻找 x64 位库而不是 x32

Visual Studio、Windows 7 x64 中的“生成 Win32 资源时出错”

opencv 学习一安装环境

cmake/modules/AddLLVM.cmake 处的 CMake 错误

CMake 使用 32 或 64 编译器

windows 7 x64用cmake,mingw32安装LLVM,编译时报错MemoryBuffer.cpp:381:25: error