Precautions for cross-platform C and C ++ code

Source: Internet
Author: User

Precautions for cross-platform C and C ++ code

In our development, the demand for cross-platform platforms is getting stronger and stronger. It is worth studying how to keep C/C ++ Code Compiled on multiple platforms. There are many cross-platform articles on the Internet and cross-platform databases on the Internet. So I will talk about my experiences from my cross-platform development experience, hoping to play a certain role for everyone. It mainly involves Windows and linux operating systems.

1. Question about path and header file path Separators

In Windows, both forward and backward slashes are allowed, but in Linux, only slash (/) is allowed /.

In Windows, the path is case-insensitive and case-sensitive in Linux.

2. char Problems

If you consider cross-platform, you need to specify either signed or unsigned. Because different platforms directly declare char, the uncertainty of signed or unsigned may occur.

3. Wide characters.

In Windows, wchar_t occupies two bytes. in Linux, wchar_t occupies four bytes, but in Linux, two bytes can be specified. This also causes a problem, in some third-party libraries, wchar_t may only specify four bytes, which leads to incompatibility.

4. There is no stricmp function in Linux. in Linux, The strcasecmp function compares strings.

5. Call related to the platform should be isolated using macros. Generally, different directories represent different platforms. BOOST and OGRE are used in this way. You can also use another class or file, this will result in macro definitions related to the operating system and compiler everywhere.

6. header file inclusion

In Windows, some C standard library header files do not need to be explicitly included, but must be explicitly included in linux. Therefore, try to include the header files in the. c and. cpp files.

7. Pay attention to the differences between the machine's big tail end and small tail end

The size and tail end have a great impact on File Reading and Writing. To write cross-platform c ++ programs, the size and tail end must be considered. For example, if you write a file on a machine with a large tail end and read it on a machine with a small tail end, the result is definitely incorrect. Therefore, when we design the file format, you must specify whether the file is stored at the end of a large object or at the end of a small object, or whether some part of a file is stored at the end of a large object or at the end of a small object.

8. Try to use only functions or classes that appear earlier in STL

The early appearance of things is relatively stable, and each implementation of STL is basically implemented, so that cross-platform compatibility with multiple platforms can be achieved.

9. When Using std: exception, you must note that exceptions cannot be thrown in LINUX. If the exception classes inherited from the standard library are written into your own exception classes, in Linux, in the destructor of the subclass, it must be indicated that no exception is thrown. Therefore, the Destructor can be followed by throw.

10. When the template class is inherited. Be cautious

In your own code, if you need to inherit the template class, add this-> before accessing the member functions or member variables of the base class template class. In addition, constructors need to use base classes for construction. The type of the base class must be initialized using the type parameter of the class. Otherwise, the system will prompt that the base class name cannot be found in linux.

11. Try to use standard C and C ++ functions and STL, and use the types defined in C language.

12. Repeated header file inclusion

Try to use security macros to prevent repeated inclusion of header files. Many codes use # pragma once in Windows, which is not required for cross-platform applications.

13. Questions about structure alignment.

In order to simplify the processing between memory and CPU and accelerate the speed at which the CPU extracts data from memory, the CPU is usually aligned to a certain extent, that is, the members of the structure are not compact, it is often filled with some bytes among the members. Therefore, we generally do not recommend that you use struct to directly read and write data. This will result in errors when porting between different systems or computers.

 

In fact, these are just the tip of the iceberg. More exploration is needed in cross-platform C/C ++ development.

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: info-contact@alibabacloud.com 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.