VS Code不会构建具有多个.ccp源文件的c ++程序

问题描述 投票:2回答:4

请注意,我在Ubuntu 17.10上使用VS Code并使用GCC编译器。

我在构建一个使用其他.ccp文件的简单程序时遇到了麻烦。我可能会遗漏一些明显的东西,因为我对编程很新,但我会解释到目前为止我做过的事情。这是阻止我继续我正在做的教程的事情。

我写了一个非常简单的程序来证明我的观点如下。

main.ccp


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

using namespace std;

int main ()
{

speak();

return 0;
}

Cat.h


#pragma once



void speak();

Cat.ccp


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

using namespace std;

void speak()
{
std::cout << "Meow!!" << std::endl;

}

这个简单的程序在Codeblocks和Visual Studio Community 2017中构建都没有问题,我无法弄清楚我需要做些什么才能让它运行。底部的此错误表示根本没有拾取Cat.ccp文件。如果我要将此Cat.ccp中的定义删除到头文件中,程序编译并运行正常,但我需要使用该.ccp文件,因为我知道这是分离代码的可接受方式。我会注意到所有文件也在同一个文件夹中。

我知道我可能需要告诉VS Code在哪里查找Cat.ccp文件,但我很奇怪它在同一位置找到了标题。然而,在看完网络后,我一直在阅读,我可能需要将目录放入c_cpp_intellisense_properties.json。这就是我的样子。

{
"configurations": [
    {
        "name": "Mac",
        "includePath": [
            "/usr/include",
            "/usr/local/include",
            "${workspaceRoot}"
        ],
        "defines": [],
        "intelliSenseMode": "clang-x64",
        "browse": {
            "path": [
                "/usr/include",
                "/usr/local/include",
                "${workspaceRoot}"
            ],
            "limitSymbolsToIncludedHeaders": true,
            "databaseFilename": ""
        },
        "macFrameworkPath": [
            "/System/Library/Frameworks",
            "/Library/Frameworks"
        ]
    },
    {
        "name": "Linux",
        "includePath": [
            "/usr/include/c++/7",
            "/usr/include/x86_64-linux-gnu/c++/7",
            "/usr/include/c++/7/backward",
            "/usr/lib/gcc/x86_64-linux-gnu/7/include",
            "/usr/local/include",
            "/usr/lib/gcc/x86_64-linux-gnu/7/include-fixed",
            "/usr/include/x86_64-linux-gnu",
            "/usr/include",
            "/home/danny/Documents/C++_Projects/24_-_Classes/Cat.cpp",
            "${workspaceRoot}",
            "/home/danny/Documents/C++_Projects/24_-_Classes/",
            "/home/danny/Documents/C++_Projects/24_-_Classes/.vscode",
            "/home/danny/Documents/C++_Projects/24_-_Classes/.vscode/Cat.cpp"
        ],
        "defines": [],
        "intelliSenseMode": "clang-x64",
        "browse": {
            "path": [
                "/usr/include/c++/7",
                "/usr/include/x86_64-linux-gnu/c++/7",
                "/usr/include/c++/7/backward",
                "/usr/lib/gcc/x86_64-linux-gnu/7/include",
                "/usr/local/include",
                "/usr/lib/gcc/x86_64-linux-gnu/7/include-fixed",
                "/usr/include/x86_64-linux-gnu",
                "/usr/include",
                "${workspaceRoot}",
                "/home/danny/Documents/C++_Projects/24_-_Classes/",
                "/home/danny/Documents/C++_Projects/24_-_Classes/.vscode/Cat.cpp"
            ],
            "limitSymbolsToIncludedHeaders": true,
            "databaseFilename": ""
        }
    },
    {
        "name": "Win32",
        "includePath": [
            "C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/include",
            "${workspaceRoot}"
        ],
        "defines": [
            "_DEBUG",
            "UNICODE"
        ],
        "intelliSenseMode": "msvc-x64",
        "browse": {
            "path": [
                "C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/include/*",
                "${workspaceRoot}"
            ],
            "limitSymbolsToIncludedHeaders": true,
            "databaseFilename": ""
        }
    }
],
"version": 3
}

有一次我想知道是否需要在那里添加一个双命令来告诉编译器在tasks.json中构建两个.ccp文件,但我还没弄清楚如何做到这一点,或者即使这是正确的做法。

tasks.json


{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
    {
        "label": "Build",
        "type": "shell",
        "command": "g++ -g /home/danny/Documents/C++_Projects/24_-_Classes/main.cpp -o Classes",
        "group": {
            "kind": "build",
            "isDefault": true
        },
        "problemMatcher":"$gcc"
    }
]
}

感谢任何帮助。如果你想知道,我之所以不完成Codeblocks或VS社区的教程是因为我想知道大多数事情的内幕是什么。另外,我想让VS Code为我工作,因为它到目前为止一直很棒。

c++ linux ubuntu gcc visual-studio-code
4个回答
1
投票

如果你有多个文件,一个依赖于cpp文件而另一个,你需要告诉g ++编译它,所以链接器可以找到它。最简单的方法是:

$ g++ Cat.cpp main.cpp -o Classes

作为旁注,你应该编译警告,最低限度-Wall,可能-Wextra,可能-Wpedantic,所以你知道你正在做的事情是否有问题。


1
投票

在VS Code程序的终端选项卡上写下这个 (请注意,您可能必须从设置激活才能通过终端而不是输出运行)

$ g ++ nameOne.cpp nameTwo.cpp -o a.out $ ./a.out


0
投票

如果文件扩展名是ccp或cpp,它会有所不同吗?


0
投票
"tasks": [
    {
        "label": "echo",
        "type": "shell",
        "command": "g++",
        "args":[
            "-g","main.cpp","cat.cpp"
        ],
        "group": {
            "kind": "build",
            "isDefault": true
        }
    }

只需在args中添加cat.cpp然后尝试运行即可。它应该在VS代码上运行没有错误

© www.soinside.com 2019 - 2024. All rights reserved.