Giới thiệu

Như các bạn đã biết thì Visual Studio Code là được sử dụng khá phổ biến vì có tích hợp nhiều extension hỗ trợ chạy các chương trình. Nhưng nó chỉ chạy được ở dạng terminal hoặc output.

Bài viết này sẽ hướng dẫn các bạn chạy C++ trên cửa sổ console như của Dev-Cpp ngay trên Visual Studio Code.

Biên dịch và chạy chương trình trên console

Đầu tiên, các bạn nhấn F5 -> C++ (Windows), một file launch.json được tạo ra. Nếu không có bạn có thể tạo thủ công.

Các bạn sửa file lại như sau:

"configurations": [
    {
        //...

        //Sửa lại
        "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
        
        //...
        //Thêm
        "preLaunchTask": "C/C++: g++.exe build active file"
    }
]

Các bạn có thể copy đoạn code sau:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(Windows) Launch",
            "type": "cppvsdbg",
            "request": "launch",
            "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${fileDirname}",
            "environment": [],
            "console": "externalTerminal",
            "preLaunchTask": "C/C++: g++.exe build active file"
        }
    ]
}

Ctrl + S để lưu, quay trở lại file HelloWord.cpp nhấn F5 một lần nữa. Chương trình đã chạy trên Console.

Lưu ý: Nếu bạn muốn chạy chương trình ngay trong Terminal thì sửa lại dòng "console": "externalTerminal", thành "console": "integratedTerminal",.

Leave a Reply

This site uses cookies to offer you a better browsing experience. By browsing this website, you agree to our use of cookies.