vscode 开发c++

 

makefile.mk

#makefile.mk 公共头文件
ifndef TARGET
#  /root/make/src/test_include
# notdir
TARGET:=$(notdir $(shell pwd)) #test_include
endif
CXXFLAGS:=$(CXXFLAGS) -g  -std=c++11  -Iinclude#g++ -c 编译 自动推导
LDFLAGS:=$(LDFLAGS) #链接 可用于自动推导
LDLIBS:=$(LDLIBS) -lpthread #链接库 用于自动推导
#输出路径 /usr/bin  /usr/lib /usr/include
OUT:=/usr

SRCS:=$(wildcard *.cpp *.cc *.c) #test_include.cpp testcpp.cc testc.c
OBJS:=$(patsubst %.cpp,%.o,$(SRCS)) #test_include.o testcpp.cc testc.c
OBJS:=$(patsubst %.cc,%.o,$(OBJS))
OBJS:=$(patsubst %.c,%.o,$(OBJS)) #test_include.o testcpp.o testc.o

#区分动态库 静态库 和执行程序
ifeq ($(LIBTYPE),.so) #动态库 $(strip $(TARGET)) 去掉前后空格\t 
    TARGET:=lib$(strip $(TARGET)).so  
    LDLIBS:=$(LDLIBS) -shared
    CXXFLAGS:=$(CXXFLAGS) -fPIC
endif
ifeq ($(LIBTYPE),.a) #静态库 
    TARGET:=lib$(strip $(TARGET)).a  
endif

#启动脚本
STARTSH=start_$(TARGET)
#停止脚本
STOPSH=stop_$(TARGET)

# $(1) TARGET ,$(2)  OUT
define Install 
    @echo "begin install "$(1)
    -mkdir -p $(2)
    cp $(1) $(2)
    @echo "end install "$(1)
endef

#生成启动停止脚本,并安装到$(OUT)
# $(1) TARGET ,$(2)  OUT,$(3) $PARAS
define InstallSH
    @echo "begin make start shell"
    echo "nohup $(1) $(3)  &" > $(STARTSH)
    chmod +x $(STARTSH)
    cp $(STARTSH) $(2)
    @echo "end make start shell"

    @echo "begin make stop shell"
    echo killall $(1) >$(STOPSH)
    cp $(STOPSH) $(2)
    @echo "end make stop shell"
endef
all:depend $(TARGET)
depend:
    @$(CXX) $(CXXFLAGS) -MM $(SRCS) >.depend
-include .depend

#目标生成
$(TARGET):$(OBJS)
ifeq ($(LIBTYPE),.a) #静态库 
    $(AR) -cvr $@ $^
else
    $(CXX) $(CXXFLAGS) $(LDFLAGS) $^ -o $@  $(LDLIBS)
endif


#安装程序和库
install:$(TARGET)
ifdef LIBTYPE
    $(call Install,$(TARGET),$(OUT)/lib)
    $(call Install,*.h,$(OUT)/include)
else
    $(call Install,$(TARGET),$(OUT)/bin)
    $(call InstallSH,$(TARGET),$(OUT)/bin)
endif

#卸载程序和库
uninstall:clean
ifndef LIBTYPE
    -$(STOPSH)
    $(RM) $(OUT)/bin/$(TARGET)
    $(RM) $(OUT)/bin/$(STARTSH)
    $(RM) $(OUT)/bin/$(STOPSH)
else
    $(RM) $(OUT)/lib/$(TARGET)
endif
    

#rm -r test.o test
#目标清理
clean:
    $(RM) $(OBJS)  $(TARGET) .depend

.PHONY: clean  uninstall install  all depend #伪目标 没有对应的文件

 

 

makefile

#sCXXFLAGS:=-Iinclude 
# LDFLAGS:=-L../xthread  -L../xcom
# LDLIBS:=-lxthread -lxcom
include  makefile.mk #统一的makefile头

 

tasks.json

{
    "tasks": [
        {
            "label": "build_debug", // 任务名称,调试时可以指定不用任务进行处理
            "type": "shell", // [shell, process], 定义任务作为作为进程运行还是在shell中作为命令运行; (测试没看出啥区别...)
            "command": "make all", // 要执行的命令,可以是外部程序或者是shell命令。这里使用make编译命令
            "problemMatcher": [ // 要使用的问题匹配程序。可以是一个字符串或一个问题匹配程序定义,也可以是一个字符串数组和多个问题匹配程序。
                "$gcc"
            ],
            "group": { // 定义此任务属于的执行组。它支持 "build" 以将其添加到生成组,也支持 "test" 以将其添加到测试组。
                "kind": "build",
                "isDefault": true
            },
            "presentation": { // 配置用于显示任务输出并读取其输入的面板
                "echo": true, // 控制是否将执行的命令显示到面板中。默认值为“true”。
                "reveal": "always", // 控制运行任务的终端是否显示。可按选项 "revealProblems" 进行替代。默认设置为“始终”。
                "focus": false, // 控制面板是否获取焦点。默认值为“false”。如果设置为“true”,面板也会显示。
                "panel": "shared", // 控制是否在任务间共享面板。同一个任务使用相同面板还是每次运行时新创建一个面板。
                "showReuseMessage": true, // 控制是否显示“终端将被任务重用,按任意键关闭”提示
                "clear": false // 运行前清屏
            }
        },
        {
            "label": "build_release",
            "type": "shell",
            "command": "make all",
            "args": ["CFLAGS = -O2"], // 编译参数, 替换makefile中让CFLAGS字段
            "dependsOn":["build_clean"], // 指定依赖让task, 即会先执行build_clean,然后再执行build_release
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        },
        {
            "label": "build_clean",
            "type": "shell",
            "command": "make",
            "args": ["clean"], // 相当于执行 make clean命令
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ],
    "version": "2.0.0"
}

 

 

launch.json

 

{
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
       
        {
            "name": "(gdb) Lauch", // 启动配置的下拉菜单中显示的名称
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/Demo1", // 将要进行调试的程序的路径, workspaceFolder指当前工作目录(即vscode打开的目录:hello),main指的是makefile编译后目标码(可执行程序)的名字
            "args": [], // 程序启动的参数
            "stopAtEntry": false, // 设置true时,程序将暂停在程序入口处, 即main()的第一个{位置
            "cwd": "${workspaceFolder}", // 调试时的工作目录
            "environment": [],
            "externalConsole": false, // 调试时,是否显示控制台串口
            "MIMode": "gdb", // 调试命令
            "setupCommands": [
                {
                    "description": "为 gdb 启用整齐打印",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                },
                {
                    "description": "将反汇编风格设置为 Intel",
                    "text": "-gdb-set disassembly-flavor intel",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "build_debug", // 使用哪个任务进行编译,需要指定tasks.json中的一个,这里选择用build_debug任务进行编译
            "miDebuggerPath": "/usr/bin/gdb" // 调试命令的路径
        }
    ]
}

 

posted @ 2023-01-31 19:42  誓鼎  阅读(73)  评论(0编辑  收藏  举报