Compiling a C++ Application from the Command Line
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.
Contents
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:
- In RAD Studio, open the project you want to compile.
- Go to Tools > Options > Environment Options and check the Show command line box.
- Build the project (Project > Build <project>).
- 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).
- 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.)
- 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; }
- 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\19.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
- Create the path \Debug\Win32.
- Run Build.bat.
- 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:
- Create a batch file, Preprocess.bat, with the following content:
SET RADSTUDIO_PATH=C:\Program Files (x86)\Embarcadero\Studio\19.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.
- Run Preprocess.bat.
- See File1.tmp (the generated file).