This problem is basically encountered by all languages. Even if JAVA is compiled once and can be run everywhere, it is because the virtual machine helps to cover up the implementation details of various platforms, leaving only unity for the upper layer. interface. Although the C++ standard library does not provide a corresponding solution, macro facilities such as #ifdef can help us span different platforms. If the workload is heavy, you can simply use a third-party cross-platform library for C++. , the workload is small, it is recommended to encapsulate it a little yourself. Third parties can use libraries such as boost and QT. For simple sleep, c++11标准模板库 provides std::this_thread::sleep_for
Encapsulation. Encapsulate all functions that may cause platform problems yourself, and then use conditional compilation to distinguish them. The sleep you mentioned is just a simple problem, and many details of complex ones such as socket are different.
You need to encapsulate the differences between platforms yourself
This problem is basically encountered by all languages. Even if JAVA is compiled once and can be run everywhere, it is because the virtual machine helps to cover up the implementation details of various platforms, leaving only unity for the upper layer. interface.
Although the C++ standard library does not provide a corresponding solution, macro facilities such as
#ifdef
can help us span different platforms. If the workload is heavy, you can simply use a third-party cross-platform library for C++. , the workload is small, it is recommended to encapsulate it a little yourself.Third parties can use libraries such as
boost
andQT
. For simple sleep,c++11标准模板库
provides std::this_thread::sleep_forYou can use boost library: http://www.boost.org/ Each API has been packaged across platforms.
Any issues with the code can be solved by adding layers.
Use macros.
ifdef determines whether it is win32, if not, define Sleep(xxxxx) sleep(xxxx/1000)
Or directly use the libc function
There is another way, compile with cygwin, but this will have dependent dll
Encapsulation. Encapsulate all functions that may cause platform problems yourself, and then use conditional compilation to distinguish them. The sleep you mentioned is just a simple problem, and many details of complex ones such as socket are different.
You can use tbox + xmake for perfect cross-platform development. .
http://xmake.io
http://tboox.org