小编典典

VS Code不会使用多个.ccp源文件构建c ++程序

linux

请注意,我在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
}

有一次,我在想,如果我需要在里面加上一个双指令告诉编译器建立在两个.ccp文件 tasks.json
但我没有设法弄清楚如何做到这一点,或者即使这是正确的方法。

task.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或VSCommunity中完成本教程,是因为我想知道大多数情况下发生了什么。另外,我想让VS Code为我工作,因为到目前为止效果很好。


阅读 788

收藏
2020-06-07

共1个答案

小编典典

如果您有多个文件,而一个文件依赖cpp另一个文件,则需要告诉g++也对其进行编译,以便链接程序可以找到它。最简单的方法是:

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

附带说明一下,您可能应该最小程度地-Wall,可能-Wextra和可能地使用警告进行编译-Wpedantic,以使您知道自己正在做的事情是否有问题。

2020-06-07