win10 + vs code + MinGW + Cmake 配置
vscode 编译任务 (tasks.json)如下:
{
// 这里配置了三个Task
"tasks": [
// Task 1:生成build文件夹
{
"type": "shell", // 这里决定了task的类型,shell类型就是在命令行里运行command的命令,还有一种类型是“process”,这里就不介绍了
"label": "mk_build", // task的名字
"command": "mkdir", // 需要被运行的命令
"args": ["build"], // 传递给command的参数
"options": {
"cwd": "${workspaceFolder}",
},
},
// Task 2:运行cmake
{
"type": "shell",
"label": "cmake",
"command": "cmake",
"args": [
"-G",
"MinGW Makefiles", // 这里必须定义生成器,否则默认cmake生成的是VS Studio可编译的工程
"${workspaceFolder}"
],
"options": {
"cwd": "${workspaceFolder}/build", // 工作路径,就是说cmake的输出都会放在${workspaceFolder}/build文件夹下
},
"problemMatcher": [],
"group": {
"kind": "build",
"isDefault": true
}
},
// Task 3:运行make
{
"type": "shell",
"label": "build",
"command": "mingw32-make",
"args": ["-j4"],
"options": {
"cwd": "${workspaceFolder}/build",
},
"problemMatcher": [],
"group": {
"kind": "build", // 这里说明这个任务时输入"build"类型的,也就是说明,这是一个生成任务
"isDefault": true
},
"dependsOn": [
"cmake" // 这里设置,说明了这个任务一定要在Task “cmake”执行之后方可执行
]
}
],
"version": "2.0.0"
}
vs code 调试设置(launch.json):
{
"version": "0.2.0",
"configurations": [
// Debug 1,这里也可以配置多个Debug任务,这其实和tasks.json很类似
{
"name": "first_exe", // Debug任务名
"type": "cppdbg", // 类型
"request": "launch",
"program": "${workspaceFolder}/build/cmake_test_project.exe", // 需要调试的应用程序,这是需要按照你的需要进行调整的
"args": [], // 传递参数给应用程序
"stopAtEntry": false,
"cwd": "${workspaceFolder}", // 当前工作路径
"environment": [{"name": "PATH","value": "${PATH};C:\\Program Files\\mingw-w64\\x86_64-8.1.0-win32-seh-rt_v6-rev0\\mingw64\\bin;D:/software/aruco/bin"}], // 这里需要特别注意,这里要把应用程序运行需要的动态链接库的路径,放进来,这里也需要你根据自己需要进行调整,我在这里给出一个示范
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-win32-seh-rt_v6-rev0\\mingw64\\bin\\gdb.exe", // 这里需要填写你自己的gdb.exe的路径
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
测试代码如下(以矩阵运算库eigen为例):main.cpp
#include <iostream>
#include "Eigen/Dense"
using namespace std;
using namespace Eigen;
typedef Matrix<float,1,Dynamic> MatrixType;
typedef Map<MatrixType> MapType;
typedef Map<const MatrixType> MapTypeConst; // a read-only map
typedef Matrix<int, Dynamic, Dynamic,RowMajor> MatrixTypeInt;
typedef Map<MatrixTypeInt> MapTypeInt;
typedef Map<const MatrixTypeInt> MapTypeConstInt; // a read-only map
int main()
{
MatrixXf m(4,4);
m<< 1,2,3,4,
5,6,7,8,
9,10,11,12,
13,14,15,16;
cout<<"Block in the middle"<<endl;
cout<<m.block<2,2>(1,1)<<endl<<endl;
for(int i = 1;i <= 3;++i)
{
cout<<"Block of size "<<i<<"x"<<i<<endl;
cout<<m.block(0,0,i,i)<<endl<<endl;
}
int pp = 2, qq = 2;
cout<<m.block(1,1,pp,qq)<<endl<<endl;
int array[8];
for(int i = 0; i < 8; ++i) array[i] = i;
cout << "Column-major:\n" << Map<Matrix<int,2,4> >(array) << endl;
cout << "Row-major:\n" << Map<Matrix<int,2,4,RowMajor> >(array) << endl;
cout << "Row-major using stride:\n" <<
Map<Matrix<int,2,4>, Unaligned, Stride<1,4> >(array) << endl;
int data[] = {1,2,3,4,5,6,7,8,9};
Map<RowVectorXi> v(data,4);
cout << "The mapped vector v is: " << v << "\n";
new (&v) Map<RowVectorXi>(data+4,5);
cout << "Now v is: " << v << "\n";
int tmpN1 = 2;
int tmpN2 = 4;
MatrixTypeInt tmp1(tmpN1, tmpN2);
tmp1.setIdentity();
MapTypeInt tmpMapInt(data, tmpN1, tmpN2); // m2map shares data with m2
cout<< tmp1<<endl;
cout<< tmpMapInt<<endl;
cout<< "11 qfr " << MapTypeInt(data, tmpN1, tmpN2)<<endl;
int n_dims = 5;
MatrixType m1(n_dims), m2(n_dims);
m1.setRandom();
m2.setRandom();
float *p = &m2(0); // get the address storing the data for m2
MapType m2map(p,m2.size()); // m2map shares data with m2
MapTypeConst m2mapconst(p,m2.size()); // a read-only accessor for m2
cout << "m1: " << m1 << endl;
cout << "m2: " << m2 << endl;
cout << "Squared euclidean distance: " << (m1-m2).squaredNorm() << endl;
cout << "Squared euclidean distance, using map: " <<
(m1-m2map).squaredNorm() << endl;
m2map(3) = 7; // this will change m2, since they share the same array
cout << "Updated m2: " << m2 << endl;
cout << "m2 coefficient 2, constant accessor: " << m2mapconst << endl;
/* m2mapconst(2) = 5; */ // this yields a compile-time error
}
CMakeLists.txt 如下:
cmake_minimum_required(VERSION 3.0)
project(cmake_test_project)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g")
include_directories( "E:\\code\\eigen-3.3.8" )
############### source ######################
set(
rtk_tc_src_list
${CMAKE_CURRENT_SOURCE_DIR}/main.cpp
)
add_executable(${PROJECT_NAME} ${rtk_tc_src_list})
参考链接
Windows+VSCode+MinGW+CMake环境配置
记录每天生活的点点滴滴,呵呵呵呵呵呵

浙公网安备 33010602011771号