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 in C++ to build a cross-platform application framework

2025-01-18 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 C++, which has a certain reference value, and interested friends can refer to it. I hope you can learn a lot after reading this article.

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

LibA: compile to get the library file libA.so/libA.a

LibB: compiled to get the library file libB.so/libB.a, which needs to call functions in the libA library

AppC: an application that calls functions in the libB library

III. Operation under Linux system

1. Generate Makefile file through cmake instruction

In order not to pollute the source code, we create a new build directory and compile in it:

$make build $cd build $cmake..

Compilation output:

two。 Compile libA

$cd libA/src $make

The compilation results are as follows:

Install to the output directory under the source code:

$make install

At this point, the relevant files are installed to the output directory of libA under the source code path:

3. Compile libB

Because libB calls functions in libA, you need to manually copy the relevant header files and library files to the libB directory. Of course, this step can also be written in CMakeLists.txt.

Then go to the build/libB/src directory and execute the compilation instructions:

$cd build/libB/src $make

Similarly, copy the library file and header file generated by libB to the libB/output directory in the source code:

4. Compile the executable program appC

Because appC calls functions in libB, you need to manually copy the relevant header files and library files to the include and lib/linux directories in the appC directory.

In addition, since I've been using dynamic libraries, I also need to copy the libA header files and library files to the appC directory.

Then go to the build/appC/src directory and execute the compilation instructions:

$cd build/appC/src $make

Execute the output result:

IV. Operation under Windows system

1. Generate VS solution

Execute cmake under the build directory to get the VS solution:

Open the project file CppFrame.sln, and the solution on the right is shown below:

two。 Compile libA

Right-click on libA_shared and select * * generate * *:

You can see the file generated by the compilation in the build\ libA\ src\ Debug directory:

Here is a problem to note: in libA/src/CMakeLists.txt, if you compile the dynamic library, set it as follows:

If you compile the static library, set it as follows:

These macro definitions need to be understood in combination with the definitions in ADll.h, mainly to solve the problem of exporting and importing dynamic libraries under the Windows platform.

The same is required when compiling the libB library below.

3. Compile libB

Because libB calls functions in libA, you need to manually copy the header files and library files related to the libA library to the libB directory.

On the libB_shared target, click "generate", and the compiled output is as follows:

At this point, in the build/libB/src/Debug directory, see the generated library file:

We manually copy the libB library file and header file to the appC directory for backup.

4. Compile appC

Click * * generate * * on the main target of VS, and the compiled output is as follows:

At this point, you can see the executable program main.exe in the build\ appC\ src\ Debug directory.

In order to execute this program, you also need to copy libA.dll and libB.dll to the current directory, as shown below:

Thank you for reading this article carefully. I hope the article "how to use Cmake to build a cross-platform application framework in C++" shared by the editor will be helpful to you. At the same time, I hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

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