ubuntu+VsCode+Cmake+eigen 开发eigen应用

以下内容参见官方文档:

https://code.visualstudio.com/docs/cpp/cmake-linux

1. 安装Cmake工具

点击左侧的Extensions,搜索Cmake tools,这里已经安装。

2. 安装Cmake

cmake --versioin

ubuntu系统已经安装cmake, 如果没有安装cmake,请查阅资料在系统中安装cmake.

3. 查看gcc是否安装

gcc -v

如果没有安装gcc,则执行下面命令安装:

sudo apt-get update
sudo apt-get install build-essential gdb

4. 创建一个cmake工程

1. 新建一个空文件夹eigen_VsCode_cmake用于存放工程

扫描二维码关注公众号,回复: 11603066 查看本文章

2. 打开VsCode, 添加文件夹eigen_VsCode_cmake

3. 创建源文件和CMakeLists.txt文件

输入快捷键Ctrl+Shift+P 运行 CMake: Quick Start 命令:

输入工程名字:eigenMatrix

选择可执行文件Executable。

生成.cpp和CMakeLists.txt文件如下所示。

可以看出CMakeLists.txt中project名字就是CMake: Quick Start阶段输入的名字eigenMatrix。

CMakeLists.txt文件:

cmake_minimum_required(VERSION 3.0.0)
project(eigenMatrix VERSION 0.1.0)

include(CTest)
enable_testing()

add_executable(eigenMatrix eigenMatrix.cpp)

set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack)

main.cpp修改为eigenMatrix.cpp 文件:

#include <iostream>
#include <ctime>
#include <eigen3/Eigen/Core>
#include <eigen3/Eigen/Dense>

using namespace std;
using namespace Eigen;

#define MATRIX_SIZE 50

int main(int argc, char *argv[])
{
    Eigen::Matrix<float, 2, 3, Eigen::ColMajor> matrix_23;
    
    Eigen::Vector3d v_3d;
    
    Eigen::Matrix3d matrix_33 = Eigen::Matrix3d::Zero();
    
    Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic> matrix_dynamic;
    
    Eigen::MatrixXd matrix_x;
    
    matrix_23 << 1, 2, 3, 4, 5, 6;
    cout << "matrix_23:"<<endl;
    cout << matrix_23 << endl;
    
    cout << "matrix_23:" << endl;
    for (int i=0;i<2;++i){
        for (int j=0;j<3;++j){
            cout<<matrix_23(i,j)<<endl;
        }
    }
    
    v_3d << 1, 2,3;
    
    Eigen::Matrix<double, 2,1> result=matrix_23.cast<double>() * v_3d;
    cout<<"result:"<<endl;
    cout<<result<<endl;
    
    matrix_33 = Eigen::Matrix3d::Random();
    cout << "matrix_33:" <<endl;
    cout << matrix_33 <<endl;
    
    cout <<"matrix_33.transpose():"<<matrix_33.transpose() <<endl;
    cout <<"matrix_33.sum():"<<matrix_33.sum()<<endl;
    cout <<"matrix_33.trace():"<<matrix_33.trace()<<endl;
    cout <<"matrix_33 * 10"<<matrix_33 * 10<<endl;
    cout <<"matrix_33.inverse():"<<matrix_33.inverse()<<endl;
    cout<<"matrix_33.determinant():"<<matrix_33.determinant()<<endl;
    
    Eigen::SelfAdjointEigenSolver<Eigen::Matrix3d> eigen_slover(matrix_33.transpose()*matrix_33);
    cout<<"eigen_slover.eigenvalues():"<<eigen_slover.eigenvalues()<<endl;
    cout<<"eigen_slover.eigenvectors():"<<eigen_slover.eigenvectors()<<endl;
    
    Eigen::Matrix<double, MATRIX_SIZE, MATRIX_SIZE> matrix_NN;
    matrix_NN = Eigen::MatrixXd::Random(MATRIX_SIZE, MATRIX_SIZE);
    Eigen::Matrix<double, MATRIX_SIZE, 1> v_Nd;
    v_Nd = Eigen::MatrixXd::Random(MATRIX_SIZE, 1);
    
    clock_t time_stt=clock();
    Eigen::Matrix<double, MATRIX_SIZE, 1> x=matrix_NN.reverse()*v_Nd;
    cout<<"time use in normal invers is"<<1000*(clock()-time_stt)/(double)CLOCKS_PER_SEC<<"ms"<<endl;
    
    time_stt=clock();
    x=matrix_NN.colPivHouseholderQr().solve(v_Nd);
    cout<<"time use in Qr compsition is"<<1000*(clock()-time_stt)/(double)CLOCKS_PER_SEC<<"ms"<<endl;
    
    
    return 0;
}

5. 选择编译工具CMake: Select a Kit(toolchain

Ctrl+Shift+P -》CMake: Select a Kit  选择编译工具链

选择GCC for x86_64-linux-gnu 7.5.0

VsCode窗口最下面显示当前的编译器,可以点击修改toolchain。

6. 选择variant,通常说的debug或者release

A variant contains instructions for how to build your project. By default, the CMake Tools extension provides four variants, each corresponding to a default build type: DebugReleaseMinRelSize, and RelWithDebInfo. These options do the following:

Debug: disables optimizations and includes debug info. Release : Includes optimizations but no debug info. MinRelSize : Optimizes for size. No debug info. RelWithDebInfo : Optimizes for speed and includes debug info.

To select a variant, open the Command Palette (Ctrl+Shift+P) run the CMake: Select Variant command.

Select variant

Select Debug to include debug information with your build.

Select debug variant type

The selected variant will appear in the Status bar next to the active kit.

7. CMake: Configure 根据kit和variant生成配置文件

Now that you've selected a kit and a variant, open the Command Palette (Ctrl+Shift+P) and run the CMake: Configure command to configure your project. This generates build files in the project's build folder using the kit and variant you selected.

 

8. Build eigenMatrix编译工程

After configuring your project, you're ready to build. Open the Command Palette (Ctrl+Shift+P) and run the CMake: Build command, or select the Build button from the Status bar.

Build

You can select which targets you'd like to build by selecting CMake: Set Build Target from the Command Palette. By default, CMake Tools builds all targets. The selected target will appear in the Status bar next to the Build button.

9.Debug eigenMatrix调试工程

在代码中添加断点

Ctrl+Shift+P-》CMake: Debug

停留在调试的断点处

猜你喜欢

转载自blog.csdn.net/wzl19910916/article/details/108434357