C++ linux/MacOS环境开发和windows环境开发感觉是两个世界,公司招聘C++程序员的时候都会强调是哪一个平台。当然C++开发的区分不止于平台,还有前端后端,不同业务的区别。本文以有限的水平记录一下跨平台开发一点小经验。
在Linux环境中我们常用gcc/g++编译器,大的项目会用到makefile, scons等编译工具, linux有其独有的库,如sys/socket., unistd.h等。
在Windows环境开发一般就是用Visual Studio编译。
C++是编译型语言,不像Java有“虚拟机”, 一般来说Windows和Linux两套平台需要两套程序,但也能通过一些方法实现跨平台,即两个平台使用一份代码编译。
方法如下:
- 尽量使用跨平台库如:STL, Boost, Qt等
- 如不可避免和系统有关,使用#ifdef区分
- 使用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