简体   繁体   中英

Visual Studio Code C++ include path

We're reasonably experienced C++ developers. We've used Visual Studio for years, but we absolutely cannot get Visual Studio Code to look in our external header file folders.

We can compile and run a simple, one file C++ program, but as soon as we try to include an external header, it gives us the dreaded "No such file or directory" error.

We have the Microsoft C/C++ Extension installed. We have the g++ compiler installed and in our path. And, like I said, it compiles a single file C++ program fine. We didn't do anything fancy:

只是源头的开始

We added the path to the external header in the c_cpp_properties.json file by hand:

"includePath": [
    "${workspaceFolder}/**",
    "C:/special_api/**"
],

We got the error. Then we used the C/C++ Configurations GUI:

它在 C/C++ 配置 GUI 中的外观

We got the error.

We switched the slashes around, since Windows sometimes has issues with that:

"includePath": [
    "${workspaceFolder}/**",
    "C:\\special_api\\**"
],

But no matter what we do, we get this error:

C:\Work Area\cpp-experiment\HelloWorld.cpp:2:10: fatal error: experiment.h: No such file or directory
#include "experiment.h"
      ^~~~~~~~~~~~~~
compilation terminated.

Build finished with error(s).
The terminal process terminated with exit code: -1.

And I don't even see where it's specifying the include folder in the compilation command:

C:\Tools\x86_64-8.1.0-win32-seh-rt_v6-rev0\mingw64\bin\g++.exe -g "C:\Work Area\cpp-experiment\HelloWorld.cpp" -o "C:\Work Area\cpp-experiment\HelloWorld.exe"

Though I have to admit, I'm not a C++ command-line compilations expert; I usually just use the IDE and let it handle the complex command.

We know the header file is valid because when we moved it to the same folder as the.cpp file, it worked fine. VSC just can't find it when it's in the external folder.

Of course we've read the docs, but they're less than helpful for this particular issue. Does anyone know how to get Visual Studio Code to recognize external header file paths?

You need to write a task to do the build. The task will normally run a makefile where you specify the build option, including the compiler search paths. VSCODE does not do this for you.

task example

    {
        "label": "make PDG",
        "options": {
            "cwd": "${workspaceFolder}/../../PelexDataGraph"
        },
        "command": "mingw32-make.exe",
        "args": [
            "pdg"
        ],
        "group": "build",
        "problemMatcher": [
            "$gcc"
        ]
    },

makefile example

#source file search paths
VPATH = ../../src ../../src/ext ../../PeakParams2 \
    ../../PelexDataGraph

# compiler include search paths
INCS=-I../../src -I../../src/ext \
    -I../../pelexMixer \
    -I../../../windex/include -I../../../boost/boost1_72

# libraries required by linker
LIBS=-lstdc++fs  -lgdiplus -lgdi32 -lcomdlg32 -lws2_32 -lwsock32

# folder for .o files
ODIR=./obj

# PPE sources
_OBJppe = PeakParameterEditor.o \
cPPEForm.o cValues.o cChannelFilterEditor.o \
cDetectorConfig.o cGroup.o cMixerUIClient.o \
Widgets.o \
Configure.o cPeakFilterSet.o ChannelLabels.o \
raven_sqlite.o sqlite3.o
OBJppe = $(patsubst %,$(ODIR)/%,$(_OBJppe))

# PDG sources
_OBJpdg = PelexDataGraph.o
OBJpdg = $(patsubst %,$(ODIR)/%,$(_OBJpdg))

$(ODIR)/sqlite3.o: sqlite3.c
    gcc  -c -o $@ $<
$(ODIR)/%.o: %.cpp
    g++   -std=c++17 -c -o $@ $< $(INCS)

ppe: $(OBJppe) 
    g++ -o ../../bin/PeakParameterEditor.exe $^ $(INCS) $(LIBS)

pdg: $(OBJpdg) 
    g++ -o ../../bin/PelexDataGraph.exe $^ $(LIBS)

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