GCC 64-bit program makefile condition compilation experience-32-bit and 64-bit version, debug version and release version (compatible with mingw, TDM-GCC)

Source: Internet
Author: User

Author: zyl910

To adapt to the increasing popularity of 64-bit systems, it is often necessary to compile the code into 32-bit and 64-bit versions respectively. Second, in addition to generating the debug version for development and testing, you also need to generate the release version for release. This article describes how to use makefile Conditional compilation to generate these versions, and not only compatible with GCC in Linux, but also supports GCC compiler in windows such as mingw, TDM-GCC.

I. C program code

To test the effect of Conditional compilation, the following C language program is used as an example (gcc64_make.c )--

# Include <stdio. h> # include <assert. h> // obtain the number of digits of the program (the Code compiled into) int getprogrambits () {return sizeof (int *) * 8;} int main (INT argc, char * argv []) {printf ("bits: \ t % d \ n", getprogrambits (); Assert (argc> 1); Return 0 ;}

 

In the main function, the meaning of the first two statements is --
The first statement is used to display the number of digits of the current program. If it is compiled into a 32-bit version, "bits: 32" is displayed. If it is compiled into a 64-bit version, "bits: 64" is displayed ".
The second statement is an assertion. The argc variable must be greater than 1. If the program is compiled to debug, if the command parameter is not added during running, the asserted fails, and the program is output and terminated. If the program is compiled to release, all assertions are blocked, no error message.

Ii. GCC command line parameters

Review the GCC command line parameters to see the differences between different versions --
32-bit version: add the-M32 parameter to generate the 32-bit code.
64-bit version: add the-M64 parameter to generate 64-bit code.
Debug: add the-G parameter to generate debugging information.
Release: adds the-static parameter to the static link, so that the program no longer depends on the dynamic library. Add the-O3 parameter for the fastest speed optimization. Add the-dndebug parameter to define the ndebug macro to block assertion.

When the-M32 or-M64 parameter is not specified, the Code with the same number of digits as the operating system is generally generated, but some compilers have exceptions, such --
In 32-bit Linux, GCC is compiled as 32-bit code by default.
In 64-bit Linux, GCC is compiled into 64-bit code by default.
The mingw in the window system is always compiled into 32-bit code. Because mingw only supports 32-bit code.
A MinGW-w64 under a window system (for example, a TDM-GCC is installed, and a MinGW-w64 is selected), which is compiled as 64-bit code by default, including in a 32-bit Windows system.

Iii. makefile code

The makefile code is --

# Flagscc = gcccflags =-walllflags = # argsrelease = 0 bits = # [ARGs] generation mode. 0 indicates the debug mode, and 1 indicates the release mode. make release = 1. ifeq ($ (release), 0) # debug cflags + =-gelse # Release cflags + =-static-O3-dndebug lflags + =-staticendif # [ARGs] number of digits of the program. 32 stands for 32-bit programs, 64 stands for 64-bit programs, and Other Default Programs. make bits = 32. ifeq ($ (BITs), 32) cflags + =-M32 lflags + =-m32else ifeq ($ (BITs), 64) cflags + =-M64 lflags + =-M64 else endifendif. phony: All clean # filestargets = gcc64_makeobjs = gcc64_make.oall: $ (targets) gcc64_make: $ (objs) $ (CC) $ (lflags)-o $ @ $ ^ gcc64_make.o: gcc64_make.c $ (CC) $ (cflags)-C $ <clean: Rm-F $ (objs) $ (targets) $ (addsuffix. EXE, $ (targets ))

 

To control Conditional compilation, the variables release and bits are defined and the initial values are assigned respectively. Then, use ifeq to determine the values of the release and bits variables, and add different parameters respectively.
When "make" is directly executed due to initial values, the default debug version is obtained.
If you assign values to variables when executing make, different versions will be obtained --
Make release = 0: (default digit) debug version.
Make release = 1: (default digit) release version.
Make bits = 32: 32-bit (Debug) version.
Make bits = 64: 64-bit (Debug) version.
Make release = 0 bits = 32: 32-bit debug version.
Make release = 0 bits = 64: 64-bit debug version.
Make release = 1 bits = 32: 32-bit release version.
Make release = 1 bits = 64: 64-bit release version.

The Code style of this makefile is well-designed and can be easily expanded --
When you need to add code files or dependencies, modify the content after "# Files.
When you need to adjust the compilation parameters, modify the first half of the parameter variables.
To add a new Conditional compilation parameter, define a variable in "# ARGs" and assign the initial value, and then use "ifeq" to judge the variable to adjust the compilation parameter.

The last "RM-F $ (objs) $ (targets) $ (addsuffix. EXE, $ (targets) "is designed to be compatible with mingw, TDM-GCC and other Windows GCC compiler --
Install msys and configure the PATH environment variable. You can also use the RM command to delete files in windows.
Because the extension name of the executable file under Windows is exe, the extension "addsuffix "".exe .exe" is used.
The. exe executable file will not be generated under linux, but the executable file without the extension will not be generated in windows, causing RM to report an error because the file cannot be found. You can add the-F parameter to ignore this error.

Iv. Test Result 4.1: GCC 4.7.0 in 64-bit fedora 17

Open the terminal, run the CD command to enter the directory where the program is located, and run the following command --

make cleanmake./gcc64_makemake cleanmake RELEASE=1./gcc64_makemake cleanmake BITS=32./gcc64_makemake cleanmake RELEASE=1 BITS=32./gcc64_makegcc --version

 

Running result --

GCC 4.6.2 (mingw (4.2) in 20120426 Windows XP SP3 32-bit Edition ))

Open a command prompt, run the CD command to enter the directory where the program is located, and run the following command --

make cleanmakegcc64_makemake cleanmake RELEASE=1gcc64_makemake cleanmake BITS=64gcc --version

 

Running result --

4.3 Windows 7 SP1 64-bit GCC 4.6.1 (TDM-GCC ))

Open a command prompt, run the CD command to enter the directory where the program is located, and run the following command --

make cleanmakegcc64_makemake cleanmake RELEASE=1gcc64_makemake cleanmake BITS=32gcc64_makemake cleanmake RELEASE=1 BITS=32gcc64_makegcc --version

 

Running result --

 

 

References --
Write makefile with me. Chen Hao. http://blog.csdn.net/haoel/article/details/2886
Makefile Conditional compilation for debug and release. Kung Fu Panda. http://www.cnblogs.com/caosiyang/archive/2012/06/13/2548051.html
Summary of assert () function usage. glroy. http://www.cnblogs.com/ggzss/archive/2011/08/18/2145017.html
TDM-GCC 4.5.2 of GCC for Windows. http://www.cnblogs.com/wxxweb/archive/2011/05/30/2063434.html

Download source code --
Http://files.cnblogs.com/zyl910/gcc64_make.rar

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.