VS Code 不会使用多个 .ccp 源文件构建 C++ 程序 [英] VS Code will not build c++ programs with multiple .ccp source files

查看:25
本文介绍了VS Code 不会使用多个 .ccp 源文件构建 C++ 程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Note that I'm using VS Code on Ubuntu 17.10 and using the GCC Compiler.

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

I'm having trouble building a simple program which makes use of additional .ccp files. I'm probably missing something obvious here as I'm fairly new to programming but I'll explain what I've done so far. This is something that is stopping me from continuing with a tutorial I'm doing.

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

I have written a very simple program to demonstrate my point as follows.

#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 文件,因为我知道这是分离代码的可接受方式.我会注意到所有文件也都在同一个文件夹中.

This simple program builds in both Codeblocks and Visual Studio Community 2017 no problem and I can't figure out what I need to do to make it run. This error at the bottom indicates that the Cat.ccp file is not being picked up at all. If I was to drop the definition from this Cat.ccp into the header file the program compiles and runs fine but I need to make use of that .ccp file as I understand this is the accepted way of separating code. I'll note that all the files are in the same folder too.

我知道我可能需要告诉 VS Code 在哪里查找 Cat.ccp 文件,但我很奇怪它在同一位置找到了标头.尽管如此,在网上查看后,我一直在阅读我可能需要将一个目录放入 c_cpp_intellisense_properties.json.这是我的样子.

I understand that I may need to tell VS Code where to look for the Cat.ccp file but it's strange to me that it finds the header in the same location. Nevertheless, after looking online I've been reading that I may need to place a directory into the c_cpp_intellisense_properties.json. Here's what mine looks like.

{
"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 文件,但我还没有弄清楚如何这样做,或者即使这是正确的方法.

At one point I wondered if I need to add a double command in there to tell the compiler to build both .ccp files in the tasks.json but I haven't managed to figure out how to do that, or even if that's the right approach.

{
// 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 为我工作,因为到目前为止它一直很棒.

Appreciate any help. And just in case you're wondering, the reason I don't just finish the tutorial in Codeblocks or VS Community is that I like to know what's going on under the hood in most things. Plus I'd like to get VS Code working for me as it's been great so far.

推荐答案

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

If you have multiple files and one depends on a cpp file for another, you need to tell g++ to compile it as well, so the linker can find it. The simplest way would be:

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

<小时>

作为旁注,您可能应该编译时带有警告,至少是 -Wall,可能是 -Wextra,可能还有 -Wpedantic,这样你就知道你正在做的事情是否有问题.


As a side note, you should probably compile with warnings, minimally -Wall, likely -Wextra, and possibly -Wpedantic, so you know if something you're doing is problematic.

这篇关于VS Code 不会使用多个 .ccp 源文件构建 C++ 程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆