Ubuntu使用cmake和vscode开发自己的项目,引用自己的头文件和openCV

创建文件夹

复制代码
mkdir my_proj

继续创建include 和 src文件夹,形成如下的目录结构

用vscode打开项目

创建add.h

cpp 复制代码
#ifndef ADD_H
#define ADD_H
 
int add(int numA, int numB);
 
#endif

add.cpp

cpp 复制代码
#include "add.h"
 
int add(int numA, int numB)
{
    return numA + numB;
}

main.cpp

cpp 复制代码
#include <iostream>
#include "add.h"
 
int main()
{
   std::cout << "numA + numB = " << add(10,20) <<std::endl;
   return 0;
}

形成这样的目录结构

在my_proj中创建CMakeLists.txt,写入如下内容

cpp 复制代码
cmake_minimum_required(VERSION 2.8)

project(test)

set( CMAKE_BUILD_TYPE "Debug" )

include_directories(./include)

set(SRC_LIST ./src/add.cpp ./src/main.cpp)

add_executable(main ${SRC_LIST})

BUILD_TYPR 设置为Debug可以打断点调试

在my_proj中创建build.sh,写入如下内容

cpp 复制代码
mkdir build
cd build
cmake ..
make

修改build.sh文件的权限

cpp 复制代码
chmod +x build.sh

运行build.sh

在build文件夹中,会生成一个main的可执行程序,将它的路径记下来

在vscode中ctrl+shift+d 创建一个launch.json,将刚才的main的可执行程序的路径复制到"program":

cpp 复制代码
{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "(gdb) Launch",
      "type": "cppdbg",
      "request": "launch",
      "program": "/home/robotics/my_proj/build/main",
      "args": [],
      "stopAtEntry": false,
      "cwd": "${fileDirname}",
      "environment": [],
      "externalConsole": false,
      "MIMode": "gdb",
      "setupCommands": [
          {
              "description": "Enable pretty-printing for gdb",
              "text": "-enable-pretty-printing",
              "ignoreFailures": true
          },
          {
              "description": "Set Disassembly Flavor to Intel",
              "text": "-gdb-set disassembly-flavor intel",
              "ignoreFailures": true
          }
      ]
    }
  ]
}

c_cpp_properties.json这样写

cpp 复制代码
{
    "configurations": [
        {
            "name": "linux-gcc-x64",
            "includePath": [
                "${workspaceFolder}/**",
                "${workspaceFolder}/include"
            ],
            "compilerPath": "/usr/bin/gcc",
            "cStandard": "${default}",
            "cppStandard": "${default}",
            "intelliSenseMode": "linux-gcc-x64",
            "compilerArgs": [
                ""
            ]
        }
    ],
    "version": 4
}

然后就可以打上断点调试了

如果想要引用openCV,CMakeLists.txt中这样写

复制代码
find_package( OpenCV REQUIRED )
include_directories(./include
${OpenCV_INCLUDE_DIRS} 
)
target_link_libraries( main ${OpenCV_LIBS} )

vscode 中ctrl+shift+p创建task.json,写入如下内容

bash 复制代码
{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build",
            "type": "shell",
            "command": "./build.sh",
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

就可以在vscode中ctrl+shift+b 运行build.sh文件,不必在Terminal中运行了。

编译完成后按F5运行程序

相关推荐
shadon1789 天前
回答 如何通过inode client的SSLVPN登录之后,访问需要通过域名才能打开的服务
linux
小米里的大麦9 天前
014 Linux 2.6内核进程调度队列(了解)
linux·运维·驱动开发
算法练习生9 天前
Linux文件元信息完全指南:权限、链接与时间属性
linux·运维·服务器
忘了ʷºᵇₐ9 天前
Linux系统能ping通ip但无法ping通域名的解决方法
linux·服务器·tcp/ip
成遇9 天前
在Vscode中安装Sass并配置
vscode·rust·sass
浩浩测试一下9 天前
渗透测试指南(CS&&MSF):Windows 与 Linux 系统中的日志与文件痕迹清理
linux·运维·windows·安全·web安全·网络安全·系统安全
敏叔V5879 天前
大模型Text2SQL之在CentOS上使用yum安装与使用MySQL
linux·mysql·centos
小生云木9 天前
Linux离线编译安装nginx
linux·运维·nginx
API开发9 天前
苹果芯片macOS安装版Homebrew(亲测) ,一键安装node、python、vscode等,比绿色软件还干净、无污染
vscode·python·docker·nodejs·openssl·brew·homebrew
zkyqss9 天前
OVS Faucet练习(下)
linux·笔记·openstack