C++跨平台开发Linux&Windows

36 篇文章 1 订阅

C++ linux/MacOS环境开发和windows环境开发感觉是两个世界,公司招聘C++程序员的时候都会强调是哪一个平台。当然C++开发的区分不止于平台,还有前端后端,不同业务的区别。本文以有限的水平记录一下跨平台开发一点小经验。

在Linux环境中我们常用gcc/g++编译器,大的项目会用到makefile, scons等编译工具, linux有其独有的库,如sys/socket., unistd.h等。

在Windows环境开发一般就是用Visual Studio编译。

C++是编译型语言,不像Java有“虚拟机”, 一般来说Windows和Linux两套平台需要两套程序,但也能通过一些方法实现跨平台,即两个平台使用一份代码编译。

方法如下:

  1. 尽量使用跨平台库如:STL, Boost, Qt等
  2. 如不可避免和系统有关,使用#ifdef区分
  3. 使用CMake编译程序

举例:


// crossplatform.h
#ifndef CROSSPLATFORM_H_
#define CROSSPLATFORM_H_

#ifdef WIN32

#include <windows.h>
#include <WinSock2.h>

#else //WIN32

#include <unistd.h>
#include <sys/socket.h>

#endif //WIN32

void sleepMilliseconds(unsigned int milliseconds)
{
#ifdef WIN32
    Sleep(milliseconds);
#else
    usleep(milliseconds*1000);
#endif
}

void sleepSeconds(unsigned int seconds)
{
    sleepMilliseconds(seconds * 1000);
}

#endif // CROSSPLATFORM_H_


//main.cpp
#include <iostream>
#include "cross-platform.h"

int main()
{
    std::cout << "c++ cross platform development" << std::endl;
    sleepSeconds( 5 );
    std::cout << "exit..." << std::endl;
}
# CMakeLists.txt
cmake_minimum_required (VERSION 2.8)
project(CrossPlatformCPP)
if(WIN32)
    messages("build on Windows")
else()
    messages("build on Linux")
endif()
add_executable(CrossPlatformCPP main.cpp)

cmake命令如下:

$ mkdir build
$ cd build
$ cmake ..
$ cmake --build .

在linux环境输出为:

$ cmake …
– The C compiler identification is GNU 4.8.4
– The CXX compiler identification is GNU 4.8.4
– Check for working C compiler: /usr/bin/cc
– Check for working C compiler: /usr/bin/cc – works
– Detecting C compiler ABI info
– Detecting C compiler ABI info - done
– Check for working CXX compiler: /usr/bin/c++
– Check for working CXX compiler: /usr/bin/c++ – works
– Detecting CXX compiler ABI info
– Detecting CXX compiler ABI info - done
== build on Linux
– Configuring done
– Generating done
– Build files have been written to: ***
$ cmake --build .
Scanning dependencies of target CrossPlatformCPP
[100%] Building CXX object CMakeFiles/CrossPlatformCPP.dir/main.cpp.o
Linking CXX executable CrossPlatformCPP
[100%] Built target CrossPlatformCPP

在windows环境中为:

>cmake …
– Building for: Visual Studio 14 2015 – Selecting Windows SDK version to target Windows 10.0.17763.
– The C compiler identification is MSVC 19.0.24215.1
– The CXX compiler identification is MSVC 19.0.24215.1
– Detecting C compiler ABI info
– Detecting C compiler ABI info - done
– Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/cl.exe - skipped
– Detecting C compile features
– Detecting C compile features - done
– Detecting CXX compiler ABI info
– Detecting CXX compiler ABI info - done
– Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/cl.exe - skipped
– Detecting CXX compile features
– Detecting CXX compile features - done
== build on Windows
– Configuring done
– Generating done
– Build files have been written to: ***
>cmake --build .
Microsoft ® 生成引擎版本 14.0.25420.1
版权所有© Microsoft Corporation。保留所有权利。
Checking Build System
Building Custom Rule C:/Users/Administrator/Documents/test/CMakeLists.txt
test79-main.cpp
c:\users\administrator\documents\test\test79-cross-platform.h(28): warning C4244: ‘argument’: conversion from ‘double’ to ‘unsigned int’, possible loss of data [C:\Users\Administrator\Documents\test\build\CrossPlatformCPP.vcxproj]
CrossPlatformCPP.vcxproj -> C:\Users\Administrator\Documents\test\build\Debug\CrossPlatformCPP.exe
CrossPlatformCPP.vcxproj -> C:/Users/Administrator/Documents/test/build/Debug/CrossPlatformCPP.pdb (Full PDB)
Building Custom Rule ***/CMakeLists.txt

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值