声明:Ubuntu 16 VSCode C++
上图工程文件目录:包括 CMakeLists.txt, src(存放.cpp文件), include(存放.h文件), bin(生成的可执行文件),lib(存放库文件), Main(Main.cpp),buid
1 首先打开空的文件夹
新建如上文件夹
CMakelists.txt中写:
cmake_minimum_required(VERSION 3.0.0)
project(Arry)
# 基本的
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g")
set(CMAKE_CXX_FLAGS " -std=c++11 ")
set(CMAKE_CXX_FLAGS_RELEASE " -O3 ") # -std=c++11 ")
# 执行文件输出位置
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
# 库文件生成位置
set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)
################ 依赖
# 找到OpenCV
find_package(OpenCV 3.3.0 REQUIRED)
# 链接进来
include_directories(${OpenCV_INCLUDE_DIRS})
# 定义cpp文件需要的库文件依赖项
set(LIBSSS
${OpenCV_LIBS})
# 添加头文件位置
include_directories(${PROJECT_SOURCE_DIR}/include)
# 添加src
add_subdirectory(src)
# 添加main位置
add_subdirectory(Main)
src中的CMakeLists.txt写:
# 这样就能生成.so的共享库
add_library(Arry SHARED arry.cpp)
#有更多的就在arry.cpp后继续加xx.cpp
# 链接所需要的东西
target_link_libraries(Arry ${LIBSSS})
Main文件夹中的CMakeLists.txt写:
# 生成可执行文件 名字:test
add_executable(main main.cpp)
# 链接库文件
target_link_libraries(main Arry ${LIBSSS})
① ctrl + shift + p 选择CMake::Quick Start, 选择GCC 5.5.0(5.4也行)提示输入工程名字:, 然后根据自己目的选择library 或者 exe;这时候会创建CMakeLists.txt
② ctrl + shift + p Task::Configure Task, 选择默认模板选择test,这时候创建task.json,
将这一段代码放入:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"options": {
"cwd": "${workspaceRoot}/build"
},
"tasks": [
{
"label": "cmake",
"type": "shell",
"command": "cmake",
"args": ["-G", "Unix Makefiles", "-DCMAKE_BUILD_TYPE=Debug", ".."]
},
{
"label": "make",
"group":{
"kind":"build",
"isDefault":true
},
"type": "shell",
"command": "make",
"args": []
}
]
}
③ ctrl + shift + p 选择Task::Run Task,选择test,就开始编译。要不行,直接在终端cmake… make也行
④调试:在debug中选择创建lunch.json,写:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/bin/main", // 可执行文件的位置
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}