Detailed explanation of makefile command under windows

Reprinted from https://blog.csdn.net/xiexievv/article/details/45775005

1. If you already have a vc6 dsp project, you can directly export the nmake script file (.mak)

    “Project - Export Makefile...”

nmake -f nMakeTest.mak CFG="nMakeTest - Win32 Debug"

nmake -f nMakeTest.mak CFG="nMakeTest - Win32 Debug" all

nmake -f nMakeTest.mak CFG="nMakeTest - Win32 Release" clean

     Note: If the /F option is not specified, a file named makefile in the current directory is used

    【nmake /?】 Get more help! vc6: [D:\program files\Microsoft Visual Studio\VC98\Bin]

                                             vs2008:【D:\program files\Microsoft Visual Studio 9.0\VC\bin】

     In order to use the command line tools and function libraries under vc6 or vs2008 correctly, some environment variables need to be set. The quickest way is to open the command line window as follows (take vs2008 as an example):

     

2. The c++ project of vs does not provide the function of exporting nmake script files. We can only use tools or manually write nmake script files.

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

3. rc.exe [convert .rc resource text into .res binary file]

/l 0x804 // Default language ID (hexadecimal number representation) 0x804: Simplified Chinese 0x409: United States   More...

/fo"nMakeTest.res" // Specify the res name of the rc file output

例:rc.exe /l 0x804 /fo"nMakeTest.res" /d "_DEBUG" /d "_AFXDLL" “nMakeTest.rc”

4. Common options of cl.exe [compile .c, .cpp, .cxx into obj files]   more...

/nologo // do not print copyright notices

/I "../include" // Add header file search path (if the path has spaces, be sure to enclose it in quotes)

/DWIN32 // precompiled macro definition (win32 program)

/D_CONSOLE // precompiled macro definition (console program)

/D "_DEBUG" // precompiled macro definition (Debug version)

/D_CRT_SECURE_NO_DEPRECATE // Precompile macro definition (close C4996 warning. C4996 warning will be reported when using strcpy, strcat and other unsafe functions)

/D_CRT_NONSTDC_NO_DEPRECATE // Precompile macro definition (close C4996 warning. C4996 warning will be reported when using unsafe functions such as strcpy and strcat)

/Od // Optimization options: bring in Debug information

/O2 // Optimization options: fastest speed

/O1 // Optimization options: minimum size

/W3 // Set warning level 3

/WX // Treat Warining as error

/Fp"nMakeTest.pch" // Specify the precompiled file name

/Yu"stdafx.h" // use precompiled headers during build

/FI "myheader.h" // #include this file on the first line of each source file

/Fd"vcpdb/testpdb" // Input the idb and pdb files compiled by vc (see the /Gm option below) into the vcpdb directory,

                               And rename it to testpdb.idb and testpdb.pdb (the pdb here is the project database file, which is used to store the database information of the project)

/Fo"objFiles" // Output obj files to the objFiles directory

/c // compile but not link

/feMyTest // After compiling, output the MyTest.exe executable file

/EHsc // Turn on "C++ Exceptions" to avoid compiler warnings

/LD // Create a dynamic link library 
/LDd // Create a debug dynamic link library

/ML // use libc.lib to create single-threaded executable 
/MLd // use libcd.lib to create debug single-threaded executable 
/MT // use libcmt.lib to create multi-threaded executable 
/MTd // use libcmtd. lib create debug multithreaded executable 
/MD // use msvcrt.lib/msvcrt.dll to create multithreaded executable
/MDd // use msvcrtd.lib/msvcrtd.dll to create debug multithreaded executable

/Z7 //Generate debug information compatible with C7.0 
/Zd //Generate line number 
/Zi //Generate complete debug information

/Gm // Enable minimal regeneration 

          The compiler stores dependencies between source files and class definitions in .idb files.
          Use the information from the .idb file to determine whether a source file needs to be compiled.
          Instead, the source file must be recompiled as long as it contains the modified .h file.

/link // Pass the options specified after /link to link.exe

// By default, after cl.exe is compiled, it will automatically call link.exe to connect,
// So after compiling .c or .cpp with main function directly with cl.exe, obj and exe files will be generated.

Example: cl /c test1.cpp test2.cpp // Compile test1.cpp, test2.cpp

例:cl *.cpp /MD /c /I"G:\Visual C++\VC98\PlatformSDK\Include"

5. Common options of link.exe [Link obj, lib, res into executable files such as dll or exe]

/dll // output dll file

-lib // Generate lib static library file Example: link -lib *.obj /out:test.lib 

/libpath:"..\PublicSDK\lib" // Specify the external lib search path (no spaces in the path, otherwise an LNK1181 error will be reported when linking)

/subsystem:windows[console] // Specify the subsystem

/machine specifies the target platform {AM33|ARM|EBC|IA64|M32R|MIPS|SH3|SH3DSP|SH4|SH5|THUMB|X86|X64}, etc.

/NODEFAULTLIB:libcd.lib // When linking, ignore libcd.lib library

/debug // Generate debug information

/export:myAdd=_Add,@1 // Export the extern "C" Add function, change the symbol name to myAdd, and set the export sequence number to 1 (a method for dll dynamic library to export symbols)

/export:_g_isTest,@2 // Export the extern "C" g_isTest variable and set the export number to 2 (a method for exporting symbols from a dll dynamic library)

/def:"nMakeTest.def" // Module export file [if the name of the def file is the same as the name of the dll, it does not need to be indicated explicitly] (another way to export symbols from a dll dynamic library)

;nMakeTest.lib exports DLL functions
; Author: kekec
LIBRARY nMakeTest.def
EXPORTS
Add@1
g_isTest@2

Note: You can also use __declspec(dllexport) in the code to export symbols

#ifdef WIN32DLL_EXPORTS
#define WIN32DLL_API __declspec(dllexport)
#else
#define WIN32DLL_API __declspec(dllimport)
#endif

/************** export.c ***************/ 
#ifdef __cplusplus
extern "C"
{
#endif
    WIN32DLL_API int __stdcall Add(int a, int b)
    {
        return (a + b);
    }

    WIN32DLL_API int g_isTest = 0;
#ifdef __cplusplus
}
#endif

/pdb:"nMakeTest.pdb" // Rename the generated pdb file (Program Debug Database), save debugging symbols and other information

/map:"nMakeTest.map" // Rename the generated map file

/out:"nMakeTest.exe" // rename the generated exe file

/implib:"test.lib" // Generate an export library named test.lib

/entry:_DllMainCRTStartup@12 // Specify the starting address of the _DllMainCRTStartup function dll

/incremental:yes // Enable incremental linking

          The incremental switch is enabled by default.
          The size of the exe or dll file generated by enabling incremental linking is larger.
          Because of the padding of code and data, incrementally linked exe or dll files will contain jump trunks to handle function relocations to new addresses. 
          It is clearly stated on MSDN: To ensure that the final release version does not contain padding or trunk, please turn off incremental linking.

Example: link gdiplus.lib /subsystem:windows /out:test.exe file1.obj file2.lib file3.res // Generate a windows executable program named test.exe
Example: link gdiplus.lib /subsystem:console /out :test.exe *.obj file2.lib file3.res // Generate a console executable program named test.exe
: link gdiplus.lib /subsytem:windows /dll /out:test.dll /implib:test. lib /def:test.def *.obj file2.lib file3.res // Generate a dynamic library named test.dll

例:link *.obj rc.res /LIBPATH:"G:\Visual C++\lib" /SUBSYSTEM:WINDOWS /MACHINE:X86 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib OpenGL32.Lib

6. nmake instruction description

(1) Symbol description

     # // Comments (comments cannot be used on the line where the command is located, the command and comments should be written on a separate line; for example: erase nMakeTest.obj # Delete the nMakeTest.obj file [illegal])

     ^#abc // Indicates the string #abc

     \ // The connector is used to combine two lines into one; in a macro, when writing in multiple lines, be sure to use "\" to connect

     % // file specifier, indicating that the following string is a file name

---------------------

If the file name is c:\prog.exe
%s will be c:\prog.exe 
%:F will be c:\prog.exe
%:dF will be c
%:pF will be c:\
%:fF will be prog
%:eF will be exe

---------------------

     @ // Command modifier; prevents the result of the modified command from being printed

      ! // command modifier

  $ // macro reference character

      : // Dependency symbol

  ?[*] // Wildcard support  

++++++++++++++++++++++++++++++++++++

     $@ // represents the value of all target full names (path + file name + extension) one by one

     $$@ // Same meaning as $@, but only valid as a dependency in a dependency

     $< // Represents the individual values ​​of all dependent targets, only valid in the command of the inference rule

     $^ // Represents a set of all dependent targets, separated by spaces, if there are duplicates, they will be deduplicated;

     $+ // Same meaning as $^, just without deduplication.
     $? // Represents the set of all target-centric dependencies, separated by spaces

     $* // path and file name of the current target, no file extension
     $** // all dependencies of the current target

----------------------------

Modifier Description  
D drive and directory  
B file name  
F file name and extension  
R drive, directory and file name

----------------------------

(2) Long file names are enclosed in double quotation marks

  Example: ALL : nMakeTest.dll // When the file name is short, quotation marks are not required
  Example: ALL : "$(OUTDIR)\nMakeTest.exe" // When the file name is long, especially if there are spaces in the path, it must be use quotation marks

(3) Predefined rules

.c.obj // default action: cl /c $*.c

The default action can also be explicitly overridden:

 

.c.obj:
    cl /c /Ox /DWIN32 $<

 

(4) Include files

!INCLUDE nmake.opt

include makefile.mak

(5) Conditional judgment - 01

!IF "$(CFG)" == ""
CFG=nMakeTest - Win32 Debug
!MESSAGE No configuration specified. Defaulting to nMakeTest - Win32 Debug.
!ELSE
!MESSAGE Be specified.
!ENDIF

(6) Condition judgment - 02 [!IFNDEF !IFDEF]

!IFNDEF PRIVATE_RUNTIMEMODE_DEBUG
RUNTIMEMODE_DEBUG = /MDd 
!ELSE
RUNTIMEMODE_DEBUG = $(PRIVATE_RUNTIMEMODE_DEBUG)
!ENDIF

(7) Output message log

!MESSAGE Invalid configuration "$(CFG)" specified.

(8) Description block - the core of makefile [Note: No blank line can appear between dependencies (or rules) and command blocks, a tab character is used before commands, and multiple commands are used; split]

     As long as any file in dependencies is newer than targets, execute the commands command

targets... : dependences...
  commands...

(9) ALL / CLEAN

OUTDIR=.\Release
INTDIR=.\Release
ALL : "$(OUTDIR)\nMakeTest.exe"
CLEAN :
  -@erase "$(INTDIR)\nMakeTest.obj"
  -@erase "$(INTDIR)\nMakeTest.pch"
  -@erase "$(INTDIR)\nMakeTest.res"
  -@erase "$(INTDIR)\nMakeTestDlg.obj"
  -@erase "$(INTDIR)\StdAfx.obj"
  -@erase "$(INTDIR)\vc60.idb"
  -@erase "$(OUTDIR)\nMakeTest.exe"
  -@erase "$(OUTDIR)\nMakeTest.map"
"$(OUTDIR)" :
  if not exist "$(OUTDIR)/$(NULL)" mkdir "$(OUTDIR)"

(10) Compile

CPP=cl.exe
CPP_PROJ=/nologo /MD /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /D "_AFXDLL" /D "_MBCS" /Fp"$(INTDIR)\nMakeTest.pch" /Yu"stdafx.h" /Fo"$(INTDIR)\\" /Fd"$(INTDIR)\\" /FD /c 
.c{$(INTDIR)}.obj::
  $(CPP) @<<
  $(CPP_PROJ) $< 
<<
.cpp{$(INTDIR)}.obj::
  $(CPP) @<<
  $(CPP_PROJ) $< 
<<
.cxx{$(INTDIR)}.obj::
  $(CPP) @<<
  $(CPP_PROJ) $< 
<<

(11) Links

LINK32=link.exe
LINK32_FLAGS=/nologo /subsystem:windows /incremental:no /pdb:"$(OUTDIR)\nMakeTest.pdb" /map:"$(INTDIR)\nMakeTest.map" /machine:I386 /out:"$(OUTDIR)\nMakeTest.exe" 
LINK32_OBJS= \
  "$(INTDIR)\nMakeTest.obj" \
  "$(INTDIR)\nMakeTestDlg.obj" \
  "$(INTDIR)\StdAfx.obj" \
  "$(INTDIR)\nMakeTest.res"
"$(OUTDIR)\nMakeTest.exe" : "$(OUTDIR)" $(DEF_FILE) $(LINK32_OBJS)
   $(LINK32) @<<
  $(LINK32_FLAGS) $(LINK32_OBJS)
<<

(12) File dependencies

SOURCE=.\nMakeTest.cpp
"$(INTDIR)\nMakeTest.obj" : $(SOURCE) "$(INTDIR)" "$(INTDIR)\nMakeTest.pch"
SOURCE=.\nMakeTest.rc
"$(INTDIR)\nMakeTest.res" : $(SOURCE) "$(INTDIR)"
$(RSC) $(RSC_PROJ) $(SOURCE)

(13) Precompiled files

SOURCE=.\StdAfx.cpp
!IF "$(CFG)" == "nMakeTest - Win32 Release"
CPP_SWITCHES=/nologo /MD /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /D "_AFXDLL" /D "_MBCS" /Fp"$(INTDIR)\nMakeTest.pch" /Yc"stdafx.h" /Fo"$(INTDIR)\\" /Fd"$(INTDIR)\\" /FD /c 
"$(INTDIR)\StdAfx.obj" "$(INTDIR)\nMakeTest.pch" : $(SOURCE) "$(INTDIR)"
  $(CPP) @<<
 $(CPP_SWITCHES) $(SOURCE)
<<
!ELSEIF "$(CFG)" == "nMakeTest - Win32 Debug"
CPP_SWITCHES=/nologo /MDd /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_WINDOWS" /D "_AFXDLL" /D "_MBCS" /Fp"$(INTDIR)\nMakeTest.pch" /Yc"stdafx.h" /Fo"$(INTDIR)\\" /Fd"$(INTDIR)\\" /FD /GZ /c 
"$(INTDIR)\StdAfx.obj" "$(INTDIR)\nMakeTest.pch" : $(SOURCE) "$(INTDIR)"
  $(CPP) @<<
 $(CPP_SWITCHES) $(SOURCE)
<<
!ENDIF

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325209844&siteId=291194637