Compiling and Linking an OS X C++ Console Application from Command Line
Go Up to Mac OS X Application Development
This topic describes how to compile and link a C++ console application for Mac OS X from the command line. Batch files are used for convenience.
Tools
BCCOSX and XLINK are used to compile and link. The location of BCCOSX and XLINK is considered to be C:\Program Files (x86)\Embarcadero\Studio\17.0\bin
(the default installation path).
Instructions
- Create a directory ("working directory"). The following operations must be performed in this directory, unless other directory is specified.
- Create a C++ source file, main.cpp, with the following content:
#include <tchar.h> #include <stdio.h> #include <pthread.h> void *thread_routine(void *arg) { printf("\nThread data pointer: %p", arg); return NULL; } int _tmain(int argc, _TCHAR* argv[]) { pthread_t t; char data[1 << 4]; // 16 bytes int result; result = pthread_create(&t, NULL, thread_routine, data); // create thread // verify result ... // perform concurrent operations result = pthread_join(t, NULL); // wait for thread to complete // verify result ... return 0; }
This code creates and runs a POSIX thread.
- Create a compiler settings file, BCCOSX.CFG, with the following content:
--sysroot="C:\SDKs\SDK1" -I"C:\Program Files (x86)\Embarcadero\Studio\17.0\include\osx";"C:\Program Files (x86)\Embarcadero\Studio\17.0\include\osx\crtl";"C:\Program Files (x86)\Embarcadero\Studio\17.0\include\osx\rtl"; -L"C:\Program Files (x86)\Embarcadero\Studio\17.0\lib\osx32\release" --syslib=/usr/lib --sysinc=/usr/include -F/System/Library/Frameworks
The path assigned to--sysroot
represents the local file cache directory (or the logical root directory). Here the compiler and the linker will search for the Mac OS X headers, libraries, and frameworks. To create this directory with the corresponding subdirectories and files, RAD Studio must be used:- Create a local directory
C:\SDKs
- Override the environment variable
BDSPLATFORMSDKSDIR
withC:\SDKs
- Create an OS X connection profile.
- From Tools > Options > Environment Options > SDK Manager:
- Add an OS X SDK to your development system using your new connection profile.
- With the new SDK selected, change the Local root directory to
C:\SDKs\SDK1
, and click Update Local File Cache to pull the SDK files from the remote machine into the new location.
- Create a local directory
- Create a batch file for compilation, compile.bat, with the following commands:
"C:\Program Files (x86)\Embarcadero\Studio\17.0\bin\bccosx" -c main.cpp
- Create a batch file for linking, link.bat, with the following content:
"C:\Program Files (x86)\Embarcadero\Studio\17.0\bin\xlink" -Fr"C:\SDKs\SDK1" -Fp"/usr/lib" -Fp"/usr/lib/system" -Fp"/System/Library/Frameworks" -FfAppKit -FfApplicationServices -FfCarbon -FfCoreFoundation -FfCoreServices -FfDiskArbitration -FfFoundation -FfIOKit -FfSecurity -j"C:\Program Files (x86)\Embarcadero\Studio\17.0\lib\osx32\release" -Tme start.o main.o, main ,, libcgcrtl.dylib libcgcrtl_nonshared.a libcgstl.dylib libcgunwind.1.0.dylib libSystem.B.dylib libiconv.dylib ,,
The logical root directory (
C:\SDKs\SDK1
) is specified to XLINK with the-Fr
option.The Mac OS X frameworks are specified to XLINK with the
-Ff
option.
- Double-click compile.bat to compile main.cpp. If the
-c
option is removed from the compiler command line, then the linker is invoked after compilation (but the linking options must be specified with the help of the compiler). The compilation produces the main.o object file.
- Double-click link.bat to link main.o with other object files. The linking produces the main executable file.
- Run main on Mac in a directory that contains the dynamic libraries specified to XLINK: libcgcrtl.dylib, libcgstl.dylib, and libcgunwind.1.0.dylib.