我应该如何配置我的task.json文件以在VS Code中编译多个.cpp文件?

时间:2020-02-05 09:45:49

标签: visual-studio-code c++17

我在 tasks.json 文件中尝试了以下选项:

"args": [
            "-g",
            "${fileDirname}\\*.cpp",
            "-o",
            "${fileDirname}\\${fileBasenameNoExtension}.exe"
        ]

它没有用。我还尝试了${workspaceFolder}而不是${fileDirname},但是它也不起作用。我知道我可以手动将 .cpp 文件添加到我的 tasks.json 文件中,但是我不想对其进行硬编码。还有其他可行的选择吗?

2 个答案:

答案 0 :(得分:0)

VSCode的“任务”系统不是构建系统,它应该运行外部构建系统,而不是直接运行编译器。您应该为项目确定一个实际的构建系统(例如CMake,make,msbuild等),并进行适当配置,然后配置VSCode来运行它。

答案 1 :(得分:0)

这是我的代码:

main.cpp

#include <iostream>
#include "Player.hpp"

void display_active_players() {
    std::cout << "Active players: " << Player::get_num_players() << std::endl;
}

int main() {
    display_active_players();
    Player hero {"Hero"};
    display_active_players();

    {
        Player frank {"Frank"};
        display_active_players();
    }
    display_active_players();

    Player *enemy = new Player {"Enemy", 100, 100};
    display_active_players();
    delete enemy;
    display_active_players();

    std::cout << std::endl;
    return 0;
}

Player.cpp

#include "Player.hpp"

int Player::num_players {0};

Player::Player(std::string name_val, int health_val, int xp_val) 
    : name {name_val}, health {health_val}, xp {xp_val} {
        num_players++;
}

Player::Player(const Player &source) 
    : Player {source.name, source.health, source.xp} {
}

Player::~Player() {
    num_players--;
}

int Player::get_num_players() {
    return num_players;
}

Player.hpp

#ifndef PLAYER_H
#define PLAYER_H

#include <string>

class Player {
    private:
        static int num_players;
        std::string name;
        int health;
        int xp;
    public:
        std::string get_name() { return name; }
        int get_health() { return health; }
        int get_xp() { return xp; }
        Player(std::string name_val = "None", int health_val = 0, int xp_val = 0);
        // Copy constructor
        Player(const Player &source);
        // Destructor
        ~Player();
        static int get_num_players();
};

#endif

这是我尝试编译时出现的错误:

c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\miami\AppData\Local\Temp\ccUfdvjk.o:main.cpp:(.text+0x1e): undefined reference to `Player::get_num_players()'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\miami\AppData\Local\Temp\ccUfdvjk.o:main.cpp:(.text+0xaa): undefined reference to `Player::Player(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, int, int)'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\miami\AppData\Local\Temp\ccUfdvjk.o:main.cpp:(.text+0x10e): undefined reference to `Player::Player(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, int, int)'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\miami\AppData\Local\Temp\ccUfdvjk.o:main.cpp:(.text+0x137): undefined reference to `Player::~Player()'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\miami\AppData\Local\Temp\ccUfdvjk.o:main.cpp:(.text+0x18c): undefined reference to `Player::Player(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, int, int)'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\miami\AppData\Local\Temp\ccUfdvjk.o:main.cpp:(.text+0x1b9): undefined reference to `Player::~Player()'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\miami\AppData\Local\Temp\ccUfdvjk.o:main.cpp:(.text+0x1f4): undefined reference to `Player::~Player()'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\miami\AppData\Local\Temp\ccUfdvjk.o:main.cpp:(.text+0x24a): undefined reference to `Player::~Player()'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\miami\AppData\Local\Temp\ccUfdvjk.o:main.cpp:(.text+0x28d): undefined reference to `Player::~Player()'
collect2.exe: hata: ld ��k�� durumu 1 ile d�nd�