Considerations for cross-platform C, C + + code

Source: Internet
Author: User

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.
14. Note The trap of BOM (byte order mark)
If you create a source file in Windows with Notepad, then Windows will put a BOM tag at the top of the file, the so-called byte order mark, so that the source code is not a problem in Windows, but it is compiled under Linux, So you need to use a different text editor or create a source file directly in vs. Linux under gcc/g++ does not recognize the source file with the BOM tag.
15. Note that the parameter type and the type of the argument list in the function declaration do not match when the function is called. This refers to whether there is a const or a reference parameter. There is no problem with the CL compiler under Windows, and Linux gcc/g++ will error.
16. Note that two angle brackets should not be written in an attached. For example std::vector<std::vector<int>> VEC; Under Windows This is completely fine, so under Linux it is compiled, so Linux can leave a space between two consecutive angle brackets, namely std::vector<std::vector<int> > VEC;

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.