vs2019 + cmake achieve remote Linux Development

In the previous article we introduced the use vs2019 as a remote Linux system development environment, but we created a traditional sln project, and for Linux developers to structure the project cmake or autotools organization is more simple and intuitive , also in line with the habit on Linux environment.

autotools is a more ancient building is the most widely used system, you can not always avoid similar on Linux ./configure && makesuch an order, is behind the autotools for you to complete the detection system to generate a series of work environment the makefile.

cmake is a relatively new tool, autotools powerful although widely used, but it's learning costs and maintenance cost is also very alarming, so people cmake created to simplify the work. cmake is very easy to learn, on the expression of no less than autotools, but also provides a wealth of official modules and third-party modules in order to customize a variety of functions. There have been many projects to start using the cmake, such as google test framework, qbittorrent, KDE, _MySQL_, the future will be to migrate from Qt qmake to cmake, it has already provided preliminary support.

Unfortunately vs2019 does not support the autotools tool chain, but vs2019 support cmake, and compared to vs2017, vs2019 cmake support provides remote development, and supports more setting options, so today we will explain how to use vs2019 + cmake Linux remote achieve development. But note that this article is how to setup the development environment, and will not introduce cmake syntax, and I also assume that our readers have a basic understanding of how to write a simple CMkaeLists.txt that, if you do not know may need to cmake simple to learn, which is beyond the scope of this article you can look for other blog articles garden to learn relevant knowledge. Of course, even if not later understand the content of the listed CMakeLists.txt it does not matter, I will try to give straightforward comments.

Well, now let us into the theme.

Create a remote cmake project

Create a very simple, select "Create new project" and find "CMkae Project", select and click Next to, and create traditional items of course exactly the same as in vs startup window:

Once created your project will be the following scenario (if the project name is CMakeProject1):

You might wonder, why did not distinguish cmake project sln project on Linux and Windows platform? The answer is that we can change between local and remote environments through environmental projects set!

CMakeLists.txt the entire project by the organization, and is responsible for running vs cmake in what environment, thus achieving almost the same set of items can compile and run without modification (as long as your target platform is equipped with cmake on different platforms, and versions the lowest was 3.8; vs local environment comes with cmake).

cmake project is in default in the local environment, so let's create one called "LinuxQt" remote project, then set the corresponding remote Linux environment.

Setting remote environment

Before setting up remote environment, you need the remote option Tools menu at the top of the dialog box in the connection set up and synchronize the remote environment file header, the specific process may refer to this , the same process will not go into details.

In the initial launch of the project in terms either a file or is empty, without our remote environment, so we need to CMakeLists.txt documents show that the right-Explorer:

Find the "project-name of CMake settings", project-name is the name of your project, click. Then will generate a "CMakeSettings.json" file, which is the project's configuration file, double-click displays a graphical configuration interface:

First, we see the configuration name, this is the place for you to custom configure a name, the right of the green plus sign to add a new configuration representation, because we want to use a remote Linux environment, so we directly modify the default configuration items.

Next is the type of configuration, and this option corresponds to the cmake, set here after CMakeLists.txt no longer written, and there are Debug, Release and other models, we choose Release, because Qt on remote debugging environment I did not install accord, choose Debug build target in addition to volume increases of no use.

Below is the focus, the name of the remote computer option. Click on the drop-down box to appear remote environment we added in the Connection Manager, if you do not add a remote environment, you can open the connection manager directly add button to the right. This option is empty by default, which is the native compiler is not enabled remote environment.

Next is the tool set, which is the final call of the compiler tool chain, vs gcc support and clang, linux_x64the corresponding gcc, linux_clang_x64correspondence clang, in addition to arm supported platforms, the choice of what to see corresponding platform tool chain and personal preferences, choose me here the gcc.

Then "remotely generated root" option, screenshot is not given, it is vs the entire project is stored remotely compile-time path, the default in your home directory under the .vsdirectory, you can also modify the path according to their needs we demonstrate a project directly use the default value.

After generating the root option is to set the parameters when calling cmake program, as long as they fill in the required parameters can be input box, here we do not have to useless screenshot.

vs2019 in a powerful feature that can be displayed value of the variable in cmake generated by the system or module (required after a cache successfully refreshed, that is, after cmakelists file is saved or manually click to generate the project cache Project menu) :

Then we click on Show advanced options, because you want vs provide code completion also need to set up a little:

Here you can set what type of makefile cmake generated after running cmake to compile complete the installation directories and program directory, and the path itself cmake where (if you install cmake to a less conventional places such as / opt).

Which focus on IntellSense option, which is to select the code completion engine:

You can see all the options are made 平台名称-编译器名称-32位/64位in this format consisting default value is empty, we want code completion will be used to select the remote environment and corresponds exactly to the kind of pattern.

In addition the upper right corner has been directly edit json file button, if you hate gui, then you can select it.

Finally, we save the changes, vs will automatically refresh the cache, we can now remotely developed.

Write CMakeLists.txt

We said before the organization cmake projects need to rely on CMakeLists.txt, now we have to write it.

Our test project will use Qt, randomly display some random number generated by different engines, then put them in the chart. Selecting this example is to demonstrate the ability to better cmake projects, but the program is still remote development gui some difficulties on vs:

  1. vs run a remote environment program relies on ssh, but the Linux gui procedures required for the connection xserver (usually connection information in the environment variable), ssh start shell environment without these environment variables, you may also need to command additional setup program starts line parameters, otherwise a runtime error will occur.
  2. This is the reason for its Qt, Qt moc rely on their own system, and native c ++ is slightly different, so the code completion time will often not find the type of (clion no such problems).
  3. vs own problems, although it supports Qt cmake, but vs does not work when the remote environment calls moc, custom widget vtable will not find a similar report and other issues.
  4. qt vs tool does not work in a remote environment.

Although there are more flaws, but we have to write a single project file and not a custom widget, while only the compiled program without running, then it is no problem.

Let's look at how CMakeLists.txt is written:

project(LinuxQtExample)

# 设置c++语言标准,我使用c++17
set(CMAKE_CXX_STANDARD 17)

cmake_minimum_required (VERSION 3.10)

set(CMAKE_INCLUDE_CURRENT_DIR ON)

# 自动调用moc, uic, rcc
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTORCC ON)

# 找到这些Qt组件
find_package(Qt5Widgets REQUIRED)
find_package(Qt5Core REQUIRED)
find_package(Qt5Gui REQUIRED)
find_package(Qt5Charts REQUIRED)

# 将源代码添加到此项目的可执行文件。
add_executable (LinuxQt "main.cpp")

# 将Qt的库链接至程序
target_link_libraries(LinuxQt Qt5::Core Qt5::Widgets Qt5::Gui Qt5::Charts)

More how to build Qt program with cmake venue here .

Write test code

After the end of the above settings you can begin writing code, code hinting and completion can work (although not normal for some full complement of Qt, but make c ++ standard library are all known working):

#include <QApplication>
#include <QBarCategoryAxis>
#include <QBarSet>
#include <QBarSeries>
#include <QChart>
#include <QChartView>
#include <QPushButton>
#include <QString>
#include <QStringList>
#include <QValueAxis>
#include <QVBoxLayout>

#include <iostream>
#include <random>

// 这个函数里变量名起的很烂,因为是示例我偷懒了,请你不要在实际项目中写出这种代码
// 创建柱状图数据的函数
// std::random_device的某些实现在Windows上存在bug,每次运行会返回同样的结果序列,linux没问题
// QtCharts的所有类型/函数都在对应的命名空间中,和其他的QtWidgets不同
static QtCharts::QBarSeries* createSeries()
{
    auto dataSet1 = new QtCharts::QBarSet("mt19937");
    auto seed = std::random_device{}();
    std::uniform_int_distribution<int> u(0, 100);
    std::mt19937 rd1(seed);
    for (int i = 0; i < 10; ++i) {
        auto a = u(rd1);
        std::cout << a << std::endl;
        *dataSet1 << a;
    }

    auto dataSet2 = new QtCharts::QBarSet("minstd_rand");
    std::minstd_rand rd2(seed);
    for (int i = 0; i < 10; ++i) {
        auto a = u(rd2);
        std::cout << a << std::endl;
        *dataSet2 << a;
    }

    auto dataSet3 = new QtCharts::QBarSet("default");
    std::default_random_engine rd3(seed);
    for (int i = 0; i < 10; ++i) {
        auto a = u(rd3);
        std::cout << a << std::endl;
        *dataSet3 << a;
    }

    auto dataSet4 = new QtCharts::QBarSet("ranlux48");
    std::ranlux48 rd4(seed);
    for (int i = 0; i < 10; ++i) {
        auto a = u(rd4);
        std::cout << a << std::endl;
        *dataSet4 << a;
    }

    auto dataSet5 = new QtCharts::QBarSet("knuth_b");
    std::knuth_b rd5(seed);
    for (int i = 0; i < 10; ++i) {
        auto a = u(rd5);
        std::cout << a << std::endl;
        *dataSet5 << a;
    }

    auto barSeries = new QtCharts::QBarSeries;
    barSeries->append(dataSet1);
    barSeries->append(dataSet2);
    barSeries->append(dataSet3);
    barSeries->append(dataSet4);
    barSeries->append(dataSet5);
    return barSeries;
}

int main(int argc, char* argv[])
{
    QApplication app(argc, argv);
    auto chart = new QtCharts::QChart;

    // 创建Y轴显示数据
    auto axisY = new QtCharts::QValueAxis;
    axisY->setRange(0, 100);
    axisY->setTickCount(10);
    axisY->setTitleText("Y轴");
    chart->addAxis(axisY, Qt::AlignLeft);

    // x轴显示10次取随机数的结果
    QStringList x;
    for (int i = 0; i < 10; ++i) {
        x << QString::number(i+1);
    }
    auto axisX = new QtCharts::QBarCategoryAxis;
    axisX->append(x);
    chart->addAxis(axisX, Qt::AlignBottom);

    auto barSeries = createSeries();
    chart->addSeries(barSeries);

    chart->setTitle("随机数分布图");
    // 显示图例以及让图例摆放在图表的底部
    chart->legend()->setVisible(true);
    chart->legend()->setAlignment(Qt::AlignBottom);
    // 显示chart的容器
    auto view = new QtCharts::QChartView(chart);
    view->setRenderHint(QPainter::Antialiasing);

    auto layout = new QVBoxLayout;
    layout->addWidget(view);
    // 点击按钮刷新显示的数据
    auto button = new QPushButton("点击刷新");
    QObject::connect(button, &QPushButton::clicked, [chart]() {
        // removeAll会帮你删除原来的series,所以不必担心内存泄漏
        chart->removeAllSeries();
        auto barSeries = createSeries();
        chart->addSeries(barSeries);
    });
    layout->addWidget(button, Qt::AlignCenter);
    auto window = new QWidget;
    window->setLayout(layout);
    window->setWindowTitle("图表");
    // 图表默认会显示成最小,为了不让图表缩成一团需要给一个固定的大小
    window->resize(700, 500);
    window->show();
    app.exec();
}

The code used in encoding utf8 Chinese string, you need to set the encoding of the source file is garbled utf8 so as not to run on Linux. See in particular here .

Run the test

As previously said, we can not directly click the Run button, so we can only choose to gui programs generate top toolbar -> All generation, this will automatically vs cmake and make calls to either build the program:

Vs you can see the entire project with rsync to synchronize the remote machine, then run cmake and make.

Under "Remote generate root" After the success of our generation to previously set out/build/..., ellipsis indicates that your cmake project configuration name, the compiled program here, in a remote environment running the following:

to sum up

Cmake on the overall project easier and better than sln control, but the details still lacking.

cmake province is also easy to learn, has a powerful, if you are migrating from Linux development environment on Windows might try to cmake.

Guess you like

Origin www.cnblogs.com/apocelipes/p/11431808.html