天天看点

Windows下配置cygwin/cmake

对于那些低配置的电脑, 要在windows做一些简单的coding work,安装一个VS实在有些转不开。所以我首先想到了通过cygwin+cmake配置一个简单的开发环境,对于我那台老旧的IBM T43完全没问题。

1. 安装cygwin

首先,在cygwin官网上下载最新的setup程序。运行setup.exe,依照wizard一步步安装,需要注意的是除了默认的选择,记得一定要安装gcc, gdb, make & cmake。这里我们不需要单独安装cmake,而且cmake/make程序必须是通过cygwin的setup程序安装的。整个过程可能会有点慢,特别在网络环境不好的情况下,请耐心等待。

2. 试运行

启动cygwin.exe, 运行如下命令,

mkdir <path-to-project-build>
cd <path-to-project-build>
cmake <path-to-project-source>
make
           

只要为你的project写好CMakeLists.txt文件 (至于如何编写请看看这些 tutorial.),通过执行上述命令,很容易编译生成你的exe。至于如何调试,可以尝试用gdb来debug你的程序,虽然没有VS那么好用,但是在这样一个轻量级的环境里,算是一个不错的选择。

这里,我不太清楚如何直接在cmake命令中直接指定build目录。我试过如下用法,好像不能工作。

cmake --build <dir> <path-to-source>
           

如果仅仅指定project source目录,生成的makefile等内容全部放到了project source所在的这一层目录中,比较混乱。所以上面的命令中,会先创建一个build目录,然后切换到该目录下,然后运行cmake并指定source目录。这样,生成的makefile以及相应的工程配置会自动放入build目录中,保证source目录的干净整洁。

PS: 

如果你通过单独安装cmake,然后在cygwin环境下是不工作的。同样,如果你通过cygwin的setup程序只安装了cmake,而没有安装make,在运行cmake时也不能工作。通常都会出现如下类似的错误:

$ CMake Error: CMake was unable to find a build program corresponding to "Unix Makefiles".  CMAKE_MAKE_PROGRAM is not set.  You probably need to select a different build tool.
Missing variable is:
CMAKE_C_COMPILER_ENV_VAR
CMake Error: Error required internal CMake variable not set, cmake may be not be built correctly.
Missing variable is:
CMAKE_C_COMPILER
CMake Error: Could not find cmake module file:/home/LordEvil/build/CMakeFiles/CMakeCCompiler.cmake
CMake Error: Error required internal CMake variable not set, cmake may be not be built correctly.
Missing variable is:
CMAKE_CXX_COMPILER_ENV_VAR
CMake Error: Error required internal CMake variable not set, cmake may be not be built correctly.
Missing variable is:
CMAKE_CXX_COMPILER
CMake Error: Could not find cmake module file:/home/LordEvil/build/CMakeFiles/CMakeCXXCompiler.cmake
CMake Error: CMAKE_C_COMPILER not set, after EnableLanguage
CMake Error: CMAKE_CXX_COMPILER not set, after EnableLanguage
-- Configuring incomplete, errors occurred!
CMake Error: The source directory "/home/LordEvil/build/tool." does not exist.
Specify --help for usage, or press the help button on the CMake GUI.
           

继续阅读