美文网首页
Vscode C/C++ make project

Vscode C/C++ make project

作者: zxlele_c763 | 来源:发表于2021-03-24 21:05 被阅读0次

vscode make C++ project

Used this toolkit in vscode

C/C++ Project Generator

Requirements

  • If you are on linux you must install gcc and make
  • If you are on window you must install mingw

How to use

  • Go to command pallete (usually : ctrl + shift + p)
  • Search for "Create C project" or "Create c++ project" depending on your preference
  • Select the folder where the project should be created
  • That's it, project will open
modify makefile

add source , include and library files in makefile.

# define source directory
SRC     := src
LEETCODESOURCE  := leetcodeSource

# define include directory
INCLUDE := include
LEETCODEINCLUDE := leetcodeInclude

# define lib directory
LIB     := lib

ifeq ($(OS),Windows_NT)
MAIN    := main.exe
SOURCEDIRS  := $(SRC) $(LEETCODESOURCE)
INCLUDEDIRS := $(INCLUDE) $(LEETCODEINCLUDE)
LIBDIRS     := $(LIB)
FIXPATH = $(subst /,\,$1)
RM          := del /q /f
MD  := mkdir
else
MAIN    := main
SOURCEDIRS  := $(shell find $(SRC) $(LEETCODESOURCE) -type d)
INCLUDEDIRS := $(shell find $(INCLUDE) $(LEETCODEINCLUDE) -type d)
LIBDIRS     := $(shell find $(LIB) -type d)
FIXPATH = $1
RM = rm -f
MD  := mkdir -p
endif

# define any directories containing header files other than /usr/include
INCLUDES    := $(patsubst %,-I%, $(INCLUDEDIRS:%/=%))

# define the C libs
LIBS        := $(patsubst %,-L%, $(LIBDIRS:%/=%))

# define the C source files

windows make file reference

#
# 'make'        build executable file 'main'
# 'make clean'  removes all .o and executable files
#

# define the Cpp compiler to use
CXX = g++

# define any compile-time flags
CXXFLAGS    := -std=c++17 -Wall -Wextra -g

# define library paths in addition to /usr/lib
#   if I wanted to include libraries not in /usr/lib I'd specify
#   their path using -Lpath, something like:
LFLAGS =

# define output directory
OUTPUT  := output

# define source directory
SRC     := src
LEETCODESRC := leetcode\\source

# define include directory
INCLUDE := include
BOOSTINCLUDE := C:\\My_Documents\\D_backup\\CPP_Test\\boost\\boost_1_66_0\\boost_1_66_0
LEETCODEINCLUDE := leetcode\\include

# define lib directory
LIB     := lib
BOOSTLIB := C:\\My_Documents\\D_backup\\CPP_Test\\boost\\boost_1_66_0\\boost_1_66_0\\libs

ifeq ($(OS),Windows_NT)
MAIN    := main.exe
SOURCEDIRS  := $(SRC) $(LEETCODESRC)
INCLUDEDIRS := $(INCLUDE) $(BOOSTINCLUDE) $(LEETCODEINCLUDE)
LIBDIRS     := $(LIB) $(BOOSTLIB)
FIXPATH = $(subst /,\,$1)
RM          := del /q /f
MD  := mkdir
else
MAIN    := main
SOURCEDIRS  := $(shell find $(SRC) -type d)
INCLUDEDIRS := $(shell find $(INCLUDE) -type d)
LIBDIRS     := $(shell find $(LIB) -type d)
FIXPATH = $1
RM = rm -f
MD  := mkdir -p
endif

# define any directories containing header files other than /usr/include
INCLUDES    := $(patsubst %,-I%, $(INCLUDEDIRS:%/=%))

# define the C libs
//LIBS      := $(patsubst %,-L%, $(LIBDIRS:%/=%), C:\\My_Documents\\D_backup\\CPP_Test\\boost\\boost_1_66_0\\boost_1_66_0\\libs)

# define the C source files
SOURCES     := $(wildcard $(patsubst %,%/*.cpp, $(SOURCEDIRS)))

# define the C object files 
OBJECTS     := $(SOURCES:.cpp=.o)

#
# The following part of the makefile is generic; it can be used to 
# build any executable just by changing the definitions above and by
# deleting dependencies appended to the file from 'make depend'
#

OUTPUTMAIN  := $(call FIXPATH,$(OUTPUT)/$(MAIN))

all: $(OUTPUT) $(MAIN)
    @echo Executing 'all' complete!

$(OUTPUT):
    $(MD) $(OUTPUT)

$(MAIN): $(OBJECTS) 
    $(CXX) $(CXXFLAGS) $(INCLUDES) -o $(OUTPUTMAIN) $(OBJECTS) $(LFLAGS) $(LIBS)

# this is a suffix replacement rule for building .o's from .c's
# it uses automatic variables $<: the name of the prerequisite of
# the rule(a .c file) and $@: the name of the target of the rule (a .o file) 
# (see the gnu make manual section about automatic variables)
.cpp.o:
    $(CXX) $(CXXFLAGS) $(INCLUDES) -c $<  -o $@

.PHONY: clean
clean:
    $(RM) $(OUTPUTMAIN)
    $(RM) $(call FIXPATH,$(OBJECTS))
    @echo Cleanup complete!

run: all
    ./$(OUTPUTMAIN)
    @echo Executing 'run: all' complete!

compile and debug , clean, run project

  • run task make can compile whole project
  • run debug can start gdb debug
  • run task clean, clean the make project
  • Terminal-->RunTask choice clean/build/build & run
图片.png

相关文章

网友评论

      本文标题:Vscode C/C++ make project

      本文链接:https://www.haomeiwen.com/subject/ahzhhltx.html