In the past two years, intermittent learning and use of C, usually in the Codeblocks write code, compile the program, click the button on the line. The entire compilation process is not understood at all. Compared to the year of learning Java, really chose two different roads, when learning Java, all in the DOS under the study, Javac, JAVA,JAVAW and other commands used by those quite ripe, a few years later began using Eclipse to write code.
Today in looking for how to use codeblocks compile generated EXE file to add version information, found an article, introduced in DOS under the use of GCC, I follow the instructions above to operate a bit, the process did not encounter any problems.
Just feel the GCC command to use the parameters that is quite a lot ah.
1.Console Command line version:
File name: MAIN.C
Code:
Program code:
#include <stdio.h>
int main ()
{
printf ("Hello world\n");
return 0;
}
Below the command line, enter:
Gcc-wall-c-g-o main.o main.c for file compilation.
Parameter explanation:
-wall to open all compilation warnings
-c only compile, not link.
-G generates debug information.
-o main.o generate compiled output file called MAIN.O
Finally, the file link:
Ld-lc:\mingw\lib-o Main.exe MAIN.O C:\MINGW\LIB\CRT2.O-LMINGW32-LKERNEL32-LMSVCRT
Parameter explanation:
-lc:\mingw\lib set the link when the library file path, if not set,-lmingw32 these parameters will not be found.
-O Main.exe sets the output of the final executable file Main.exe.
C:\MINGW\LIB\CRT2.O This is the console program initialization module for Windows systems.
-lmingw32-lkernel32-lmsvcrt These three are libmingw32.a libkernel32.a LIBMSVCRT.A related libraries that indicate a link-time link. The latter two correspond to Kernel32.dll and Msvcrt.dll, and the Basic program code printf function actually calls the puts function in the Msvcrt.dll dynamic library.
When compiling formally, you can use the following command line to generate the smallest EXE:
Gcc-s-o2 Main.c-mconsole
Parameter explanation:
-S compilation generates assembly code
-o2 for Target code optimization, O1 to O3 range
-MCONSOLE Specifies the build console program.
Although the sample compiled here does not have as many steps as above, you still need to understand what a program is inverted and which libraries/modules are linked, as shown in the example. Then we use Depends.exe to view the program with the runtime dependencies, as shown in:
Version 2.Windows:
File name: MAIN.C
Code:
Program code:
#include <windows.h>
typedef HINSTANCE _H;
int WINAPI WinMain (_h hinst, _h hprev, LPSTR cmd, int nshow)
{
Return MessageBox (NULL, "Hello world!", "OK", MB_OK);
}
Command Line Input:
Gcc-c MAIN.C
Ld-l "C:\Program files\codeblocks\mingw\lib" MAIN.O "C:\Program files\codeblocks\mingw\lib\crt2.o"-lmingw32- Lkernel32-luser32-lmsvcrt
The example here is if it is a path with a space.
When compiling formally, you can use the following command line to generate the smallest EXE: Note that-mwindows represents the build Windows application
Gcc-s-o2 Main.c-mwindows
Use Depends.exe to view the program and runtime dependencies. View window messages and messages with Spy + +.
2012-09-15
Learn to use GCC to compile under DOS