一次windows下使用cmake遇到的问题

背景

在windows下的cmake和mingw提供的make,在windows环境下进行了简单尝试,结果发现make的时候失败:

#include <iostream>
 
int main(){
    std::cout << "Hello, makefile." << std::endl;
    return 0;
}

CMakeList如下:

project(test)
add_executable(test test.cpp)

非常简单的语法,但在项目目录创建了build目录以后:

cd build; cmake ..
make

出错了:make: *** No targets specified and no makefile found. Stop.,是的,make出错,但这时候还不懂问题所在,以下是build目录的生成文件:

build目录下文件

如上,这种样式的build,不就满满的vs风嘛?!后来才在一位博主的文章中得到灵感,确定了cmake时不明确指定平台,它就会自动读取系统信息,然后进行系统相关的默认生成,所以上面需要用上make,就需要明确一点:

PS D:\Desktop\test\build> cmake .. -G "Unix Makefiles"
-- 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: D:/software/mingw-w64/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: D:/software/mingw-w64/bin/c++.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: D:/Desktop/test/build
PS D:\Desktop\test\build> ls


    目录: D:\Desktop\test\build


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d-----          2023/6/8     16:17                CMakeFiles
-a----          2023/6/8     16:17          16598 CMakeCache.txt
-a----          2023/6/8     16:17           1554 cmake_install.cmake
-a----          2023/6/8     16:17           5337 Makefile


PS D:\Desktop\test\build> make
[ 50%] Building CXX object CMakeFiles/test.exe.dir/test.cpp.obj
[100%] Linking CXX executable test.exe
[100%] Built target test
PS D:\Desktop\test\build> ./test
Hello, cmakefile.

整理一下,cmake的使用,是控制项目有序生成对应系统平台的build,windows下是生成比较标准的vs解决方案,嗯所以很多时候用cmake以后还是用vs打开来编译一下;linux下则是专门生成makefile(当然windows下指定参数来生成也是可以),后面再make就行。

更多文章可以查看

posted @ 2023-06-08 16:22  夏目&贵志  阅读(254)  评论(0编辑  收藏  举报