用CMake构建工程时 cmake -G“Unix Makefiles“ 的使用

在Windows上使用CMake构建项目出现了很多错误,搜索了很多资料尝试改了很多地方始终没能找到与我类似的情况,一度让我以为是系统问题。今天发现就一句,是没用cmake -G"Unix Makefiles" 的问题。

1.在CMakelists文件中设置了以下参数仍然无效

#SET(CMAKE_MAKE_PROGRAMC "C:/mingw64/bin/make.exe")
#SET(CMAKE_C_COMPILER "C:/mingw64/bin/gcc.exe")
#SET(CMAKE_CXX_COMPILER "C:/mingw64/bin/g++.exe")

如果不设置以上代码则会出现指定的C编译器未知。The C compiler identification is unknown

d:\clang-work1>cmake .. -G"NMake Makefiles"
CMake Error: The source directory "D:/" does not appear to contain CMakeLists.txt.
Specify --help for usage, or press the help button on the CMake GUI.

d:\clang-work1>cmake -G"NMake Makefiles" .
-- The C compiler identification is unknown
-- The CXX compiler identification is unknown

2.CMake在windows下默认生成NMake Makefiles,用cmake -G"MinGW Makefiles" 则会出现以下错误原因未知。

D:\clang-work\clang-work>cmake -G"MinGW Makefiles" .
CMake Error: Error: generator : MinGW Makefiles
Does not match the generator used previously: NMake Makefiles
Either remove the CMakeCache.txt file and CMakeFiles directory or choose a different binary directory.

D:\clang-work\clang-work>

3.直接用cmake . 则会出现以下错误

D:\clang-work\clang-work>cmake .
-- Building for: NMake Makefiles
-- The C compiler identification is unknown
-- The CXX compiler identification is unknown
CMake Error at CMakeLists.txt:7 (project):
  The CMAKE_C_COMPILER:

    cl

  is not a full path and was not found in the PATH.

  To use the NMake generator with Visual C++, cmake must be run from a shell
  that can use the compiler cl from the command line.  This environment is
  unable to invoke the cl compiler.  To fix this problem, run cmake from the
  Visual Studio Command Prompt (vcvarsall.bat).

  Tell CMake where to find the compiler by setting either the environment
  variable "CC" or the CMake cache entry CMAKE_C_COMPILER to the full path to
  the compiler, or to the compiler name if it is in the PATH.


CMake Error at CMakeLists.txt:7 (project):
  The CMAKE_CXX_COMPILER:

    cl

  is not a full path and was not found in the PATH.

  To use the NMake generator with Visual C++, cmake must be run from a shell
  that can use the compiler cl from the command line.  This environment is
  unable to invoke the cl compiler.  To fix this problem, run cmake from the
  Visual Studio Command Prompt (vcvarsall.bat).

  Tell CMake where to find the compiler by setting either the environment
  variable "CXX" or the CMake cache entry CMAKE_CXX_COMPILER to the full path
  to the compiler, or to the compiler name if it is in the PATH.


-- Configuring incomplete, errors occurred!
See also "D:/clang-work/clang-work/CMakeFiles/CMakeOutput.log".
See also "D:/clang-work/clang-work/CMakeFiles/CMakeError.log".

4.cmake … -G"NMake Makefiles"则会出现以下错误,与使用cmake . 错误一样。

d:\clang-work1>cmake -G"NMake Makefiles" .
-- The C compiler identification is unknown
-- The CXX compiler identification is unknown
CMake Error at CMakeLists.txt:7 (project):
  The CMAKE_C_COMPILER:

    cl

  is not a full path and was not found in the PATH.

  To use the NMake generator with Visual C++, cmake must be run from a shell
  that can use the compiler cl from the command line.  This environment is
  unable to invoke the cl compiler.  To fix this problem, run cmake from the
  Visual Studio Command Prompt (vcvarsall.bat).

  Tell CMake where to find the compiler by setting either the environment
  variable "CC" or the CMake cache entry CMAKE_C_COMPILER to the full path to
  the compiler, or to the compiler name if it is in the PATH.


CMake Error at CMakeLists.txt:7 (project):
  The CMAKE_CXX_COMPILER:

    cl

  is not a full path and was not found in the PATH.

  To use the NMake generator with Visual C++, cmake must be run from a shell
  that can use the compiler cl from the command line.  This environment is
  unable to invoke the cl compiler.  To fix this problem, run cmake from the
  Visual Studio Command Prompt (vcvarsall.bat).

  Tell CMake where to find the compiler by setting either the environment
  variable "CXX" or the CMake cache entry CMAKE_CXX_COMPILER to the full path
  to the compiler, or to the compiler name if it is in the PATH.


-- Configuring incomplete, errors occurred!
See also "D:/clang-work1/CMakeFiles/CMakeOutput.log".
See also "D:/clang-work1/CMakeFiles/CMakeError.log".

d:\clang-work1>

5.只有使用cmake -G"Unix Makefiles"才会正确生成Makefile文件

E:\clang-work>cmake -G"Unix Makefiles"
CMake Warning:
  No source or binary directory provided.  Both will be assumed to be the
  same as the current working directory, but note that this warning will
  become a fatal error in future CMake releases.


-- The C compiler identification is GNU 8.1.0
-- The CXX compiler identification is GNU 8.1.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: C:/mingw64/bin/gcc.exe - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: C:/mingw64/bin/c++.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: E:/clang-work

E:\clang-work>

6.使用cmake -G"MinGW Makefiles"也为正确,但昨天这么写却始终报错。不知是我写错了还是哪个地方没设置好已经不记得了。可能是重新开机了,可能是将mingw32-make.exe复制了一份改名为make.exe但我想应该不是。

初学CMake很多东西不懂,自己遇到问题时到网上搜到的资料很少,刚好这个问题得以解决有一点点经验就想贡献一点资料,不足之处请指正。

猜你喜欢

转载自blog.csdn.net/yangjia_cheng/article/details/111408753