What is cross-platform?
A simple example is that the program you write can run in windows and can run in linux after simple processing.
Standard definition: A program language, software, or hardware device can operate on multiple operating systems or computers with different hardware architectures.
Why do we need to focus on cross-platform platforms?
In fact, if you only want your program to run in a specific software and hardware environment, you do not need to pay attention to or consider cross-platform. However, such a program is not very portable. Once there is a need to run in a new environment or operating system, it usually takes a lot of effort to modify and transplant it, even the entire software system is re-designed, which is very costly and uneconomical.
Therefore, cross-platform is an important concept in software development, that is, the software to be developed does not depend on the operating system or the hardware environment, such software can be widely used.
So what hinders the implementation of cross-platform software?
Can an MFC application run in linux? Can the winSocket network communication program run in Unix? Is the application that uses the Access database to record system parameters portable? Can multimedia applications developed using the DirectX library be transplanted to other systems?
Obviously, the above applications do not have cross-platform performance, for the following reasons:
MFC is an application framework provided by Microsoft. It is closely coupled with the windows operating system. winSocket is a series of network programming APIs provided by windows, there are different API interfaces in other operating systems. The Access database is a Windows-based desktop relational database management system and does not have cross-platform performance, therefore, applications developed for it lose the cross-platform feature. DirectX is an application interface (or a third-party library ), it can achieve higher execution efficiency for games or multimedia programs based on windows.
From the above example, we can see what hinders cross-platform communication?
(1) Non-cross-platform application frameworks-such as MFC and WinForm
(2) APIs for specific operating systems-for example, windows APIs
(3) Non-cross-platform third-party software-for example, access Database
(4) non-cross-platform third-party libraries-for example: DirectX
(5) Non-cross-platform statements in the program -- for example: # prgram once, TRACE, Sleep, CString
At this point, we seem to have found a cross-platform approach, right! As long as we can avoid blocking cross-platform factors, we can implement the cross-platform features of our software. What are our choices on the road of cross-platform?
(1) Application Framework:
If we only develop a console project without a gorgeous or visualized interface, we do not need to select an application framework. If we need to develop a visual client, you can use cross-platform Qt or wxWidgets. Qt is a cross-platform C ++ graphical user interface application framework. It provides various functions required for application developers to create an artistic graphical user interface. wxWidgets is also an open-source cross-platform C ++ architecture library that provides a GUI) and other tools.
(2) operating system API
Currently, no unified API is supported by different operating systems at the same time. So what should we do when we need to use the API of the operating system? In fact, you can understand that the operating system APIs are just a series of functions, it provides encapsulated methods for applications such as file reading and writing, network sending and receiving, database access, memory management, and process threads. In fact, we do not have to use the APIS provided by the operating system to implement the corresponding applications. We can use third-party libraries to implement these operation functions! For example, the powerful boost Library provides comprehensive operation interfaces for the above applications. For example, the Filesystem module provides a series of operations for reading and writing files, for example, the Asio module provides a good encapsulation of network communication programming. For example, the Thread module provides a good implementation for creating and destroying process threads, the Smart Ptr module provides Smart pointers for good memory management. With these 3rd-party libraries and some containers and algorithms provided by STL, what applications do we have to use the operating system API to implement?
(3) third-party software
In fact, third-party software is not particularly difficult. In fact, it is easy to find third-party software that is not cross-platform to replace those software, although sometimes it will pay a small price. In fact, many database systems are cross-platform, such as mySQL and PostgreSQL. We can choose these cross-platform databases to prevent our cross-platform path from being blocked.
(4) third-party Libraries
As mentioned above, there are actually many good cross-platform third-party libraries, such as boost libraries, OpenGL libraries, and rtp applications, jrtplib libraries, and ACE libraries for network programming. For specific applications, I believe that you can always find some corresponding cross-platform libraries to meet your needs.
(5) programming statements
Similarly, we need to use some cross-platform statements to replace some non-cross-platform statements. For example, # prgram once can be replaced by the following statement.
# Ifndef XXXX_H _
# Define XXXX_H _
......
# Endif // XXXX_H _
Of course, for statements with no replacement, we can use another solution, that is, Conditional compilation, in the following form:
# If defined WINDOWS
Sleep (1000)
# Elif LINUX
Sleep (1000*1000)
# Else
......
# Endif
Speaking of this, I think everyone should be aware of the basic concepts and knowledge of cross-platform. However, it is still a long journey to really write cross-platform programs, because there are many other details that need to be accumulated in practice, below I will give you a few common questions as the end of this article!
1. Differences between file and directory case and path Separator
In windows, the path separator is case-insensitive. in linux, the path separator is case-sensitive and the path separator is "/". Note This when operating the file and directory paths in the program.
2. The file extensions in linux and windows are different.
For example, there are no exe and dll programs in linux, and the dll corresponds to a. o file in linux. Therefore, do not determine whether the system contains a XX exe program in the program.
3. Add one more line to the end of each. h file, otherwise there will be a warning during compilation in linux.
4. The number of bytes occupied by the basic type in C ++) changes with the length of the CPU. Therefore, if you want to represent the number of bytes occupied by an int, do not directly write "4", instead use the sizeof () function
5. Memory alignment
For the performance of CPU processing, the data in the struct is not close to each other, but to open some gaps. In this case, the address of each data in the struct is exactly an integer multiple of the length of a specific character. Since the C ++ standard does not define memory alignment details, your code cannot depend on alignment details. Sizeof () should be used to calculate the size of a struct ().
......
Okay, so much, I hope that my cross-platform software programming experience can help you, have any other questions, welcome to exchange: lujun_hust@126.com
This article from the "three shadows" blog, please be sure to keep this source http://ticktick.blog.51cto.com/823160/289618