One, foreword

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

This requirement is relatively easy to handle for the Linux family of platforms, and in most cases requires only a new cross-compiling tool chain, which involves the hardware platform-related parts and several inline assemblies.

However, for the Windows platform, it is a little more troublesome. You might say that minGW can build on A Windows platform with Cygwin, but can you expect customers to deploy a Linux-compatible environment when they install your application? The best solution is to use Microsoft’s own development environment, such as VS, etc.

In this article, we present a simple program describing how to use the cmake build tool to organize a cross-platform application framework.

Reading this article, you can gain the following knowledge points:

  1. Cmake in the compilation of library files, applications in the relevant instructions;

  2. Writing method of dynamic library export and import in Windows system;

  3. How to use macro definition for cross-platform programming;

In the public account background message [430], you can receive sample code. In Linux/Windows systems can be directly compiled, executed, ready to use.

Second, example code description

1. Function Description

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

2. File structure

  1. Common: Put some open source third-party libraries, such as: network processing, JSON format parsing, etc.

  2. Application: Application, a library generated using Utils;

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

  4. If you want to extend other modules, you can just copy one from the Utils file structure.

3. Cmake build procedure

Include cMakelists. TXT in other folders: cmakelists. TXT: cmakelists. TXT

4. Utils directory description

The compiled output of this directory is the library file:

Linux 系统:libUtils.so, libUtils.a;

Windows: libutils. DLL, libutils. lib, libutils. a;

The cmakelists. TXT file contains the following contents:

So far, the code is written to the simplest function, getSystemTimestamp(), which will be called in an executable application.

5. Application directory description

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

The contents of the cmakelists. TXT file are as follows:

3. Procedure in Linux

1. Create the build directory build

$ mkdir build
Copy the code

Compiling in a separate build directory and generating intermediate code that does not contaminate the source code is very convenient for using version control tools such as Git. You can simply ignore the build directory when you commit.

2. Run cmake to generate a Makefile

$ cd build
$ cmake ..
Copy the code

3. Compile the Utils library

$ cd Utils/src
$ make
Copy the code

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

$ make install
Copy the code

4. Compile the executable program Application

The libutils. so library is used by the Application, so you need to manually copy libutils. so and header files to the corresponding lib/ Linux and include directories under the Application.

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

$ cd build/Application/src
$ make
Copy the code

You can see the output by executing the generated executable program main.

4. Procedure in Windows

1. Generate VS project by cmake command

In the same way, create a new build directory and run cmake.. To generate a VS solution, I’m using VS2019:

2. Compile the Utils library

Use VS2019 to open the project file demoapp.sln. In the solution on the right, you can see:

Right-click libUtils_shared and select “Generate” :

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

3. Compile the executable program Application

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

In the VS Solution window, right-click on the main target and select Generate:

At this point, you can see the generated executable program under build\Application\ SRC \Debug:

Click on main.exe to execute.

You need to copy the libutils. DLL dynamic library file to the directory where main.exe resides, and then execute the file.

Five, the summary

This article is based on dynamic libraries. If you compile and use static libraries, the execution is the same.

If there is any problem in the operation process, please leave a message and discuss. Thank you!

In the public account background message [430], you can receive sample code. In Linux/Windows systems can be directly compiled, executed, ready to use.

Good luck!

















1. C Pointers – from basic principles to fancy tricks, illustrated in graphics and code. The original GDB bottom debugging principle so simple 3 step by step analysis – how to use C object-oriented programming 4. How to use Google protobuf to think, design and implement their own RPC framework 5. It is said that software architecture should be layered and divided into modules. What should be done specifically? (I)