简体   繁体   English

如何将更多文件夹添加到 c++ 包含路径?

[英]how can I add more folder to c++ include path?

I am trying to create a sample project for myself using Plog library.我正在尝试使用Plog库为自己创建一个示例项目。 according to its documentation, I must add its include folder to my include path.根据其文档,我必须将其包含文件夹添加到我的包含路径中。 SO, I add these Lines to C/C++ configuration in my vs-code:所以,我将这些行添加到我的 vs 代码中的 C/C++ 配置中:

${default} ${默认}
${workspaceFolder}/** ${工作区文件夹}/**

and this is my main function:这是我的主要 function:

#include <iostream>
#include "plog/Log.h"
int main() {
    plog::init(plog::debug, "log.txt");
    std::string name;
    std::cin >> name; 
    LOGD << "user entered name :" << name; 
    std::cout << name << std::endl; 
    return 0;
}

but when I run this code I am getting this error:但是当我运行这段代码时,我收到了这个错误:

fatal error: plog/Logger.h: No such file or directory致命错误:plog/Logger.h:没有这样的文件或目录

which plog/Logger.h is referenced by log.h in the main function.其中 plog/Logger.h 由主 function 中的 log.h 引用。 plog folder which contains All headers of plog library is located in my project root folder.包含所有plog库头文件的plog文件夹位于我的项目根文件夹中。 this is My folder structure:这是我的文件夹结构:

root
   |_plog
   |    |
   |    |_ all my header files *.h
   |_main.cpp 

Is there any more configuration which I missed?还有其他我错过的配置吗? or did I make a mistake in any step?还是我在任何步骤中都犯了错误?

Try尝试

#include <plog/Log.h>

Why do you have ** after ${workspaceFolder}?为什么在 ${workspaceFolder} 之后有 **?

First we need to know what the directory structure is.首先我们需要知道目录结构是什么。 For instance例如

+-- C:\
    +-- ProgramData
        +-- plog-dist
            +-- plog
                +-- Log.h
            +-- lib
                +-- plog.lib
            +-- bin
                +-- plog.dll
    +-- Users
        +-- Me
            +-- Documents
                +-- myproject

Let us assume that you already have an environment variable setup让我们假设您已经设置了环境变量

setx PLOG=C:\ProgramData\plog-dist

You can either manage this under property pages or you can set it on individual projects.您可以在属性页面下管理它,也可以在单个项目上设置它。 Let us just do the second method because it is easier and has less explaining.让我们只做第二种方法,因为它更容易并且解释更少。 Under C++ General Properties , the first line says Additional Include Directories .C++ General Properties下,第一行显示Additional Include Directories Add in加入

$(PLOG)

This will pick up your environment variable PLOG.这将获取您的环境变量 PLOG。 In your code在您的代码中

#include "plog/Log.h"

VS will look in $(PLOG) for plog/Log.h. VS 将在 $(PLOG) 中查找 plog/Log.h。 If the distribution is如果分布是

+-- ProgramData
    +-- plog
        +-- Log.h

And the environment variable is环境变量是

setx PLOG=C:\ProgramData\plog

Then you should just那么你应该只是

#include "Log.h"

VS will look in $(PLOG) for Log.h VS 将在 $(PLOG) 中查找 Log.h

Next go to the linker section of properties.接下来 go 到 linker 部分的属性。 Here, you will see Additional Library Directories .在这里,您将看到Additional Library Directories Add $(PLOG)/lib.添加 $(PLOG)/lib. This is where it can find plog.lib.这是它可以找到 plog.lib 的地方。 If plog.lib lives in $(PLOG) then add $(PLOG).如果 plog.lib 位于 $(PLOG) 中,则添加 $(PLOG)。 It really depends on what the directory structure is.这实际上取决于目录结构是什么。

Next go to the Input property page .接下来 go 到Input 属性页 Under Additional Dependencies add plog.lib .附加依赖项下添加plog.lib

If you set up the PLOG variable and type tree /f %PLOG% from the cmd line, it will tell you where everything is relative to $(PLOG).如果您设置 PLOG 变量并从 cmd 行中键入tree /f %PLOG% ,它将告诉您所有内容与 $(PLOG) 的关系。

Assuming that below is your folder hierarchy:假设以下是您的文件夹层次结构:

root
   |_plog
   |    |
   |    |_ Init.h
   |    |_ Log.h
   |    |_ Init.h
   |    |_ Other plog's *.h files
   |_main.cpp 

In that case, adding ${workspaceFolder} to your include paths must suffice.在这种情况下,将${workspaceFolder}添加到包含路径就足够了。

When you do #include "plog/Log.h" , it expects the plog folder to be a direct children of the include paths.当您执行#include "plog/Log.h"时,它期望plog文件夹是包含路径的直接子级。 As plog folder is a direct child of ${workspaceFolder} directory, this should work.由于plog文件夹是${workspaceFolder}目录的直接子目录,因此应该可以。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM