简体   繁体   中英

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

I am trying to create a sample project for myself using Plog library. 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:

${default}
${workspaceFolder}/**

and this is my main 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

which plog/Logger.h is referenced by log.h in the main function. plog folder which contains All headers of plog library is located in my project root folder. 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}?

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 . Add in

$(PLOG)

This will pick up your environment variable PLOG. In your code

#include "plog/Log.h"

VS will look in $(PLOG) for 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

Next go to the linker section of properties. Here, you will see Additional Library Directories . Add $(PLOG)/lib. This is where it can find plog.lib. If plog.lib lives in $(PLOG) then add $(PLOG). It really depends on what the directory structure is.

Next go to the Input property page . Under Additional Dependencies add 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).

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.

When you do #include "plog/Log.h" , it expects the plog folder to be a direct children of the include paths. As plog folder is a direct child of ${workspaceFolder} directory, this should work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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