ubuntu+VsCode+Cmake+eigen 开发eigen应用
以下內(nèi)容參見官方文檔:
https://code.visualstudio.com/docs/cpp/cmake-linux
1. 安裝Cmake工具
點(diǎn)擊左側(cè)的Extensions,搜索Cmake tools,這里已經(jīng)安裝。
2. 安裝Cmake
cmake --versioin
ubuntu系統(tǒng)已經(jīng)安裝cmake, 如果沒有安裝cmake,請(qǐng)查閱資料在系統(tǒng)中安裝cmake.
3. 查看gcc是否安裝
gcc -v如果沒有安裝gcc,則執(zhí)行下面命令安裝:
sudo apt-get update sudo apt-get install build-essential gdb?
4. 創(chuàng)建一個(gè)cmake工程
1. 新建一個(gè)空文件夾eigen_VsCode_cmake用于存放工程
2. 打開VsCode, 添加文件夾eigen_VsCode_cmake
3. 創(chuàng)建源文件和CMakeLists.txt文件
輸入快捷鍵Ctrl+Shift+P 運(yùn)行?CMake: Quick Start?命令:
輸入工程名字:eigenMatrix
選擇可執(zhí)行文件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窗口最下面顯示當(dāng)前的編譯器,可以點(diǎn)擊修改toolchain。
?
6. 選擇variant,通常說(shuō)的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:?Debug,?Release,?MinRelSize, 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?Debug?to include debug information with your build.
The selected variant will appear in the Status bar next to the active kit.
?
7. CMake: Configure 根據(jù)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.
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調(diào)試工程
在代碼中添加斷點(diǎn)
Ctrl+Shift+P-》CMake: Debug
停留在調(diào)試的斷點(diǎn)處
?
總結(jié)
以上是生活随笔為你收集整理的ubuntu+VsCode+Cmake+eigen 开发eigen应用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: ubuntu中使用VsCode+Eige
- 下一篇: 完美解决 keil5.25 某宝Jl