lundi 9 août 2021

Not able set include path in VS Code for C++

I am new to VS Code and C++ and running into strange problem. I have searched all across and not able to solve. I am including a file from my own src/include folder. I do get fatal error but the code compiles fine if I added the include path manually to Makefile! Somehow I am not able to set the path correctly for preprocessing step but at the time of compilation it just goes fine. Below is the snippet of compile output and other structure:

**$ make**
src/main2.cpp:1:10: fatal error: 'main2.h' file not found
#include "main2.h"
         ^~~~~~~~~
1 error generated.
src/main.cpp:2:10: fatal error: 'main2.h' file not found
#include "main2.h"
         ^~~~~~~~~
1 error generated.
g++ -Isrc/include -g -O0 -Wall -Wextra -Wshadow -Wnon-virtual-dtor -pedantic -o obj/main.o -c src/main.cpp
g++ -Isrc/include -g -O0 -Wall -Wextra -Wshadow -Wnon-virtual-dtor -pedantic -o obj/main2.o -c src/main2.cpp
g++ -g -O0 -Wall -Wextra -Wshadow -Wnon-virtual-dtor -pedantic -o myapp obj/main.o obj/main2.o 

**$ ./myapp** 
Hello, world!
Inside test!!

**src/main.cpp**

#include <iostream>
#include "main2.h"

int main() 
{
    std::cout<<"Hello, world!" << std::endl;
    test();
    return 0;
}

**src/main2.cpp**

#include "main2.h"
#include <iostream>

void test(void)
{
    std::cout<<"Inside test!!"<<std::endl;
}

**src/include/main2.h**

#ifndef MAIN2_H
#define MAIN2_H

#include <iostream>

void test(void);

#endif

If I dont add below line in my Makefile then I am not able to run and build as in prev. step

INC=-I$(SRCDIR)/include

# Building rule for .o files and its .c/.cpp in combination with all .h
$(OBJDIR)/%.o: $(SRCDIR)/%$(EXT)
    $(CC) $(INC) $(CXXFLAGS) -o $@ -c $<

$ make
src/main2.cpp:1:10: fatal error: 'main2.h' file not found
#include "main2.h"
         ^~~~~~~~~
1 error generated.
src/main.cpp:2:10: fatal error: 'main2.h' file not found
#include "main2.h"
         ^~~~~~~~~
1 error generated.
g++  -g -O0 -Wall -Wextra -Wshadow -Wnon-virtual-dtor -pedantic -o obj/main.o -c src/main.cpp
src/main.cpp:2:10: fatal error: 'main2.h' file not found
#include "main2.h"
         ^~~~~~~~~
1 error generated.
make: *** [obj/main.o] Error 1

Now I did add lot of things in my c_cpp_properties.json and global settings.json as below but nothing seems to work!

c_cpp_properties.json

{
  "configurations": [
    {
      "name": "macos-gcc-x64",
      "includePath": [
        "${workspaceFolder}/**",
        "${workspaceRoot}/**",
        "${default}"
      ],
      "compilerPath": "/usr/bin/clang",
      "cStandard": "${default}",
      "cppStandard": "c++11",
      "intelliSenseMode": "macos-gcc-x64",
      "compilerArgs": [
        "-Wall",
        "-Wextra",
        "-Wpedantic"
      ],
      "browse": {
        "path": [
            "${workspaceRoot}",
            "${workspaceFolder}"
        ],
        "limitSymbolsToIncludedHeaders": true,
        "databaseFilename": ""
    }
    }
  ],
  "version": 4
}

settings.json

    "C_Cpp.default.includePath": [
        "${default}",
        "${workspaceRoot}/src/include",
        "${workspaceFolder}/**"
    ],
    "C_Cpp.default.forcedInclude": [ "{default}", "${workspaceRoot}/**" ]

I am using VS Code on Mac OS! Would appreciate any help in this regard.

Below is my makefile

########################################################################
####################### Makefile Template ##############################
########################################################################

# Compiler settings - Can be customized.
CC = g++
CXXFLAGS = -g -O0 -std=c++11 -Wall -Wextra -Wshadow -Wnon-virtual-dtor -pedantic
LDFLAGS = 

# Makefile settings - Can be customized.
APPNAME = myapp
EXT = .cpp
SRCDIR = src
OBJDIR = obj

############## Do not change anything from here downwards! #############
SRC = $(wildcard $(SRCDIR)/*$(EXT))
OBJ = $(SRC:$(SRCDIR)/%$(EXT)=$(OBJDIR)/%.o)
DEP = $(OBJ:$(OBJDIR)/%.o=%.d)
# UNIX-based OS variables & settings
RM = rm
DELOBJ = $(OBJ)
# Windows OS variables & settings
DEL = del
EXE = .exe
WDELOBJ = $(SRC:$(SRCDIR)/%$(EXT)=$(OBJDIR)\\%.o)

INC=-I$(SRCDIR)/include


########################################################################
####################### Targets beginning here #########################
########################################################################

all: $(APPNAME)

# Builds the app
$(APPNAME): $(OBJ)
    $(CC) $(CXXFLAGS) -o $@ $^ $(LDFLAGS)

# Creates the dependecy rules
%.d: $(SRCDIR)/%$(EXT)
    @$(CPP) $(CFLAGS) $< -MM -MT $(@:%.d=$(OBJDIR)/%.o) >$@

# Includes all .h files
-include $(DEP) 



# Building rule for .o files and its .c/.cpp in combination with all .h
$(OBJDIR)/%.o: $(SRCDIR)/%$(EXT)
    $(CC) $(INC) $(CXXFLAGS) -o $@ -c $<

################### Cleaning rules for Unix-based OS ###################
# Cleans complete project
.PHONY: clean
clean:
    $(RM) $(DELOBJ) $(DEP) $(APPNAME)

# Cleans only all files with the extension .d
.PHONY: cleandep
cleandep:
    $(RM) $(DEP)

#################### Cleaning rules for Windows OS #####################
# Cleans complete project
.PHONY: cleanw
cleanw:
    $(DEL) $(WDELOBJ) $(DEP) $(APPNAME)$(EXE)

# Cleans only all files with the extension .d
.PHONY: cleandepw
cleandepw:
    $(DEL) $(DEP)

Aucun commentaire:

Enregistrer un commentaire