簡體   English   中英

我無法使用Visual Studio代碼構建C ++

[英]I can't build C++ with visual studio code

我想用C ++編譯我的“ hello world”,但我不能。 我使用Visual Studio代碼,這是我的代碼。

當我嘗試構建cpp時,錯誤是:

鐺:錯誤:沒有這樣的文件或目錄:'/ 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 <

鐺:錯誤:沒有這樣的文件或目錄:'/ 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
}

-o選項需要一個文件名,該文件名應緊接在“ -o”之后。 您已將“ -g”作為文件名。 然后命令以要編譯的文件結束。 在您的情況下,“第一個”(不存在)。

嘗試將json文件中的順序移動到類似以下內容:

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

  ],

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM