Compiling a C++ Application from the Command Line

From RAD Studio
Jump to: navigation, search

Go Up to How To Compile and Build Applications

This topic describes how to build or preprocess a simple 32-bit Windows C++ application using a batch file that contains the command lines used by the IDE.

Finding the Command Lines

You can take a file that you normally build inside the IDE, and run it on the command line. First, you need to find the command lines in the IDE, as follows:

  1. In RAD Studio, open the project you want to compile.
  2. Go to Tools > Options > Environment Options and check the Show command line box.
  3. Build the project (Project > Build <project>).
  4. Open the Messages View (View > Messages) and click the + sign in front of either "Bcc32 command line for <project>" (for compiling, building, and linking) or "Cpp32 command line for <project>" (for preprocessing).
  5. Copy the command lines shown in the Messages View.

Building the Project (Compiling and Linking)

The following example uses a batch file to deliver the command lines to the C++ compiler, BCC32. (You can also paste the command lines into the CMD window, instead of using a batch file.)

  1. Create a C++ source file, File1.cpp, with the following content:
    #include <vcl.h>
    #include <stdio.h>
    #pragma hdrstop
    
    #include <tchar.h>
    //---------------------------------------------------------------------------
    
    #pragma argsused
    
    int _tmain(int argc, _TCHAR* argv[]) {
    	puts("Hello world!");
    	return 0;
    }
    
  2. Create a batch file, Build.bat, containing the command line copied from the Messages View. The paths in the .bat file might vary depending on where your IDE is installed and the host Windows platform. For example:
    SET RADSTUDIO_PATH=C:\Program Files (x86)\Embarcadero\Studio\21.0
    
    REM COMPILE
    
    SET INCLUDE_FILE_SEARCH_PATH="%RADSTUDIO_PATH%\include";"%RADSTUDIO_PATH%\include\windows\crtl";"%RADSTUDIO_PATH%\include\windows\sdk";"%RADSTUDIO_PATH%\include\windows\rtl";"%RADSTUDIO_PATH%\include\windows\vcl";"%RADSTUDIO_PATH%\include\windows\vcl"
    
    "%RADSTUDIO_PATH%\bin\bcc32.exe" -D_DEBUG -D;USEPACKAGES -n.\Debug\Win32 -I=%INCLUDE_FILE_SEARCH_PATH% -y -Q -k -r- -c -tC -tM -tU -C8 -o.\Debug\Win32\File1.obj -w-par -Od -v -vi- -H=.\Debug\Win32\Project1.pch -H File1.cpp 
    
    REM LINK
    
    SET LIBRARY_SEARCH_PATH=.\Debug\Win32;"%RADSTUDIO_PATH%\lib\Win32\debug";"%RADSTUDIO_PATH%lib\win32\debug\psdk";"%RADSTUDIO_PATH%\lib\Win32\debug"
    
    SET OBJECT_SEARCH_PATH=.\Debug\Win32;"%RADSTUDIO_PATH%\lib\Win32\debug";"%RADSTUDIO_PATH%\lib\win32\debug\psdk";"%RADSTUDIO_PATH%\lib\Win32\debug"
    
    "%RADSTUDIO_PATH%\bin\ilink32.exe" -L%LIBRARY_SEARCH_PATH% -j%OBJECT_SEARCH_PATH% -l.\Debug\Win32 -v -V5.0 -G8 -Tpe  c0x32w vcl.bpi rtl.bpi memmgr.lib sysinit.obj .\Debug\Win32\File1.obj , .\Debug\Win32\Project1.exe , .\Debug\Win32\Project1.map , import32.lib cp32mt.lib , ,
    
    PAUSE
    
  3. Create the path \Debug\Win32.
  4. Run Build.bat.
  5. Run \Debug\Win32\Project1.exe.

Preprocessing the Project

The following example is similar to the previous example except that this one uses a batch file to deliver the command lines to the preprocessor, CPP32:

  1. Create a batch file, Preprocess.bat, with the following content:
    SET RADSTUDIO_PATH=C:\Program Files (x86)\Embarcadero\Studio\21.0
    
    SET INCLUDE_FILE_SEARCH_PATH="%RADSTUDIO_PATH%\include";"%RADSTUDIO_PATH%\include\windows\crtl";"%RADSTUDIO_PATH%\include\windows\sdk";"%RADSTUDIO_PATH%\include\windows\rtl";"%RADSTUDIO_PATH%\include\windows\vcl";"%RADSTUDIO_PATH%\include\windows\vcl"
    
    "%RADSTUDIO_PATH%\bin\cpp32.exe" -D_DEBUG -D_RTLDLL;USEPACKAGES -I=%INCLUDE_FILE_SEARCH_PATH% -y -Q -k -r- -c -tWC -tWM -tU -C8 -o"File1.tmp" -w-par -Od File1.cpp 
    

    This file must be saved in the directory where File1.cpp is stored.

  2. Run Preprocess.bat.
  3. See File1.tmp (the generated file).

See Also