Take QT as an example to talk about environment building

Author: Zhe Si

Time: 2022.1.5

E-mail: 1464445232 @qq.com

GitHub: Zhe-si (Github.com)

preface

It’s been a long time since I finished my internship. To kick off 2022, I’m going to start by talking about something that almost every developer is worried about — environment setup. The opportunity is to build QT environment in a QT project recently done in 615.

This paper has two main objectives:

  1. Provides a simple, effective QT installation and environment configuration solution
  2. Talk about your understanding of the software environment from the general software level, and try to answer the question: What is our environment?

Note: The term “software” referred to in this article does not refer to the definition of program + data + documentation in software engineering, but refers to a complete and operational program.

QT installation and environment configuration

Introduction of QT

Speaking of qt, many people subconsciously think of it as a c++ interface development library. Qt is indeed the most widely used interface development, but it is more than that.

To put it simply, Qt is a cross-platform C++ application development framework, and provides developers with a convenient graphical interface development tools and more advanced object-oriented development methods.

To install and configure QT, simply follow these steps:

  1. Download the QT
  2. Install QT
  3. Configure any qt development environment
  4. Configure the QT runtime environment

To download QT

  • Try QT -> Download QT -> Download QT

  • Approach 2: Qt-unified – Windows -x86-4.2.0-online.exe – Qt-unified – Windows -x86-4.2.0-online.exe – Qt-unified – Windows -x86-4.2.0-online.exe

    Download link, extract code: LZNC

    Note: You can skip the installation package login process by turning off the Internet

  • Way three: Tsinghua mirror download

    Qt 5.14.2 (this version is high enough in most scenarios) and the following versions are provided with the installation package. The previous version only has the source code. If you want to install the qt 5.14.2, you can use the online download from the official website to download the installation package.

QT installation

Offline Installation Package

  1. Double-click to open the installation package, as shown

    On the left is the installation step. In welcom step, you need to log in to the QT official website account. This step can be skipped by shutting down the network and opening the installation package again.

  2. If logged in, an Open Source Obligations step will be added for you to confirm the open source agreement.

  3. Setup step directly next; In the Installation Floder step, select the installation location, Check “Associate Common File Types with Qt Creator” to automatically Associate qT-related file formats with Qt Creator.

  4. The Select Components step selects a specific component to install, starting with Developer and Designer Tools, which contains less content.

    This section is the additional tools section of Qt, not the Qt library, including the mandatory Qt Creator tool, the optional Qt Creator Debug tool, the MinGW tool chain suite, and the Perl language build tool. The default selection is shown below.

    MinGW is a suite of tools that come with Qt. The build tool chain needs to be compatible with the version of Qt. In order to avoid the trouble of incompatible versions, it is recommended to check the build tool and use it to build Qt projects.

    The components in the Qt + version directory are the contents of the Qt library, as shown below:

    On top of Sources are various versions of Qt, which correspond to the Build toolchain and need to be compatible with the Build toolchain version (usually downward compatible, but as consistent as possible). Select at least one Qt version based on your application scenario.

    • MSVC is the default build tool for Visual Studio (hereafter referred to as VS), version corresponding to VS version. For example, users using 64-bit VS2017 need to select MSVC 2017 64-bit.
    • MinGW is the collection of minimum header files and libraries of GNU for Windows system. It can realize cross-platform project development by translating Linux standard API interface into Windows standard API interface. Here, the author recommends using MinGW development (more standard, easy cross-platform). For example, select MINGW7.3.0-64bit for the development tool chain
    • UWP is a universal Windows development project proposed by Windows that aims to provide the same API for Windows systems running on different types of terminals.
    • Qt also provides a library of development tools for Android.

    Sources is Qt source code, recommended check; The Qt library provides web and other related components. It is not necessary, but it can be checked because it is not large.

  5. If you want to agree the agreement, click Agree. All the other defaults will be installed successfully.

Online Installation Package

The online installation package is a downloader that automatically downloads the corresponding components from the official repository according to your choice. The version can be selected freely, but the download may be slow.

The basic installation steps are the same as those for offline installation. When selecting components to install, the online installation package displays all current versions of Qt. The following are recommended for qt6.1.2. Here, the author selected the MinGW tool chain kit in Tools consistent with QT MinGW version in Qt6.2.1, as shown below:

QT development environment configuration

QT Creator configuration

No need to configure, automatically configured at installation, QT Creator will automatically find all installed QT versions, build chain (in this case, compiler), Debugger, cmake. The current configuration can be viewed and modified in Tools -> Select -> Kits.

The MinGW build tool chain directly includes the debug tool gdb.exe, which can be used directly.

If the MSVC tool chain does not contain the debug tool, you need to install it by yourself. The Windows 10 Debugging Tool (WinDbg) is integrated with the Windows 10 SDK. If you have not installed the SDK before, you can install it by following the tutorial on the official website. If you have already installed the Windows 10 SDK, you can go to the Control Panel -> Programs and Features to find the Windows Software Development Kit- Windows, right-click to change, and add the Debug tool as follows:

Use the same method to configure MSVC debug in other development environments.

Visual studio configuration

  1. Go to VS Tools -> Extensions & Updates -> Search online and install Qt Visual Studio Tools, then restart vs

  2. Choose VS -> Qt vs Tools -> Qt Versions to configure the corresponding Qt directory. You need to select bin -> qmake.exe in the Qt installation path, and configure the MSVC Qt version in vs. (This interface may vary from VS version to VS version, but the newly installed QT version is added.)

  3. To configure the qt parameters of the Project, right-click the Project -> Properties -> Qt Project Setting

    There are two main parameters: Select the newly configured Qt in Qt Installation, and add the required Qt module in Qt Modules.

Clion Configuration (CMake)

Clion does not have its own engineering configuration, but uses Cmake for project management. I prefer to use this way, configuration is more clear, the project is also more cross-platform.

Clion needs to be configured in two places, to build the toolchain and project CmakeLists files.

  1. Building a tool chain

    Clion does not have a default build chain. You need to install MinGW, Clang, or use vs build tools. If you have a build tool configured in the environment variable, it will be found automatically. You can also customize it by going to File > Settings > Build, Execute, Deploy > Toolchain and clicking +.

    The one at the top is the default build chain. You can specify others in your project, but choose the build chain that is compatible with the Qt version (the best version).

  2. CmakeLists.txt

    Cmakelists. TXT is a cmake project project file, which describes all the configuration and construction of the project. The specific syntax is recommended for you to learn in the official website tutorial.

    Here is a list of cmake config Qt CmakeLists config, believe everyone is clear:

    Cmake_minimum_required (VERSION 3.15.5) project(TE_TEST) set(CMAKE_CXX_STANDARD 17) set(CMAKE_AUTOMOC ON) Set (CMAKE_AUTORCC ON) set(CMAKE_AUTOUIC ON) # set(CMAKE_AUTOUIC ON) # Set (CMAKE_PREFIX_PATH "D:/env/c++/ Qt /qt5_12_12/5.12.12/mingw73_64") Find_package (Qt5 COMPONENTS Core Gui Widgets SerialPort Xml REQUIRED) Link_directories (libs/ GRPC /lib/release) file(GLOB_RECURSE GRPC_LIBS Libs/GRPC /lib/release/*.lib) # test case file(GLOB test_case test_case/ *.cpp) # Interface/trte_test. h interface/ trte_test. CPP interface/ trte_test. QRC interface/ trte_test. UI # Test case CPP ${test_case} # tools/ config. CPP tools/ config. h test_case/ testcase. h Exceptions/ConfigException. H) # link lib to product (also links in the Qt libraries) target_link_libraries (TE_TEST Qt5: : Core Qt5: : Gui Qt5: : Widgets Qt5::SerialPort Qt5::Xml ${GRPC_LIBS}) # test add_executable(run_test tests/test_main.cpp tools/ config.h tools/ config.cpp  exceptions/ConfigException.h ) target_link_libraries(run_test Qt5::Core Qt5::Gui Qt5::Widgets Qt5::SerialPort Qt5::Xml ${CMD_CONFIG_LIBS} )Copy the code

QT runtime environment configuration

Configure the bin directory under the installed QT into the environment variable path.

Building toolchains (MinGW, MSVC, etc.) does not need to be configured into the PATH environment variable, but can be configured if you want to use GCC on the command line, etc. Be careful not to configure multiple versions of the build chain in path.

summary

At this point, the QT environment configuration is complete, from writing code in the IDE and compiling it, correctly connecting the compilation process to the corresponding static link library, to running/debugging in the IDE or directly double-clicking the EXE to open it.

Software environment understanding

As a software developer, I have suffered from configuration environments many times, especially when running on other people’s projects or using frameworks.

This configuration QT environment, the original project is under THE VS QT project, the author according to the online tutorial careful configuration, finally no longer floating red, ran through the construction, Libgcc_s_seh-1. DLL and qtcored. DLL cannot be found, and the program input point cannot be located in the dynamic link library.

After a long struggle, the strange problems were solved and the root of the problem was understood. Suddenly reviewed the experience that matchs environment for many times and solution method, discovery principle is much the same with minor difference, after figuring out, match other environment also to have kind of the feeling that is familiar with the road lightly. So I want to take this opportunity to talk about, what is a software environment? How to configure the software environment?

QT environment

Let’s go back to the QT environment we just installed. What are we doing?

Install QT

First we download and install a QT. What is the installation?

The easiest installation is to unzip the package. The installation package is a compressed package, which may contain multiple executable files (exe), dynamic link library (DLL) and other files. The simplest installation process is to extract it to a folder. We need to run one or more of these executables correctly.

Sometimes, the installation is accompanied by the download process, which means that the installation package contains only part of the program files. During the installation, the program files of other additional functions can be automatically downloaded according to the user’s choice.

During installation, we are sometimes asked to choose whether or not to add a shortcut to the desktop or menu. In fact, the shortcut is generated from the main executable of the program and placed in the desktop or menu folder. Here is my menu folder:

Sometimes, when a program wants to find itself globally, it adds its own executables or dynamic link libraries to the PATH environment variable, or to folders that have already been added to path. To view the contents of the current PATH environment variable, enter “path” in CMD:

In other cases, installation packages want to make the operating system aware of installed software and write their own information to the registry. The following is the author’s computer QQ registered in the registry of information:

Installation also has a lot of customization based on software needs, but it’s all about getting the installed software to read and write some information or find some file so it can work correctly.

For QT, it gets the required files from the installation package and the official repository on the network, puts them all in the installation path, and writes its own information to the registry to inform the operating system of its existence. However, QT is not configured in the environment variable, so it cannot be found globally directly.

Configure the QT development environment

Next, we configured the QT development environment.

Like c/ C ++ development dev C ++, Visual Studio, Clion, Java development Eclipse, IDEA, I believe everyone is familiar. And they all have a common name, called IDE, full name “integrated development environment”. The following is my favorite IDE:

Cited in RedHat’s definition of IDE: An integrated development environment (IDE) is software for building applications that combine common developer tools into a single graphical user interface (GUI).

The IDE mainly includes a source code editor, native build automation tools (that is, code compilation, packaging, etc.), debuggers, and of course other development tools such as database access tools, performance profilers, and so on May be integrated.

So as the name implies, we configure the QT development environment by integrating the QT development SDK into the IDE (so that the IDE can find the QT SDK), so that the IDE can use the SDK to configure, build, run/debug QT, and reuse other tools provided by the IDE.

Earlier, we configured the c++ build chain (MinGW, MSVC) to build generic c++ programs.

At the same time, QT’s unique build tools and libraries are configured.

  • QT vs Tools uses Qmake to describe project construction. It needs to configure the location of Qmake, and Qmake knows how to find and use QT corresponding build Tools and libraries.
  • Clion (cmake) does not directly configure and use one of QT’s tools, but rather uses cmake’s generic ability to describe builds. QT writes library description files that cmake can read directly in the library. This description file describes how to find qt-specific build tools such as uic, RCC, moc and use them to process.ui,.rcc, QT meta-objects, and so on into normal c++ source code. The description also describes the location of the QT library, so we can automatically find the corresponding library through find_package.

Configure the QT runtime environment

Finally, we configured the QT runtime environment.

What is the runtime environment? As the name implies, it is the environment in which the program is running. When we talk about the runtime environment of a piece of software, we are talking about the software’s dependence on the outside world.

This makes it clear, why does software report errors when the runtime environment is wrong? Because the software runs in the runtime environment, it relies on (that is, uses) something else in the environment, and when that thing is not found or is not what we expect, and we can’t use it, the program won’t work, and an error will be reported.

What are our dependencies in the environment? For the most part, we can say dynamically linked libraries. Dynamic link libraries have the suffix.dll on Windows and.so on Linux.

What dependencies do QT programs need? Is the DLL in the QT installation directory bin (Windows dynamic link library). So we can configure the QT bin directory into the environment variable.

Note: You do not need to configure the bin directory of the build toolchain here.

Software environment

Just now we reviewed the QT installation and environment configuration process and the meaning of each process. Review other installation and configuration processes, you will find that they are similar, and the principle is the same, we can be based on QT configuration from point to surface summary:

A program from source code to run, including: compile, link, load, execute, the corresponding GNU tools are generally: compiler(GCC), linker(LD), loader.

General compilation and linking are collectively referred to as compilation, and the period is compile time. Load and run are collectively referred to as runs, which are runtime or execution time.

  • The installation

    Installation is the process of unpacking and helping users easily configure the runtime environment. The concept is relatively simple and was outlined in the “Installing QT” section of the previous section “QT Environment”.

  • Software environment

    • Development environment (compile-time environment is a subset of the development environment) :

      • Building tools: such as compiler, linker, QT meta-object processor MOC, QT user interface compiler UIC, QT resource file compiler RCC, etc. These build tools also have their own runtime environments (dependent on other dynamic libraries, etc.) that together constitute the compile-time environment of the software being developed.

      • Debugging tool: GDB

      • Project dependent static link libraries, header files, and implicitly loaded dynamic link libraries:

        External libraries that the project needs to use during compilation.

        • Statically linked libraries:

          Compile time is used (when links are made). When linking to a static library, the linker finds the functions it wants to link to and copies them to the executable file. This copy is a complete copy, so the program does not need the static library to run after linking successfully.

        • The header file:

          A declaration file for a function or type that tells the compiler that the function or type already exists and that there is an implementation of the declaration elsewhere. If the implementation is not found, an error is reported:

           undefined reference to xxx
          Copy the code

          Both statically linked libraries and implicitly loaded dynamically linked libraries are places where declared implementations are possible.

        • Implicitly loaded dynamically linked libraries:

          More on this when the runtime environment introduces dynamic link libraries.

      • Others: such as code analyzer, performance analyzer, text editing and code hints are also included.

    • Runtime environment/runtime environment:

      • Dynamically linked library

        The search method of dynamic link library is name search, that is, to call a function, as long as the function of the name exists in the dynamic link library (function signature is consistent), even if it is not the original dynamic link library can be called normally.

        • Implicit load

          Compile time and run time are used. At compile time, the linker finds the required function (or other object file), generates Position Independent Code (PIC), and does not really implement a copy; At runtime, when a program calls a dynamically linked library function during execution, the operating system first looks at all running programs to see if they already have a copy of the library function in memory, and if so, shares that copy. Link loads only if there is none.

          If the linked library is replaced, the system automatically synchronizes the memory.

        • According to load

          Active calls to system apis in code to load dynamic link libraries. Similar to reading a file because the exact location is specified.

      • Other software

      • The database

      • file

      • System registry

      • And so on, it can be said that all software is used to its running environment, software is the largest software environment is the operating system.

And “match the environment”, is to make the system ambiguous to find the external dependencies we want.

The development environment

The development environment is simply the environment of program monkey knocking code, from we create a project, to source code editing and version management, to source code construction, and finally to the program debugging and so on a whole development process.

The most basic development environment is a compiler and linker, followed by a text editor, followed by debuggers, project-building tools, and so on.

In development, we can use each tool (executable) separately to handle a development process; You can also use an IDE ide development environment that tells the IDE where the tools are, so that you can use all the tools easily within the IDE.

The general development environment has fewer problems, but please note:

  • Some libraries correspond to the version of the build toolchain
  • Some libraries have release/debug versions and 64/32 bits. Try to choose the version that corresponds to the scenario

Runtime environment

The run-time environment is where the most problems occur, especially with dynamic link libraries, because it uses the runtime to dynamically locate link libraries.

We haven’t spelled out exactly what “not found” and “not the same” mean. When do we run out of things to rely on? This starts with how the system finds this dependency, using Windows traditional desktop applications and Linux as examples.

  • Windows desktop application

    In traditional Windows desktop applications (official notes), the system searches for objects in the following order (this is the default secure DLL search mode, and the non-secure mode is to insert item 5 into item 2 ahead of the current directory of the system library directory) :

    1. The directory from which the application is loaded.
    2. System directory. Use the GetSystemDirectory function to get the path to this directory.
    3. 16 bit system directory. There is no function to retrieve the path to this directory, but a search is performed.
    4. The Windows directory. Use the GetWindowsDirectory function to get the path to this directory.
    5. Current directory.
    6. PATH Directories listed in the environment variable. Note that this does not include every application path specified by the application path registry key. The application path key is not used when calculating the DLL search path.
  • linux

    1. Dynamic library search path specified when compiling object code
    2. LD_LIBRARY_PATH specifies the dynamic library search path;
    3. Ldconfig Specifies the dynamic library search path in the cache configuration file /etc/ld.so.conf.
    4. The default dynamic library search path is /lib.
    5. The default dynamic library search path is /usr/lib.

Could not find dynamic link library, usually error:

The code could not continue because the DLL could not be found.Copy the code

Here are a few more caveats:

  • Before and after the search order

    The system searches the libraries by name in order until it finds the first matching dynamic library. That is, when there is a dynamic library with the same name, the previous configuration takes precedence over the later configuration.

    It should also be noted that there is usually an environment variable as a user-defined set of dynamic library search paths, and the assignment of the environment variable is overridden. There may be non-incremental overrides or overrides of the dynamic linked library used by other programs later.

  • Memory has loaded a static library problem

    If a dynamic library has been loaded in memory, the system will directly use the dynamic library with the same name in memory if subsequent programs rely on the dynamic library.

And “different” means that the declaration of the function is not the same as it was expected to be called. This may be due to a different version, or it may be overwritten by a later library with the same name, resulting in a call to the wrong dynamic library, etc. This error is usually reported:

Unable to locate program input point XXX on XXX.dllCopy the code

To sum up, read the book thin

Read here, we must have a certain understanding of the software environment and environment, but the above text may be too much, is not clear enough, let us read the book together.

  • There are two main types of environments: development environment and runtime environment
  • The product of the development environment is the software itself, and if the software wants to run independently, it needs to run in the appropriate runtime environment
  • Configuring the environment is to make the system unambiguous to find external dependencies, such as build tools, static libraries, dynamic libraries, etc
  • The configuration of the development environment focuses on the compatibility between the build toolchain and the static linked library, and the compatibility between the static linked library and the build target (x86 / X64, release/DEBUG)
  • The configuration of the runtime environment focuses on determining the actual runtime environment of the software and making the system find the corresponding dynamic link library correctly

Afterword.

It is very important for a developer to understand the environment of the software and the construction of the environment, which not only greatly reduces the cost of the environment, but also makes our software package more clear and easy to use, good compatibility.

Package managers such as Python’s PIP and Ubuntu’s APT do the same, but they make package management more explicit and easy by making package dependency descriptions clearer. A classic solution to runtime dependencies is for many games to package all dependent dynamic link libraries into the game.

Finally, give a good blessing: wish the world no more because of the environment and pain of the program ape!