How to successfully compile Windows programs under Console Mode

zhaozj2021-02-17  56

I believe that friends who have "using C / C compilers in Console Mode" have the feeling of jump, want to compile their own programs under console mode, but when they do this, they will encounter a lot of problems, compile A relatively simple Console program is ok, but when you compile the Windows program, you will find that there is a lot of link errors, why do you have this situation, this is related to the Windows program, the Windows program is different from the previous DOS program He needs a lot of support for the library file, let us first look at Houjie's article, know how to set some basic things:> ★★ Note: The following is suitable for PC environments ★★ >>> ● C / C compiler needs Environment Variable Settings >> Early, the C compiler on the PC requires two environmental variables: >> lib: This environment variable tells the compiler that where is the necessary libraries (which disk directory)> Include: Tell the compiler that where is the necessary Header Files (which disk directory) >> In addition, in order to make us a compiler in any Working Directory, of course we must set the path. >> Since the ancient times, the C / C compiler has been needed to be three environment variables. >>> ● Take Visual C as an example >> Take Visual C as an example, if the installation file layout is as follows: >> C: / msdev / vc98 / bin: This is placed here with compiler CL.EXE> C: / MSDEV / VC98 / INCLUDE: C / C Header Files> C: / MSDEV / VC98 / LIB: There is a C / C Standard Libraries >> So you can write a batch file as follows: >> SET PATH = C: / MSDEV / VC98 / BIN; C: / MSDEV / COMMON / MSDEV98 / BIN> SET INCLUDE = C: / MSDEV / VC98 / INCLUDE> SET lib = C: / MSDEV / VC98 / LIB >> Need additional setting PATH = C: / MSDEV / Common / MSDEV98 / BIN, is because the compiler cl.exe is executed when Mspdb60.dll is installed, and it is installed in C: / MSDEV / COMMON / MSDEV98 / BIN. >> If you write the program is not just a simple C / C program, it also uses the MFC, which can be compiled under console mode. This time your environment variable should be so setting: >> set path = C: / MSDEV / VC98 / BIN; C: / MSDEV / Common / MSDEV98 / BIN> SET INCLUDE = C: / MSDEV / VC98 / INCLUDE; C: / MSDEV / VC98 / MFC / INCLUDE> SET LIB = C: / MSDEV / VC98 / LIB; C: / MSDEV / VC98 / MFC / LIB >> MFC / Include and MFC / LIBs can be made to find MFC's header files and libraries for compilers and coupons.

If you need to use ATL, you have to add C: / MSDEV / VC98 / ATL / include in the include environment variable. >>> ● Take Borland C Builder as an example >> Take Borland C Builder as an example, if the installed file layout is as follows: >> C: / borland / cbuilder3 / bin: This is a compiler BCC32.exe > C: / borland / cbuilder3 / include: C / C header files> C: / borland / cbuilder3 / lib: C / C Standard Libraries >> So you can write a batch file as follows: >> SET PATH = C: / BORLAND / CBUILDER3 / BIN> SET INCLUDE = C: / Borland / CBuilder3 / include> set lib = c: / borland / cbuilder3 / lib >>> ● How to compile C / C programs in Console >> First, a DOS Box (DOS PROMPT, DOS VM) is turned on, and then the above-described batch file is executed in the DOS Box, complete the setting of the environment variable. You can type the set command in the DOS prompt number to see if the setting content of the environment variable is correct or not. >> Then you can type the compiler name directly under the DOS prompt number and start compiling. If you use Visual C , do this: >> C: /> CL Test.cpp >> If you use C Builder, do it: >> C: /> BCC32 Test.cpp < Enter >>> What special Options need to be required in special circumstances, you must check it yourself. All compile options can be seen as long as CL / or BCC32 (without any quotes will be added). >>> ● The relationship between the compiler and the coupling >> The early compilation process is separated from the coupling process. In other words, we have to do two actions: >> c: /> cl test.cpp> c: /> link test.obj xxx (xxx represents each necessary libraries >> or: >> C: /> BCC32 Test.cpp> C: /> TLINK32 TEST.OBJ XXX (XXX represents each necessary libraries >>> Today's compilation process is of course separate, but our actions only need one: >> C: /> CL Test.cpp >> or: >> C: /> BCC32 Test.cpp >> This is because the compiler is smart, unless you specify / c option (indicating that only compiles do not connect), otherwise it will automatically The call coupling is coupled.

In the past, the problem of "which Libraries" that made Programmer's troubles has also been a smart solution: it records the library functions used in the program, and also records the Library names they belong, so the coupon You can know which Libraries you want to link from this form. >>> ● Relationship between environmental variables and DOS VM (Virtual Machine) >> You can start multiple DOS Box at the same time, but you can't perform the above-mentioned batch files in another DOS Box and enjoy it in another DOS VM Its environment setting. >> This is because each DOS Box is a Virtual Machine, who can't see each other, nothing to do. >> Unless you set these environment variables described above unless you are in autoexec.bat. In this way, any newly opened DOS VM will inherit the variable settings because of the most primitive DOS VM environment. >>> ● Environment Spaces Insufficient >> The most easily caused, is the problem of lack of environmental space (Environment Space). >> When you install Visual C , you will find a file called vcvars32.bat in its bin subdirectory. This file is actually the above environment variable setting action (this is explained in the last step in the Visual C installation process. Hey, how many people install the software do not explain!). So, you can execute this file in any DOS box, replace the aforementioned our own batch file. >> But usually have a failure experience to get the "Out of Environment Space" error message. This is because vcvars32.bat uses the following syntax: >> set iblude = IR / ATL / include; IR / include; IR / mfc / include;%> set lib = IR / lib; IR / MFC / LIB;% >> Meaning Is the original setting (%) of the include the original settings (%), and add other settings to the original setting (%) of the LIB. If the original setting is very long, more comment several times, "Out of Environment Space"! >> One of the practices is to increase the size of the environment. Please add this line in the C: / Config.sys file: >> shell = c: /command.com C: / / E: 1024 / P >> Among them, / E: 1024 is indicating that the environmental space is adjusted to 1024 BYTES. (Not enough? Advanced) >> The second thing is not to use the "additional" sentence pattern of vcvars32.bat, and use the aforementioned our own batch file. To know, we may have several compiler environments (VC, BCB, G ┅), need to take turns to test our program; if you use "additional" sentence, more times, then big environmental space will also fight . >> Methods One and Methods Two should be dual.

>>> ● Is there any limit on any size? >> Use the console mode (or COMMAND LINE mode) to compile the coupling program, can the size of the program have restrictions on any size? The answer is not! >> Its disadvantages are that there is no tool to help you manage files. There is no preset to make you less than a few words. No analysis tools help you organize Objects, let you browse Objects, Symbols. So once you have the basic skills, you must start the design of large-scale programs, of course, as well as the integration environment (IDE). >>> ● Don't misunderstand >> I am not driving, I have to go back to the era of rioli, I will go back to the top of the mountain. But I think that for a C / C beginner, the integration environment (IDE) is probably bringing a mist, it is better to work in Console Mode first. On the one hand, I know more common sense of environmental settings. On the one hand, it is more convenient to use, and I don't have to write a 1000-line small practice to start the five-five plus arsena. On the one hand, the power to know can be all in the language. The exercise is head. >> If you have a certain degree, use the integrated environment, you will not fog like a fiddle. If you don't know, you can press the above settings. When we compile the Windows program, we need to set it on the parameter. The syntax of the Cl [option ...] file ... [option | file] ... [lib ...] [@ Command-file] [/ link link-opt ...], where Option can generally say that @ command-file is waiting for a while, the key issue is on the LIB, we must compile the Windows program To set this, add User32.lib, gdi32.lib, etc., for example, we wrote a Windows Application called Hello.cpp, we can compile: CL hello.cpp user32.lib GDI32 .lib is simple. Usually we may not know which .lib files you want to use, depending on different programs, then a good method is to put all possible .lib file names behind, it is of course very good, but also give us Bring a lot of trouble ---- We have to type so many letters each time, very cumbersome. Fortunately, there is also a compilation option @ command-file, it can reduce our input, we can put all the .lib file names in this file, stored in the current directory, such as named lib (we don't want The suffix name), so it can be entered like this: CL hello.cpp @lib is very simple, let's try it, compile us under console mode, let us more familiarize with the set of Compiler options. Note: Common. LIB files are as follows Kernel32.lib user32.lib gdi32.lib winspool.lib comdi32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib ODBC32.LIB ODBCCP32.LIB

转载请注明原文地址:https://www.9cbs.com/read-29301.html

New Post(0)