In our development, the demand for cross-platform is more and more strong, how to keep the C + + code can be compiled on multiple platforms is a more worthy research problem. There are many cross-platform articles on the web, and there are many cross-platform libraries online. Then I from my own cross-platform development experience to talk about their experiences, I hope that everyone can play a role. Mainly related to Windows and Linux two operating systems.
1. Questions about path and header file path separators
In Windows, both the forward slash and the backslash are all possible, but in Linux, only/.
In Windows, path capitalization doesn't matter, and is strictly case-sensitive in Linux.
2. Char problem
If you consider cross-platform, you need to explicitly specify that signed or unsigned, because different platforms declare char directly, resulting in signed or unsigned uncertainty.
3, about the wide character problem.
In Windows, wchar_t accounts for two bytes, four bytes in Linux, but two bytes in Linux, which can also cause a problem in some third-party libraries where wchar_t may specify only four bytes, which can lead to incompatibility.
4, Linux there is no stricmp function, under Linux is the STRCASECMP function comparison string.
5, platform-related calls as far as possible with the macro isolation, general use of different directories to represent different platforms, BOOST, Ogre, etc. is to do so, can be another class or file, this will lead to the operating system and compiler-related macro definition.
6, about the header file contains
The header files for some C standard libraries under Windows do not have to be explicitly included, but they need to be explicitly included under Linux. Therefore, try to include the header files in the. C and. cpp files as needed in this file.
7. Notice the difference between the large tail end and the small tail end of the machine
Size end of the file read and write will have a great impact, to write a cross-platform C + + program, the size of the end of the problem must be considered. For example, you write a file on the big tail machine, and then read on the small end machine, then the result is certainly wrong, so, we design the file format, we need to specify whether the file is a large end-end storage or small end storage, or a file that some parts of the large end of certain parts are small tail end.
8. Try to use only the function or class that the STL appears earlier
Earlier things are relatively stable, STL implementations will basically be implemented, so that cross-platform can be compatible with multiple platforms.
9, the use of std::exception should be noted that the Linux is not supported to throw an exception, if the exception class inherited from the standard library to write its own exception class, under Linux, the subclass of the destructor needs to indicate that the exception is not thrown, so the destructor is followed by a throw () You can do it.
10. When inheriting template classes. Need to be cautious
In your own code, when you need to inherit the template class, if you need to access a member function or member variable of the base class template class, precede it with this->. In addition, constructors need to be constructed using a base class. The type of the base class needs to be initialized with the type parameter of the class, otherwise the name of the base class cannot be found under Linux.
11. Use the standard C and C + + functions as well as the STL, using the types defined in the C language.
12. Problems with duplicate header files
Try to protect the macro to achieve the prevention of duplication of header files, many code in Windows directly with #pragma once, which does not guarantee cross-platform needs.
13, about the structure of the alignment problem.
CPU in order to simplify the processing between memory and CPU and speed up the CPU to take data from memory, often do some alignment, that is, the members of the structure is not compact storage, often in the middle of the member to fill some bytes. Therefore, we generally do not recommend to use the structure to read and write data directly, so that the different systems or computers in the migration, there will be incorrect results.
In fact, these are just the tip of the iceberg, and more needs to be explored in cross-platform C + + development.
Considerations for cross-platform C, C + + code