【问题标题】:I can't build C++ with visual studio code我无法使用 Visual Studio 代码构建 C++
【发布时间】:2019-02-17 02:45:21
【问题描述】:

我想用 C++ 编译我的“hello world”,但我不能。 我使用 Visual Studio Code,这是我的代码。

当我尝试构建 cpp 时,错误是:

clang:错误:没有这样的文件或目录:'/Users/a1/C++/first'

我的文件是“/Users/a1/C++/first.cpp”

这适用于 macOS、High Sierra 10.14 和 Visual Studio 代码。

我安装了 C/C++,但现在无法构建我的 C++。

#include "stadfx.h"
#include < iostream >

int _tmain(int argc, _TCHAR* argv[]) {
std::cout <<"Hello, World" <<std::endl;

return 0;
}

执行任务:g++ /Users/a1/C++/first.cpp -o -g /Users/a1/C++/first

clang:错误:没有这样的文件或目录:'/Users/a1/C++/first' 터미널 프로세스가 종료 코드 1(으)로 종료되었습니다.

这是我的“tasks.json”

{
  "version": "2.0.0",
  "runner": "terminal",
  "type": "shell",
  "echoCommand": true,
  "presentation" : { "reveal": "always" },
  "tasks": [
    //C++ 컴파일
    {
      "label": "build and compile for C++",
      "type": "shell",
      "command": "g++",
      "args": [
          "${file}",
          "-o","-g",
          "${fileDirname}/${fileBasenameNoExtension}"
      ],
      "group": {
        "kind":"build",
      "isDefault": true },

      //컴파일시 에러를 편집기에 반영
      //참고:   https://code.visualstudio.com/docs/editor/tasks#_defining-a-problem-matcher

      "problemMatcher": {
          "fileLocation": [
              "relative",
              "${workspaceRoot}"
          ],
          "pattern": {
              // The regular expression. 
             //Example to match: helloWorld.c:5:3: warning: implicit declaration of function 'prinft'
              "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning error):\\s+(.*)$",
              "file": 1,
              "line": 2,
              "column": 3,
              "severity": 4,
              "message": 5
          }
      }
  },
  //C 컴파일
  {
      "label": "save and compile for C",
      "command": "gcc",
      "args": [
          "${file}",
          "-o",
          "${fileDirname}/${fileBasenameNoExtension}"
      ],
      "group": {
        "kind":"build",
      "isDefault": true},

      //컴파일시 에러를 편집기에 반영
      //참고:   https://code.visualstudio.com/docs/editor/tasks#_defining-a-problem-matcher

      "problemMatcher": {
          "fileLocation": [
              "relative",
              "${workspaceRoot}"
          ],
          "pattern": {
              // The regular expression. 
             //Example to match: helloWorld.c:5:3: warning: implicit declaration of function 'prinft'
              "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning error):\\s+(.*)$",
              "file": 1,
              "line": 2,
              "column": 3,
              "severity": 4,
              "message": 5
          }
      }
  },
  // 바이너리 실행(Ubuntu)

  {

      "label": "execute",

      "command": "cd ${fileDirname} && ./${fileBasenameNoExtension}",

      "group": "test"

  }

  ]
}

这是我的“launch.json”

"version": "0.2.0",
"configurations": [
    {
        "name": "(lldb) Launch",
        "type": "cppdbg",
        "request": "launch",
        "program": "enter program name, for example ${workspaceFolder}/a.out",
        "miDebuggerPath": "/Users/a1/C++/cpp.exe",
        "args": [],
        "stopAtEntry": false,
        "cwd": "${workspaceFolder}",
        "environment": [],
        "externalConsole": true,
        "MIMode": "lldb"
    }
]}

这是我的“c_cpp_properties.json”

{
    "configurations": [
    {
        "name": "Mac",
        "includePath": [
            "${workspaceFolder}"
        ],
        "defines": ["_DEBUG", "UNICODE"],
        "macFrameworkPath": [
            "/System/Library/Frameworks",
            "/Library/Frameworks"
        ],
        "compilerPath": "/Users/a1/C++/first.cpp",
        "cStandard": "c11",
        "cppStandard": "c++17",
        "intelliSenseMode": "clang-x64"
    }
],

"browse": {
    "path": [
        "${workspaceFolder}","/Users/a1/C++/first.cpp"
    ],
    "limitSymbolsToIncludedHeaders": true,
    "databaseFilename": ""
},

"version": 4
}

【问题讨论】:

    标签: c++ visual-studio-code vscode-tasks


    【解决方案1】:

    -o 选项需要一个文件名,它应该紧跟在“-o”之后。您已将“-g”作为文件名。然后命令以要编译的文件结束。在您的情况下,“第一”(不存在)。

    尝试将您的 json 文件中的顺序移动到类似:

    "args": [
          "-g",
          "-o",
          "${fileDirname}/${fileBasenameNoExtension}",
          "${file}"
    
      ],
    

    【讨论】:

    • 谢谢!但是我越过了新错误...:C> Executing task: g++ -g -o /Users/a1/C++/first /Users/a1/C++/first.cpp
    • @심미단 这些是 Windows 特定的标头 (stdafx.h) 和宏(_tmain 不是标准 C++)。您可能需要使用更通用且符合标准的其他指南或教科书。
    • 这是一个不同的问题。您需要在目录中有该文件。
    猜你喜欢
    • 2020-06-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多