Cross-platform C ++ development experience

Source: Internet
Author: User
Cross-platform C ++ development experience

These are some of the problems we encounter during the development process and during the porting process.LinuxDifferences between programming and windows programming, as well as some solutions and experiences from the Internet.

1. Differences between file and directory case and path separator.

It is case-insensitive in windows, and the path separator is generally "/";LinuxIt is case sensitive and the path separator is "/".

2. the ITOA () function is inLinuxDoes not exist.

So using functions like sprintf (); is a good alternative. (For more information, see the following)

3. Differences between _ snprintf () and _ vsnprintf.

Sprintf () does not check the length of the target string, which may cause many security problems. Therefore, we recommend using snprintf ().

int _snprintf(
   char *buffer,
   size_t count,
   const char *format [,
   argument] ...
);

For example, it is safer to write the C2 character to C1:

char c1[256];
sa[sizeof(c1)-1] = 0;
_snprintf(c1, sizeof(c1), "%s", c2);
if(c1[sizeof(c1)-1]!=0)
{
   cout<<"warning: string is truncated")<<endl;
   c1[sizeof(c1)-1]=0;
}

If you take the initiative to check the length of C2 before applying for C1 space before writing, of course, it is the best.

4. InLinuxSTD: exception (char *) does not exist. You can use the logic_error type instead of exception.

5. Try to use STL and the Standard C library, and write programs according to the standard C ++ syntax.

For example, in this usage list <type1 <t0>: iterator ITER, there is no compilation error in the Windows compiler, but inLinuxNext, you need to add typename before it can be compiled successfully.

6. Add one or more empty lines at the end of the source file to avoid warning: "No newline at end of File ".

7. All functions for INI File Operations in windows areLinuxNo corresponding function.

We recommend that you write a class that operates the INI file by yourself. On stlchina, you can use STL to create this type of principle and a small segment of code to expand it.

8. Use fd_set instead of fd_set for the socket object set.

9. stricmp () function, inLinuxUse the strcasecmp () function.

10. InLinuxThere are no macro definitions such as invalid_socket, invalid_handle_value, socket_error, max_path, infinite, and still_active.

11. When the parameter requirement is char *, do not forget to add a. c_str () function to the passed string.

12.LinuxDirectory operation does not exist underDirect. hThe header file must contain the corresponding unistd. h and fcntl. H files.

13. The filelength () function isLinuxIt does not exist. It can be replaced by the fstat () function.

Use file and directory operation functions. Do not use functions similar to _ access with underscores. Instead, use access functions.

14. # prgram once Pre-compiled command inLinuxDoes not work, please use the following standard method to solve the problem of multiple header file inclusion:

#ifndef _XXXXX_H_
#define _XXXXX_H_
#endif

15. hash_map appears in the later version of STL library. To balance HASP and tree map, use the following macro to solve the problem:

// process hash_map

#if defined(_MSC_VER)
#if _MSC_VER >= 1300
// hasp_map not standard container

#pragma warning(disable: 4996)
#include // we believe _STLP_STRING included means using STLPORT

#elif defined(_STLP_STRING)
#include #else
#define hash_map map
#endif
#endif

16. To differentiate operating systems, the following macros are generally available: Win32 ,__Linux__, OS-related content is required. Please use the switch to write it.

17. For some code operated on the interface, it must be separated from the core code of the Program Logic using macros. Such code can be transplanted without being staggered.

18. The executable file must have an extension such as .exe.

19. The type defined by typedef in the parent class cannot be used directly in the subclass.

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.