Use the program c ++ cross platform development

Use the program c ++ cross platform development

background

During development, use c ++ as a development language, generally considered to be a painful, long-winded, long development time. There are a wide variety of languages ​​in recent years is widely used, relatively speaking than c ++ is not so out of color. c ++ though older, but it is not standing still, which he himself was a sharp change in recent years, in order to facilitate the development of c ++, the emergence of a lot of tools. I combine their work experience, going to write something, tell us.

C ++ was born early solve the very real problem, but as times change c ++ encountered new situations.

The first is the package dependency management, c ++ does not have in it the constraints, it led to the development in the management of dependencies when, very difficult, and sometimes can be said to be a mess .c ++ situation, a bit like the previous nuget appear c #, maven appear before the java but now have been resolved Microsoft launched vcpkg this tool completely solved the problem, of course, for c ++ package management, there are other tools here, I only recommend vcpkg the following reasons:.... 1 it is in line with the modern c ++ development process .2. it's really a good management of the packages and package dependencies, and header files .3. it is easy to use. during development mode of operation for each platform are the same.

Secondly, the project was organized, c ++ does not have this constraint on Well, the times which does not take into account future situation I choose here cmake scripts A:... I am writing c ++ Code, B: specific job? a: you refer in particular to write c ++ code to write code c ++ code and write code c ++ file .... Although the piece, but cmake is the code c ++ file is now regarded as standard c ++ development of a?.

Development tools again, where there are many choices, vs and vscode, and clion. I chose clion.

Finally, the development of the operating system, since it is cross platform development program, development platforms also need to choose me, I chose to win10. Linux desktop is not selected. Reason is simple, windows tool too much. I am now the age has been great.'Ve after a need to use the operating system to reflect the stage of self-force grid. I have no intention argue that the system is good or bad. I need now is the time of life. (but most farmers lack the code is time, in fact, the main reason is the work exchange use a win system) say these tools are cross-platform, can seamlessly migrate to win or linux desktops up real completely seamless operation mode does not bring any change.

In conclusion of this paper is to use clion + cmake + vcpkg development of modern c ++ cross-platform program.

The basic organization of the project

The project is written in win10, I is not afraid to write something, I'm just afraid a problem, I do not know where the problem lies. So compiling and debugging also occupy a very important position, it is remotely operated.

If the draw a diagram, it can be represented as follows.

clip_image002[9]

As can be seen above, is removed in the debug windows, the rest of the compiler and debugger, then no rows each platform again. The actual development process, compile and debug generally only need to be at the distal end of linux, win systems generally do not the need for any changes. this is completely due to cmake and vcpkg (really highly recommended, we can say, if there is no vcpkg, I can not write this stuff).

Long-winded so much, and now finally to enter the body.

Environment with

Linux environment ready

Prepare a linux operating environment, I am going to be ubuntu18.04.

Installed on the Ubuntu git, cmake, gcc7.3.0. These three online installation tutorial dime a dozen. Here is not to repeat it. Now I installed on my machine environment screenshots below.

clip_image004[10]

Installation vcpkg.

1. Create a directory / vcpkg

2. The input command clone Git HTTPS: //github.com/microsoft/vcpkg

3. Go to the directory / vcpkg,

4. Compile sudo bash ./ bootstrap-vcpkg.sh, generates an executable file vcpkg, do not install to any other location, it generated enough to stay in place.

5. 定义环境变量 VCPKG_ROOT="/vcpkg"

6. 安装gdb和ssh,这个是clion远程调试要使用的.

Windows环境准备

我使用的是 win10.安装了git,cmake,vs2019.

再安装vcpkg.定义一个环境变量VCPKG_ROOT="/vcpkg"

管理包

现在使用vcpkg安装一些必要的c++包.

比如,我安装了boost包.

vcpkg install boost

clip_image006[9]

查看一下.vcpkg list或者vcpkg list boost,部分截图如下

clip_image008[9]

clip_image010[9]

我只是为了嘚瑟一下而已,列表是在太长了.嘚瑟是程序员的必要需求.

Vcpkg的基本文件结构

vcpkg会在VCPKG_ROOT所指向的目录底下存储所有的包,

linux示意如下.

clip_image012[9]

Windows示意如下

clip_image014[9]

vcpkg主要使用的命令就是

vcpkg search xxxx ##搜索一个特定的c++库

vcpkg list xxxx ##显示已经安装的c++库.

vcpkg install xxxx ##安装一个特定的库

如果某个库文件有,而vcpkg没有收录.可以去研究一下ports/ 目录底下的各个文件夹.里面的文件指示vcpkg应该如何搜索特定的包.如果查看大部分portfile.cmake文件,就发现,vcpkg大部分库来自于github.这里只是提提,就不展开了.大家自己研究.

与Clion互动

安装clion,我这里使用的是2019.1.3版本.安装就不复述了.废话一句就是,有钱捧钱场,没钱的捧人场.

我们使用vcpkg安装好了各种库,目的是为了方便的使用,让vcpkg帮我们管理各种各样的头文件啥的.我们只需要放肆的编写bug,嗯…我们只需要专注于实现业务逻辑.这一切有一个统一的界面呈现给我们,这个界面就是clion.

如果要添加一个新的库文件一般就是如下流程

vcpkg install xxxx ----àclion更新cmake-à放肆的使用新安装的库.-à编译-à调试.

以上各个步骤,第一步需要手动输入一个指令外,第二步需要更改一点cmake文件.除去以上步骤以外,剩余的所有步骤都不需要手动干预.这在vcpkg出现之前是不敢想象的.之前光引入头文件就劝退了多少人呀.如果再加上编译,连接库文件等等动作…c++又名c艹是非常有道理的.

现在第一步手动输入指令与第二步更改cmake文件,应该都不算是负担了,如果还觉得是负担,可以说只能等AI自动编程来拯救了.(我用脸滚了一晚上键盘,编写处理新一代操作系统windoors,不是梦).

下面通过一个简单的项目把项目建立到调试,再过具体一点.

Hello world

现在的主角就要转向clion了.我打算建立一个简单的c++程序,这个程序可以在linux上编译调试,同时也可以在win上编译调试.

我使用clion建立了一个新项目,截图如下

clip_image016 [9]

以下,是我的编译工具链的设置.找到菜单 File->Settings->Build,Execution->Toolchains.截图如下

clip_image018 [9]

找到File->Settings-> Build,Execution->Deployment.配置一个远程linux主机.截图如下

clip_image020[9]

因为要远程编译,必然的要把本地的源代码传送到远端linux里去.这里就是指示clion如何对应本地项目目录和远端目录的.其中Mapping指定了如何对应.大家可以根据自己的需要设置.下图我设置成立项目根目录对应着/root/clionproject/test/.

clip_image022 [9]

更改以上两个,下面要配置clion当中的cmake了.这里才是真正使得clion和vcpkg互动起来.找到File->Settings-> Build,Execution->CMake,在ToolChain选择我们刚刚设置的工具链之一.

 clip_image024[9]

其中CMake options要填入:

-DCMAKE_TOOLCHAIN_FILE=/vcpkg/scripts/buildsystems/vcpkg.cmake

这样vcpkg就可以和clion互动起来了.clion使用vcpkg.cmake文件,此文件会指导cmake来寻找vcpkg管理的各种库文件,并指导clion使用它们.

依照同样的方式创造一下配置,使得clion也可以在vs下编译此程序.

clip_image026[9]

下面我编写一个简单的hellow world程序,此程序使用boost,编写一次,分别在linux和windows下编译(一次编写,处处编译),并打印出此时的操作系统类型.

1. 在Clion中创建一个新的项目:略

2. 添加Boost库到项目中

使用以上配置使得条件boost库变的简单.

在CMakeLists.txt中编辑如下内容

clip_image028[9]

3. 添加业务逻辑

里面有两个文件main.cpp.

clip_image030[9]

OperationSystem.hpp

clip_image032[9]

逻辑都超级简单,就不复述了.

4. 最后编译.只需要选择配置,然后点击编译就可以了.截图如下

clip_image034 [10]

以上截图是linux,windows也是一样的.就不截图了.

一次编辑,处处编译

考虑以上过程,如果一套代码你再一台机器编辑好.比如在windows编辑好.然后复制到linux底下编译一下,有没有需要更改的地方?从以下几个方面检查一下

Cmake:一套配置文件在linux和windows底下是相同的,不用更改.

依赖库:都统一使用vcpkg管理依赖库.在linux和windows下是相同的,也不用更改.

项目代码:也不用更改.

编译命令:都是cmake自动操作的.也是一样的.

发现:基本上做到了一次编辑,处处编译.由于c++历史的原因,想要完全做到此特点,也着实难办.对于一些特殊的需求,特别是硬件需求,低层需求,还是要使用预编译符号或者cmake来针对不同操作系统,区别对待.虽然不能不能做到如netcore一样真正一次编辑处处编译.毕竟那是有大厂支持的.可以说,通过以上的介绍,基本上实现了”一次编辑,处处编译”.一套代码中只有非常少量的几行代码需要针对操作系统特殊处理.

另一个由”处处编译”带来的好处就是可以自动打包.通过git上传代码至代码库,然后在目标机器中使用自动打包工具自动编译.

C++工具

格式化工具

Clion内置了一个clang格式化工具.可以使用它来格式化自己的代码.这个不多赘述了.

内存泄漏

很多语言都内置了自动垃圾回收所谓GC,比如net,go等.但是有很多时候,由于设备或者效率的要求,使得运行环境不适合GC.但是内存回收依然十分的重要.而C++的内存泄露却总是使得各位码农花费很多时间去避免.如剃刀一般剃光了很多人的头发.

C++发展了很多工具,来避免内存泄露,如智能指针.但是由于库或者历史的原因,使得智能指针不能覆盖全部代码.现在有工具来解决这个问题了.有时候,我总是觉得,造东西不怕,我是怕出问题了却不知道在哪(所以测试很重要).谷歌出了一套工具可以侦测内存泄露.名字叫做sanitizers.

下面我先截图一下,看看如果发生了内存泄露大约给出一个什么样子的提示:

clip_image036[9]

在上图中可以发现,在程序退出的时候,它打印了一些信息,明确的指出哪里(具体到了行)泄露了,泄露了多少字节.由于GCC内置了这个sanitizers.所以这里我直接使用了GCC内置的sanitizers.

下面我将使用此sanitizers.

程序很简单,我改造main.cpp,截图如下

clip_image038[9]

上段申请了一段内存并没有释放.

在cmake文件中添加以下一句

SET(CMAKE_CXX_FLAGS "-fsanitize=address")

它目的就是告诉编译器,增加一个flag sanitize=address .

现在我在linux环境中编译运行,得到如下截图.

clip_image040[9]

可是问题来了.如果同样的代码在windows底下运行,就会获得如下错误.

clip_image042[9]

Fortunately, memory leaks only need to check just once. So I tend to modify cmake, so that it will only use sanitize under the gcc compiler. And do not use this flag at msvc. Do not mess flag stand.

When the program before the end of the long position reporting memory leaks, msvc there can refer to this article. Https://docs.microsoft.com/en-us/visualstudio/debugger/finding-memory-leaks-using- the-crt-library? view = vs-2019

Meanwhile, if instead of using the clang-cl cl. Can also be used sanitize, but there are restrictions on some functions. If a detailed view Microsoft article, you will find, in fact, Microsoft's CRT memory leak detection, a more flexible and comprehensive. Function More powerful.

The above code is simple demo code logic is very simple. I am not on the source. Experimenters determined to follow his own picture a little knock it.

Google is also sanitize a ThreadSanitizer, designed to detect resource contention and deadlocks. Use is the same.

Guess you like

Origin www.cnblogs.com/gaopang/p/11243367.html