在windows下使用Vscode用CMake..命令编译源代码时候遇到的问题之解决办法

        由于我在windows下用Vscode总是不记得如何deal这个错误,因此今天就用这篇博客记录下来!

        在windows的Vscode这个IDE下,打开终端,输入命令行:

C:\Users\11602\Desktop\test\build> cmake ..

        报错

-- The C compiler identification is unknown
-- The CXX compiler identification is unknown
CMake Error at CMakeLists.txt:3 (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:3 (project):
  The CMAKE_CXX_COMPILER:


  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).

        原因:我没有选择GCC作为我的编译器!(因为我windows下还有别的VS的编译器)

        deal办法

        ①先在Vscode下用键盘按下ctrl + shift + p,再选择CMake:Configure

        ②选择GCC作为我的编译器

         ③最后再用CMake..和mingw命令(windows)编译代码即可成功运行!

        (linux下则是用CMake..和make命令(linux)配合来编译代码的)

在linxu,下

出现问题:

make: *** No targets specified and no makefile found.  Stop.

原因:也是因为没有在Vscode下设置gcc为默认编译器!(同windows下的问题)

deal方法:按照上述步骤,ctrl + shift + p ,然后输入cmake configure ,然后选择gcc作为编译器即可!

  • 2
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
CMakeLists.txt 是用于构建 C/C++ 项目的 CMake 配置文件。它定义了项目的源代码文件、编译选项、依赖库等信息,以便使用 CMake 构建项目。 在 VS Code 中使用 CMakeLists.txt 可以方便地进行 C/C++ 项目的开发和构建。你可以按照以下步骤操作: 1. 确保已安装 CMake 和 C/C++ 扩展插件:打开 VS Code,在扩展面板中搜索并安装 "C/C++" 和 "CMake Tools"。 2. 在项目根目录下创建 CMakeLists.txt 文件:在 VS Code 中打开你的项目文件夹,点击左侧的资源管理器图标,在项目根目录右键点击,选择 "New File",输入文件名为 "CMakeLists.txt"。 3. 编写 CMakeLists.txt 文件:在 CMakeLists.txt 中,你需要定义项目的编译选项、源文件、依赖库等信息。以下是一个简单的示例: ```cmake cmake_minimum_required(VERSION 3.10) project(MyProject) # 设置 C++ 标准 set(CMAKE_CXX_STANDARD 11) # 添加源文件 add_executable(MyProject main.cpp) # 添加依赖库 target_link_libraries(MyProject PUBLIC mylibrary) ``` 在这个示例中,我们使用 `cmake_minimum_required` 设置所需的最低 CMake 版本,使用 `project` 定义项目名称。然后,使用 `set` 设置 C++ 标准,使用 `add_executable` 添加源文件,使用 `target_link_libraries` 添加依赖库。 4. 配置 VS Code 使用 CMake:在 VS Code 中打开 "Settings"(File -> Preferences -> Settings),搜索 "cmake" 并选择 "Cmake: Configure"。然后选择你的项目文件夹和编译目录,点击 "Yes" 来自动生成构建目录和配置文件。 5. 构建项目:在 VS Code 中打开终端(View -> Terminal),选择你的构建目录,在终端中输入 `cmake --build .` 来构建项目。 以上是在 VS Code 中使用 CMakeLists.txt 构建 C/C++ 项目的简单步骤。根据你的项目需求,可能需要在 CMakeLists.txt 中添加更多的配置信息。希望这些信息对你有所帮助!如果有其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Fanfan21ya

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值