A Simple Method for porting Linux code to Windows-general Linux technology-Linux programming and kernel information. The following is a detailed description.
I. Preface
Linux has a variety of source code resources, but most of the Code cannot be compiled normally on the Windows platform. Windows platform cannot directly use these source code resources. If you want to use the complete code, you need to transplant it. Because of the differences between C/C ++ Library and other reasons, porting C/C ++ code is a difficult task. This article uses a practical example (Tar) to describe how to port Linux code to the Windows platform. The porting process will try to modify the code as little as possible so that the code running logic will not change. Retain the main functions of most software.
2. Preparations
Tar is a packaging tool on the Linux platform. What does porting such a program to a windows platform require?
First, some preparation work. Install the latest Cygwin version on the Windows platform and install GCC and other development tools in Cygwin. A Windows development environment is also required. You can use the latest Visual Studio version, Microsoft Visual Studio. NET 2003. Obtain the latest Tar source code from www.gnu.org. The version is 1.13. Open the tar-1.13.tar.gz. source code package under cygwin. Do not use WINRAR or WINZIP for decompression in Windows. Winrarand winzips may encounter problems when Uncompressing some tar.gz packages. Causes an exception to directories and files after unpacking. If it is a source code package, it may not be compiled correctly under Cygwin. After unzipping the package, go to the tar-1.13 directory and enter the./configure command under the current directory. After running the command, enter the make command again. Start to compile the Cygwin version of tar. Basically, compilation does not involve any questions. Go to the srcdirectory to see the newly compiled tar.exe program.
Cygwin is an API-layer Linux Simulation Environment. If it can be compiled and run under Cygwin. In fact, it can be compiled and run in Windows, but it only requires an intermediate API to simulate some Linux-specific operations. Simply determining whether a Linux program can be transplanted to the Windows platform is to see if the source code can be compiled and run under Cygwin.
Compile the Tar source code in Cygwin to determine whether the source code can be transplanted. Another reason is that a special header file config. h is required during code porting. Config. h is the most important source code file in the porting process. The Config. h file is not part of the source code. The file is generated when you run the "./configure" command under Cygwin. When running the "./Configure" command under Cygwin, the config. h file is generated according to the development environment of Cygwin. During compilation, the config. h file is also required to control the code compilation items. The porting is also based on the config. h file.
The next step is to construct a Windows project. Use Visual Studio. NET 2003 to create an empty Project named WinTar. According to the compilation output information in Cygwin, the main Tar code is in the Src and lib directories. Copy the two directories to the new job and add the code to the project. Copy Config. h to the WinTar project directory.
The preparation is complete, followed by transplantation. The porting process can be divided into three parts.
Iii. First goal: Enable WinTar to compile (Compiler)
The completion of the first goal is mainly centered on Config. h. In Linux, the development environment and Windows development environment are very different because the C Library header file and various types of definitions are different. Config. h provides a complete compilation switch to handle the differences between different platforms in different development environments. Now you need to manually modify this file so that the Tar source code can adapt to the Windows platform.
First, adjust the inclusion of various C Library Header files. Many definitions in Config. h are similar to HAVE_XXXX_H. For example, if HAVE_CONFIG_H is defined as 1, config. h can be used in the project.
# Define HAVE_MALLOC_H 1 indicates that the Malloc. h header file can be used in the project. By adjusting these definition values, you can remove header files that are not included in Windows. There may be many header file inclusion relationships to be processed in other places, but the definition here basically solves most header file inclusion problems.