Show: Delphi C++
Display Preferences

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

From RAD Studio XE2
Jump to: navigation, search

Go Up to Cross-Platform Applications

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\Embarcadero\RAD Studio\9.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:\Remote Profiles\Profile1"
    -I"C:\Program Files\Embarcadero\RAD Studio\9.0\include\osx";"C:\Program Files\Embarcadero\RAD Studio\9.0\include\osx\crtl";"C:\Program Files\Embarcadero\RAD Studio\9.0\include\osx\rtl";
    -L"C:\Program Files\Embarcadero\RAD Studio\9.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:

    1. Create a local directory C:\Remote Profiles
    2. Override the environment variable BDSPROFILESDIR with C:\Remote Profiles
    3. Create the OS X remote profile Profile1
    4. Go to Tools > Options > Environment Options > Remote Profiles and click Update Local File Cache (local root directory must be $(BDSPROFILESDIR)\Profile1)

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

    "C:\Program Files\Embarcadero\RAD Studio\9.0\bin\bccosx" -c main.cpp


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

    "C:\Program Files\Embarcadero\RAD Studio\9.0\bin\xlink" -Fr"C:\Remote Profiles\Profile1" -Fp"/usr/lib" -Fp"/usr/lib/system" -Fp"/System/Library/Frameworks" -FfAppKit -FfApplicationServices -FfCarbon -FfCoreFoundation -FfCoreServices -FfDiskArbitration -FfFoundation -FfIOKit -FfSecurity -j"C:\Program Files\Embarcadero\RAD Studio\9.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:\Remote Profiles\Profile1) is specified to XLINK with the -Fr option.

    The Mac 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
Previous Versions
In other languages