Show: Delphi C++
Display Preferences

Compiling and Linking an OS X C++ Console Application from Command Line

From RAD Studio
Jump to: navigation, search

Go Up to OS X Application Development


This topic describes how to compile and link a C++ console application for 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\18.0\bin (the default installation path).

Instructions

  1. Create a directory ("working directory"). The following operations must be performed in this directory, unless other directory is specified.

  2. 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.

  3. Create a compiler settings file, BCCOSX.CFG, with the following content:

    --sysroot="C:\SDKs\SDK1"
    -I"C:\Program Files (x86)\Embarcadero\Studio\18.0\include\osx";"C:\Program Files (x86)\Embarcadero\Studio\18.0\include\osx\crtl";"C:\Program Files (x86)\Embarcadero\Studio\18.0\include\osx\rtl";
    -L"C:\Program Files (x86)\Embarcadero\Studio\18.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 OS X headers, libraries, and frameworks. To create this directory with the corresponding subdirectories and files, RAD Studio must be used:

    1. Create a local directory C:\SDKs
    2. Override the environment variable BDSPLATFORMSDKSDIR with C:\SDKs
    3. Create an OS X connection profile.
    4. From Tools > Options > Environment Options > SDK Manager:
      1. Add an OS X SDK to your development system using your new connection profile.
      2. 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.

  4. Create a batch file for compilation, compile.bat, with the following commands:

    "C:\Program Files (x86)\Embarcadero\Studio\18.0\bin\bccosx" -c main.cpp


  5. Create a batch file for linking, link.bat, with the following content:

    "C:\Program Files (x86)\Embarcadero\Studio\18.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\18.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 OS X frameworks are specified to XLINK with the -Ff option.

  6. 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.

  7. Double-click link.bat to link main.o with other object files. The linking produces the main executable file.

  8. Run main on Mac in a directory that contains the dynamic libraries specified to XLINK: libcgcrtl.dylib, libcgstl.dylib, and libcgunwind.1.0.dylib.

See Also

Personal tools
In other languages
Previous Versions
Assistance