在没有IDE的Windows上使用Cmake

Using cmake on windows without IDE

本文关键字:Cmake Windows IDE      更新时间:2023-10-16

这可能是重复的,但是我已经花了几个小时来寻找答案...没有解决方案。首先,我知道如果我使用Linux,可能不存在这个问题,但是我在Windows上。

我是C 的新手,但已经对Java和Gradle有了一些经验。我尝试使用cmake,就像我用来使用gradle一样。我已经阅读了Cmake Wiki,但我要么找不到正确的页面,要么我不明白。这是我的目录结构:

MyProject
-bin
-include
--header1.h
--header2.h
--header3.h
--header4.h
--header5.h
--header6.h
-src
--CMakeLists.txt
--MyProjectConfig.h.in
--impl1.cpp
--impl2.cpp
--impl3.cpp
--impl4.cpp
--impl5.cpp
--impl6.cpp
-main.cpp
-CMakeLists.txt

我的项目文件夹中的CMakeLists.txt看起来像:

cmake_minimum_required (VERSION 3.14)
project (MyProject)
add_subdirectory(src)
file(GLOB_RECURSE sources      src/*.cpp include/*.h)
# The version number.
set (Tutorial_VERSION_MAJOR 0)
set (Tutorial_VERSION_MINOR 1)

# configure a header file to pass some of the CMake settings
# to the source code
configure_file (
  "${PROJECT_SOURCE_DIR}/MyProjectConfig.h.in"
  "${PROJECT_BINARY_DIR}/MyProjectConfig.h"
  )
# add the binary tree to the search path for include files
# so that we will find MyProjectConfig.h
include_directories("${PROJECT_BINARY_DIR}")  
install (FILES "${PROJECT_BINARY_DIR}/MyProjectConfig.h"        
         DESTINATION include)
add_executable(MyProject main.cxx ${sources})
# add the install targets
install (TARGETS MyProject DESTINATION bin)

我的cmakelists.txt在src文件夹中看起来像:

cmake_minimum_required (VERSION 3.14)
include_directories(${MyProject_SOURCE_DIR}/MyProject/include)

我在bin bin目录中使用命令: cmake -G "MinGW Makefiles" -S ../src

我现在有2个问题:

  1. (我如何告诉cmake始终使用mingw?(我不想总是使用-g总是使用))解决
  2. (编译的文件buildCMakeFiles3.14.0-rc2CompilerIdCXXa.exe没有预期的行为。它应该打印" Hello World!"answers"我的类",而"我的类"是从impl1.cpp创建的类的属性中打印的,但是它无能为力。)需要澄清:如何在控制台上构建Windows .exe-file?

编辑:

我了解到,在创建CMAKE文件后,我必须在BIN目录中调用cmake --build .。但是,我只是没有得到Exe-File。使用flag -v,我得到此输出:

"C:Program FilesCMakebincmake.exe" -SD:gitMyProjectsrc -BD:gitMyProjectbin --check-build-system CMakeFilesMakefile.cmake 0
"C:Program FilesCMakebincmake.exe" -E cmake_progress_start D:gitMyProjectbinCMakeFiles D:gitMyProjectbinCMakeFilesprogress.marks
C:/MinGW/bin/mingw32-make.exe -f CMakeFilesMakefile2 all
mingw32-make.exe[1]: Entering directory 'D:/git/MyProject/bin'
mingw32-make.exe[1]: Nothing to be done for 'all'.
mingw32-make.exe[1]: Leaving directory 'D:/git/MyProject/bin'
"C:Program FilesCMakebincmake.exe" -E cmake_progress_start D:gitMyProjectbinCMakeFiles 0 ```
  1. 使用-g选项是这样做的标准方法。它可以防止您不得不将特定于系统的设置放入您的CMAKE配置中(例如MINGW的硬编码路径),并允许您使用其他编译器而不必更改构建脚本。
  2. 您开始的a.exe不是您的构建输出。它应称为MyProjectExec.exe。另外,您需要将调用中的所有源文件指定为add_executableadd_subdirectory不会自动添加任何源文件(应该添加哪个构建输出?),它只是执行cmakelists.txt。