Cross-platform use of the Integral type, integral

Source: Internet
Author: User

Cross-platform use of the Integral type, integral
Fundamental integral typesOrExtended integral types

First, let's take a look at the integer types that can be used across platforms:

The reason why we need to specify the int type with the specified width is that the int type itself is quite special, the number of bytes is related to the machine length and compiler (the standard does not specify the number of bytes ).

Therefore, to ensure portability, we should try to use the int type with a width. This data type is allocated with the same bytes on all platforms, so there is no problem with porting.

Notes

We use int64_t as an example to describe it. We all know that int64_t is used to represent a 64-bit integer. It is a long int in a 32-bit system and a long int in a 64-bit system. Therefore, the formatting Method for printing int64_t is as follows:

printf("%ld" , value);  // 64bit OS  printf("%lld", value);  // 32bit OS

In this way, compiling the same code in 32-bit and 64-bit systems may cause errors. The cross-platform method uses PRId64 to format the output, as shown below:

#ifndef __STDC_FORMAT_MACROS#define __STDC_FORMAT_MACROS#endif#include <inttypes.h>printf("%" PRId64 "\n", value);

For details, see:


Note: The macro definition is for the C language. If C ++ needs to use a macro such as PRId64, You need to define a _ STDC_FORMAT_MACROS macro to open it. For details, see the definition of macro _ STDC_FORMAT_MACROS in/usr/include/inttypes. h, as follows:

/* The ISO C99 standard specifies that these macros must only be    defined if explicitly requested.  */  #if !defined __cplusplus || defined __STDC_FORMAT_MACROS    # if __WORDSIZE == 64  #  define __PRI64_PREFIX    "l"  #  define __PRIPTR_PREFIX   "l"  # else  #  define __PRI64_PREFIX    "ll"  #  define __PRIPTR_PREFIX  # endif    /* Macros for printing format specifiers.  */    /* Decimal notation.  */  # define PRId8      "d"  # define PRId16     "d"  # define PRId32     "d"  # define PRId64     __PRI64_PREFIX "d"
Example

The method mentioned above is also used in the MUDUO open source library. The source code is as follows:

#include <muduo/base/Timestamp.h>#include <sys/time.h>#include <stdio.h>#ifndef __STDC_FORMAT_MACROS#define __STDC_FORMAT_MACROS#endif#include <inttypes.h>#include <boost/static_assert.hpp>using namespace muduo;BOOST_STATIC_ASSERT(sizeof(Timestamp) == sizeof(int64_t));Timestamp::Timestamp(int64_t microseconds)  : microSecondsSinceEpoch_(microseconds){}string Timestamp::toString() const{  char buf[32] = {0};  int64_t seconds = microSecondsSinceEpoch_ / kMicroSecondsPerSecond;  int64_t microseconds = microSecondsSinceEpoch_ % kMicroSecondsPerSecond;  snprintf(buf, sizeof(buf)-1, "%" PRId64 ".%06" PRId64 "", seconds, microseconds);  return buf;}string Timestamp::toFormattedString(bool showMicroseconds) const{  char buf[32] = {0};  time_t seconds = static_cast<time_t>(microSecondsSinceEpoch_ / kMicroSecondsPerSecond);  struct tm tm_time;  gmtime_r(&seconds, &tm_time);  if (showMicroseconds)  {    int microseconds = static_cast<int>(microSecondsSinceEpoch_ % kMicroSecondsPerSecond);    snprintf(buf, sizeof(buf), "%4d%02d%02d %02d:%02d:%02d.%06d",             tm_time.tm_year + 1900, tm_time.tm_mon + 1, tm_time.tm_mday,             tm_time.tm_hour, tm_time.tm_min, tm_time.tm_sec,             microseconds);  }  else  {    snprintf(buf, sizeof(buf), "%4d%02d%02d %02d:%02d:%02d",             tm_time.tm_year + 1900, tm_time.tm_mon + 1, tm_time.tm_mday,             tm_time.tm_hour, tm_time.tm_min, tm_time.tm_sec);  }  return buf;}Timestamp Timestamp::now(){  struct timeval tv;  gettimeofday(&tv, NULL);  int64_t seconds = tv.tv_sec;  return Timestamp(seconds * kMicroSecondsPerSecond + tv.tv_usec);}Timestamp Timestamp::invalid(){  return Timestamp();}
Description

For compilers that support the C ++ 11 standard, you do not need to add macro _ STDC_FORMAT_MACROS or directly compile and pass the code.

References

1. http://www.cplusplus.com/reference/cinttypes? Kw = inttypes. h

2. http://www.cprogramdevelop.com/4787258/

3. https://github.com/chenshuo/muduo/blob/master/muduo/base/Timestamp.cc


Why can c be cross-platform or cross-platform?

The c language can be cross-platform because each platform has a corresponding c compiler. As long as the source code is the same, the compiled binary files will implement the same functions, but these binary files are different.

Exe is in binary format, but it is not entirely the code of the original program. It also includes some code of the windows system.

Popular Science: C first appeared in Unix, and Windows was the last word. When C appeared, Windows was not developed yet.
C has a special name, called intermediate language, because it has the features of advanced language and can interact well with underlying hardware. In the DOS era, it is very popular to embed a piece of assembly in C directly from the number of parallel reads. Now I haven't seen anyone doing this in VC. Windows is not allowed. How can I access the hardware through the driver.
To put it far away, C can share code on multiple platforms, and it needs to be re-compiled across platforms.
However, I have also seen some software that can run on multiple platforms. What special technology may be available? You can first identify the operating system and then run the corresponding code. This should be a special skill during compilation.
C compilation and connectors have ready-made code on the Internet. Generally, not very professional technical staff did not study it. I have always regarded compilation principles as a human being. It is not easy to read the book that has the ability to compile principles. If I can understand the principles, I will become a success, the first-class expert who can understand both the dragon book, Tiger book, and whale book.

What tools and knowledge are required for cross-platform development?

1. for C/C ++, what VS and GCC compile cannot be cross-platform (. except for the. Net platform. There are different compilers under different platforms. For example, you can use VC for compiling on windows platforms and gcc/g ++ for compiling on linux, in the embedded platform, the appropriate compiler must be used for different CPU types. Therefore, the purpose of the compiler is to translate the code you write into code that can be recognized by the CPU.
2. From the programming perspective alone, the standard code is cross-platform, but if you want to compile + run it, it requires the compiler on different platforms.
3. cross-platform implementation, such as JAVA. Net, can be run everywhere at a compilation, but C/C ++ can only run everywhere after compiling once.
4. Libraries under GNU are free of charge www.gnu.org/software/
5. I have never used PKCS. If it indicates that it supports cross-platform, the library it trusts should be the C/C ++ standard library, the compilers on Windows/unix platforms support both standard libraries. Therefore, you should be able to compile

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: 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.