Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Cmake to build a cross-platform application framework

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "how to use Cmake to build a cross-platform application framework". In daily operation, I believe many people have doubts about how to use Cmake to build a cross-platform application framework. I have consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to use Cmake to build a cross-platform application framework". Next, please follow the editor to study!

I. Preface

In the process of writing applications, we often have to face a development scenario: writing cross-platform applications.

This requirement is relatively easy to deal with for Linux series platforms. In most cases, only a cross-compilation tool chain is needed, and several inline assemblers are embedded in the relevant parts of the hardware platform.

However, for the Windows platform, it is a little more troublesome. You might say that with cygwin on the Windows platform, minGW can also be compiled uniformly, but can you expect customers to deploy a Linux-compatible environment when installing your program? The best solution is to use Microsoft's own development environment, such as VS and so on.

Before, I once uploaded a similar short video on bilibili, using C++ to write a cross-platform application structure. Later, a small partner wanted to provide a C language version, so there was this article.

In this article, we will use the simplest program to describe how to use cmake as a build tool to organize a cross-platform application framework.

By reading this article, you can gain the following knowledge points:

Related instructions of cmake in compiling and storing files and applications

The method of dynamic Library Export and Import in Windows system

How to use Macro Definitions for Cross-platform programming

Second, the sample code illustrates 1. Function description

The main purpose of the sample code is to describe how to organize a cross-platform application structure. Its function is relatively simple, as shown in the following figure:

two。 File structure

Common: place some open source third-party libraries, such as network processing, json format parsing, etc.

Application: applications, using libraries generated by Utils

Uitls: put some tools and helper functions, such as file processing, string processing, platform-related helper functions, etc., and finally compile the library files (dynamic library libUtils.so, static library libUtils.a)

If you extend other modules, you can copy one according to the file structure of Utils.

3. Cmake construction steps

In the root directory of the sample code, there is a "header" CMakeLists.txt file, which is mainly used to set compiler and compilation options, and then go to the CMakeLists.txt file in include other folders, as follows:

4. Utils directory description

The compiled output of this directory is the library file:

Linux system: libUtils.so, libUtils.a

Windows system: libUtils.dll, libUtils.lib, libUtils.a

The CMakeLists.txt file is as follows:

Currently, only the simplest function, getSystemTimestamp (), is written in the code, which will be called in the executable application.

5. Application directory description

The compiled output of this directory is an executable program that calls functions in the libUtils library.

The CMakeLists.txt file is as follows:

Third, under the Linux system operation step 1. Create a build directory build$ mkdir build

Compiled in a separate build directory, the generated intermediate code will not pollute the source code, so it is very convenient to use version control tools such as git. When submitting, you only need the ignore build directory. It is highly recommended to deal with it in this way.

two。 Execute cmake to generate Makefile$ cd build $cmake..

3. Compile the Utils library $cd Utils/src $make

The last part in CMakeLists.txt is the installation instruction, which installs the resulting library files and header files into the install directory in the source code.

$make install

4. Compile the executable program Application

Application uses the libUtils.so library, so you need to manually copy the libUtils.so and header files to the corresponding lib/linux and include directories under Application.

Of course, you can also write this operation in the Utils installation command.

$cd build/Application/src $make

Execute the generated executable program main, and you can see the output.

4. Operation step 1 under Widnows system. Generate VS project through cmake instruction

By the same token, create a new build directory and execute cmake.. Directive to generate the VS solution, I used VS2019:

two。 Compile Utils library files

Open the project file DemoApp.sln using VS2019, and in the solution on the right, you can see:

Right-click on libUtils_shared and select "generate":

At this point, under the directory build\ Utils\ src\ Debug, you can see the generated file:

3. Compile the executable program Application

Because Application needs to use Utils-generated libraries, you need to manually copy the libraries and header files to the lib/win32 and include directories under Application.

In the VS solution window, on the main target, right-click and select "generate":

At this point, you can see the generated executable program in the directory build\ Application\ src\ Debug:

Click main.exe directly to execute and report an error:

You need to copy the libUtils.dll dynamic library file to the directory where main.exe is located and then execute it to be successful.

At this point, the study on "how to use Cmake to build a cross-platform application framework" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report