MATLAB Examples

Relocate Code to Another Development Environment with packNGo

Use the code generator packNGo option to generate code in one location. After code generation, collect the compiler command-line options, move the generated code to another location, and compile the generated code in combination with custom code.

Contents

Check Platform and Set Up Path and Folders

For this example, you must have the gcc toolchain running on a Linux® platform. After verifying this platform, this example sets up the path, folders, and files.

When you rerun the example, to ease clean up of generated files, the work folders (code_generation_area_dir and build_area_dir) are separate from the source files (models_dir and hand_code_dir).

All locations of source file and folders use paths that are relative to the folder containing the example .M file.

if isunix
originalFolder = pwd;
thisFilesFullName = mfilename( 'fullpath' );
thisFilesAbsolutePath = fileparts( thisFilesFullName );
modelsFolder = fullfile( thisFilesAbsolutePath, 'models_dir' );
codeGenerationRootFolder = ...
	fullfile( thisFilesAbsolutePath, 'code_generation_area_dir' );
buildAreaRootFolder = ...
	fullfile( thisFilesAbsolutePath, 'build_area_dir' );
handCodeFolder = ...
	fullfile( thisFilesAbsolutePath, 'hand_code_dir' );

Clean Up

To eliminate build artifacts from previously running the example, this example searches for and eliminates folders containing these by-products of previous runs.

if( 0 < exist( codeGenerationRootFolder, 'dir' ) )
	clear mex;
	rmdir( codeGenerationRootFolder, 's' );
else
% No previous codeGenerationRootFolder.
end
mkdir( codeGenerationRootFolder );

if( 0 < exist( buildAreaRootFolder, 'dir' ) )
	clear mex;
	rmdir( buildAreaRootFolder, 's' );
else
% No previous buildAreaRootFolder.
end
mkdir( buildAreaRootFolder );
if( 0 < exist( modelsFolder, 'dir' ) )
	clear mex;
	rmdir( modelsFolder, 's' );
else
% No previous modelsFolder.
end
mkdir( modelsFolder );
copyfile((fullfile(matlabroot,'toolbox','rtw','rtwdemos','rtwdemo_packngo','rtwdemo_packngo.slx')), ...
    (fullfile(modelsFolder,'rtwdemo_packngo.slx')),'f');
if( 0 < exist( handCodeFolder, 'dir' ) )
	clear mex;
	rmdir( handCodeFolder, 's' );
else
% No previous handCodeFolder.
end
mkdir( handCodeFolder );
copyfile((fullfile(matlabroot,'toolbox','rtw','rtwdemos','rtwdemo_packngo','main.c')), ...
    (fullfile(handCodeFolder,'main.c')),'f');
copyfile((fullfile(matlabroot,'toolbox','rtw','rtwdemos','rtwdemo_packngo','main.h')), ...
    (fullfile(handCodeFolder,'main.h')),'f');
directoriesToAddToMatlabPathCellStr = ...
	{ ...
	thisFilesAbsolutePath, ...
	modelsFolder ...
	};
for directoryIndex = 1 : numel( directoriesToAddToMatlabPathCellStr )
    currentDirectory = directoriesToAddToMatlabPathCellStr{ directoryIndex };
    addpath( currentDirectory );
end

Generate Code and Output a .zip File with packNGo

In this example, the code generation configuration for the rtwdemo_packngo model enables the Generate code only parameter (GenCodeOnly) and enables the Package code and artifacts parameter (PackageGeneratedCodeAndArtifacts). These options direct the code generator to create a packNGo archive (.zip file) and stop the build process without compiling the generated code. Compilation occurs later with integrated custom code that replaces the generated main.c file.

cd( codeGenerationRootFolder );
modelBaseName = 'rtwdemo_packngo';
open_system( modelBaseName );
slbuild( modelBaseName );
close_system( modelBaseName );
### Starting build procedure for model: rtwdemo_packngo
### Successful completion of code generation for model: rtwdemo_packngo

Extract Build Information from Makefile

When using the toolchain approach for the build process, you can extract build information from the generated makefile. The information includes source files, dependency files, and macro definitions and values that appear in the makefile. With current working folder set to the folder containing the generated makefile, rtwdemo_packngo.mk, in the Command Window, type:

cd( 'rtwdemo_packngo_grt_rtw' );
system('gmake -f rtwdemo_packngo.mk info')
gmake[5]: Entering directory '/private/tmp/Bdoc17b_685977_71560/publish_examples4/tpaf4f9237/ex21882019/code_generation_area_dir/rtwdemo_packngo_grt_rtw'
### PRODUCT = ../rtwdemo_packngo
### PRODUCT_TYPE = executable
### BUILD_TYPE = Top-Level Standalone Executable
### INCLUDES = -I/private/tmp/Bdoc17b_685977_71560/publish_examples4/tpaf4f9237/ex21882019/code_generation_area_dir -I/mathworks/devel/bat/Bdoc17b/build/matlab/simulink/include/sf_runtime -I/private/tmp/Bdoc17b_685977_71560/publish_examples4/tpaf4f9237/ex21882019/code_generation_area_dir/rtwdemo_packngo_grt_rtw -I/private/tmp/Bdoc17b_685977_71560/publish_examples4/tpaf4f9237/ex21882019/models_dir -I/mathworks/devel/bat/Bdoc17b/build/matlab/extern/include -I/mathworks/devel/bat/Bdoc17b/build/matlab/simulink/include -I/mathworks/devel/bat/Bdoc17b/build/matlab/rtw/c/src -I/mathworks/devel/bat/Bdoc17b/build/matlab/rtw/c/src/ext_mode/common
### DEFINES = -DMAT_FILE=0 -DINTEGER_CODE=0 -DMT=0 -DCLASSIC_INTERFACE=0 -DALLOCATIONFCN=0 -DONESTEPFCN=1 -DTERMFCN=1 -DMULTI_INSTANCE_CODE=0 -DTID01EQ=0 -DMODEL=rtwdemo_packngo -DNUMST=1 -DNCSTATES=0 -DHAVESTDIO -DRT -DUSE_RTMODEL -DUNIX
### ALL_SRCS = /private/tmp/Bdoc17b_685977_71560/publish_examples4/tpaf4f9237/ex21882019/code_generation_area_dir/rtwdemo_packngo_grt_rtw/rtwdemo_packngo.c /mathworks/devel/bat/Bdoc17b/build/matlab/rtw/c/src/common/rt_main.c
### ALL_OBJS = rtwdemo_packngo.o rt_main.o
### LIBS = 
### MODELREF_LIBS = 
### SYSTEM_LIBS = -lm
### TOOLCHAIN_LIBS = 
### CFLAGS = -c -isysroot /Applications/Xcode8.1.0.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk -arch x86_64 -fno-common -fexceptions -O0 -DMAT_FILE=0 -DINTEGER_CODE=0 -DMT=0 -DCLASSIC_INTERFACE=0 -DALLOCATIONFCN=0 -DONESTEPFCN=1 -DTERMFCN=1 -DMULTI_INSTANCE_CODE=0 -DTID01EQ=0 -DMODEL=rtwdemo_packngo -DNUMST=1 -DNCSTATES=0 -DHAVESTDIO -DRT -DUSE_RTMODEL -DUNIX -I/private/tmp/Bdoc17b_685977_71560/publish_examples4/tpaf4f9237/ex21882019/code_generation_area_dir -I/mathworks/devel/bat/Bdoc17b/build/matlab/simulink/include/sf_runtime -I/private/tmp/Bdoc17b_685977_71560/publish_examples4/tpaf4f9237/ex21882019/code_generation_area_dir/rtwdemo_packngo_grt_rtw -I/private/tmp/Bdoc17b_685977_71560/publish_examples4/tpaf4f9237/ex21882019/models_dir -I/mathworks/devel/bat/Bdoc17b/build/matlab/extern/include -I/mathworks/devel/bat/Bdoc17b/build/matlab/simulink/include -I/mathworks/devel/bat/Bdoc17b/build/matlab/rtw/c/src -I/mathworks/devel/bat/Bdoc17b/build/matlab/rtw/c/src/ext_mode/common
### LDFLAGS = -arch x86_64 -isysroot /Applications/Xcode8.1.0.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk -Wl,-rpath,/mathworks/devel/bat/Bdoc17b/build/matlab/bin/maci64 -Wl,-rpath,@executable_path -Wl,-rpath,@executable_path/.. -L/mathworks/devel/bat/Bdoc17b/build/matlab/bin/maci64
### SHAREDLIB_LDFLAGS = -dynamiclib -install_name @rpath/rtwdemo_packngo -isysroot /Applications/Xcode8.1.0.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk -L/mathworks/devel/bat/Bdoc17b/build/matlab/bin/maci64 -Wl, 
### CPPFLAGS = -c -isysroot /Applications/Xcode8.1.0.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk -arch x86_64 -fno-common -fexceptions -O0 -DMAT_FILE=0 -DINTEGER_CODE=0 -DMT=0 -DCLASSIC_INTERFACE=0 -DALLOCATIONFCN=0 -DONESTEPFCN=1 -DTERMFCN=1 -DMULTI_INSTANCE_CODE=0 -DTID01EQ=0 -DMODEL=rtwdemo_packngo -DNUMST=1 -DNCSTATES=0 -DHAVESTDIO -DRT -DUSE_RTMODEL -DUNIX -I/private/tmp/Bdoc17b_685977_71560/publish_examples4/tpaf4f9237/ex21882019/code_generation_area_dir -I/mathworks/devel/bat/Bdoc17b/build/matlab/simulink/include/sf_runtime -I/private/tmp/Bdoc17b_685977_71560/publish_examples4/tpaf4f9237/ex21882019/code_generation_area_dir/rtwdemo_packngo_grt_rtw -I/private/tmp/Bdoc17b_685977_71560/publish_examples4/tpaf4f9237/ex21882019/models_dir -I/mathworks/devel/bat/Bdoc17b/build/matlab/extern/include -I/mathworks/devel/bat/Bdoc17b/build/matlab/simulink/include -I/mathworks/devel/bat/Bdoc17b/build/matlab/rtw/c/src -I/mathworks/devel/bat/Bdoc17b/build/matlab/rtw/c/src/ext_mode/common
### CPP_LDFLAGS = -arch x86_64 -isysroot /Applications/Xcode8.1.0.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk -Wl,-rpath,/mathworks/devel/bat/Bdoc17b/build/matlab/bin/maci64 -Wl,-rpath,@executable_path -Wl,-rpath,@executable_path/.. -L/mathworks/devel/bat/Bdoc17b/build/matlab/bin/maci64
### CPP_SHAREDLIB_LDFLAGS = -dynamiclib -install_name @rpath/rtwdemo_packngo -isysroot /Applications/Xcode8.1.0.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk -L/mathworks/devel/bat/Bdoc17b/build/matlab/bin/maci64 -Wl, 
### ARFLAGS = ruvs
### MEX_CFLAGS = -MATLAB_ARCH=maci64 -I/private/tmp/Bdoc17b_685977_71560/publish_examples4/tpaf4f9237/ex21882019/code_generation_area_dir -I/mathworks/devel/bat/Bdoc17b/build/matlab/simulink/include/sf_runtime -I/private/tmp/Bdoc17b_685977_71560/publish_examples4/tpaf4f9237/ex21882019/code_generation_area_dir/rtwdemo_packngo_grt_rtw -I/private/tmp/Bdoc17b_685977_71560/publish_examples4/tpaf4f9237/ex21882019/models_dir -I/mathworks/devel/bat/Bdoc17b/build/matlab/extern/include -I/mathworks/devel/bat/Bdoc17b/build/matlab/simulink/include -I/mathworks/devel/bat/Bdoc17b/build/matlab/rtw/c/src -I/mathworks/devel/bat/Bdoc17b/build/matlab/rtw/c/src/ext_mode/common COPTIMFLAGS=-fno-common -fexceptions -O0 -DMAT_FILE=0 -DINTEGER_CODE=0 -DMT=0 -DCLASSIC_INTERFACE=0 -DALLOCATIONFCN=0 -DONESTEPFCN=1 -DTERMFCN=1 -DMULTI_INSTANCE_CODE=0 -DTID01EQ=0 -DMODEL=rtwdemo_packngo -DNUMST=1 -DNCSTATES=0 -DHAVESTDIO -DRT -DUSE_RTMODEL -DUNIX -silent
### MEX_CPPFLAGS = 
### MEX_LDFLAGS = LDFLAGS==''
### MEX_CPPLDFLAGS = 
### DOWNLOAD_FLAGS = 
### EXECUTE_FLAGS = 
### MAKE_FLAGS = -f rtwdemo_packngo.mk
gmake[5]: Leaving directory '/private/tmp/Bdoc17b_685977_71560/publish_examples4/tpaf4f9237/ex21882019/code_generation_area_dir/rtwdemo_packngo_grt_rtw'

ans =

     0

For information about performing a similar operation on Windows® platforms, see the model.bat description in docid:rtw_ug.brufp8r-17.

Move Generated Code and Custom Code to Common Location for Build Process

To integrate the generated code and custom code, this example moves the files to a common location for the build process. The archive from the packNGo option is unzipped.

cd( buildAreaRootFolder );
generatedPackNGoArchiveShortName = ...
	[ modelBaseName, '.zip' ];
filesToCopyCellStr = ...
	{ ...
	fullfile( codeGenerationRootFolder, generatedPackNGoArchiveShortName ), ...
	fullfile( handCodeFolder, 'main.h' ), ...
	fullfile( handCodeFolder, 'main.c' ) ...
	};
fileCopyFunctionHandle = ...
	@( sourceFileFullName )copyfile( sourceFileFullName, buildAreaRootFolder );
for sourceFileIndex = 1 : numel( filesToCopyCellStr )
    currentSourceFile = filesToCopyCellStr{ sourceFileIndex };
    fileCopyFunctionHandle( currentSourceFile );
end
generatedPackNGoArchiveDestinationFullName = ...
	fullfile( buildAreaRootFolder, generatedPackNGoArchiveShortName );
unzip( generatedPackNGoArchiveDestinationFullName );

Construct the Build Command

To compile the generated code and integrated custom code, this example constructs a build command for the gcc compiler and tools that:

  • Compiles the generated and custom source files
  • Specifies the required folders where the preprocessor can locate included header files
  • Provides the correct -D arguments for preprocessor macros that the generated code requires

Identify the Source Files for the Build Command

The generated source files are in a folder extracted from the packNGo archive (.zip file). The file name combines the:

  • Model name
  • System target file (selected in the model configuration set)
  • Original code generation folder
generatedCodeSubFolderName = ...
	[ modelBaseName, '_grt_rtw' ];
generatedCodeFolderFullName = ...
	fullfile( buildAreaRootFolder, 'code_generation_area_dir', generatedCodeSubFolderName );
sourceFileFullNamesCellStr = ...
	{ ...
	fullfile( buildAreaRootFolder, 'main.c' ), ...
	fullfile( generatedCodeFolderFullName, 'rtwdemo_packngo.c' ) ...
	};

Identify the Header File Folders

The required header file folders include:

  • Custom header file locations
  • Generated header file locations
  • Locations that contain files that the packNGo operation copied from the MATLAB installation folders to the packNGo archive
[ ~, matlabReleaseFolderName ] = fileparts( matlabroot() );
headerFileDirectoriesCellStr = ...
	{ ...
	buildAreaRootFolder, ...
	generatedCodeFolderFullName, ...
	fullfile( buildAreaRootFolder, matlabReleaseFolderName, 'extern', 'include' ), ...
	fullfile( buildAreaRootFolder, matlabReleaseFolderName, 'simulink', 'include' ) ...
	};

Identify Required Preprocessor Macros

There are a number of preprocessor macros that must be defined to guide conditional compilation in the code. The macro names and values can be copied from the defines.txt file in the generated code folder.

commandLineDefinesCellStr = ...
	{ ...
	'MODEL=rtwdemo_packngo', ...
	'NUMST=1', ...
	'NCSTATES=0', ...
	'HAVESTDIO', ...
	'RT', ...
	'USE_RTMODEL', ...
	'UNIX', ...
	'MAT_FILE=0', ...
	'INTEGER_CODE=0', ...
	'MT=0', ...
	'CLASSIC_INTERFACE=0', ...
	'ALLOCATIONFCN=0', ...
	'ONESTEPFCN=1', ...
	'TERMFCN=1', ...
	'MULTI_INSTANCE_CODE=0', ...
	'TID01EQ=0' ...
	};

Prepare Cell Array Data as Strings

After gathering the source, header, folder, and macro information for the build command into cell array data, this example constructs the build command by reorganizing this data as properly-formed strings. The strings serve as part of a compilation command.

commandLineDefinesCompilerArgumentString = ...
	strjoin( strcat( '-D', commandLineDefinesCellStr ), ' ' );
sourceFilesCompilerArgumentString = ...
	strjoin( sourceFileFullNamesCellStr, ' ' );
inclusionDirectoriesCompilerArgumentString = ...
	strjoin( strcat( '-I', headerFileDirectoriesCellStr ), ' ' );
compilationCommandString = ...
	[ ...
	'gcc', ' ', ...
	'-v', ' ', ...
	sourceFilesCompilerArgumentString, ' ', ...
	commandLineDefinesCompilerArgumentString, ' ', ...
	inclusionDirectoriesCompilerArgumentString ...
	];

Run the Compilation Command

The compilation command is:

[ compilationResult, compilationOutput ] = ...
	system( compilationCommandString );

View the Compilation Results

The results use operating system conventions for the return values because "system(...)" returns the results from the operating system. As a result, "0" indicates success, instead of "1"/"true.

assert( ( 0 == compilationResult ), compilationOutput );
disp( compilationOutput );
Apple LLVM version 8.0.0 (clang-800.0.42.1)
Target: x86_64-apple-darwin16.3.0
Thread model: posix
InstalledDir: /Applications/Xcode8.1.0.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
 "/Applications/Xcode8.1.0.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang" -cc1 -triple x86_64-apple-macosx10.9.0 -Wdeprecated-objc-isa-usage -Werror=deprecated-objc-isa-usage -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -discard-value-names -main-file-name main.c -mrelocation-model pic -pic-level 2 -mthread-model posix -mdisable-fp-elim -masm-verbose -munwind-tables -target-cpu core2 -target-linker-version 274.1 -v -dwarf-column-info -debugger-tuning=lldb -resource-dir /Applications/Xcode8.1.0.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/8.0.0 -isysroot /Applications/Xcode8.1.0.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk -D MODEL=rtwdemo_packngo -D NUMST=1 -D NCSTATES=0 -D HAVESTDIO -D RT -D USE_RTMODEL -D UNIX -D MAT_FILE=0 -D INTEGER_CODE=0 -D MT=0 -D CLASSIC_INTERFACE=0 -D ALLOCATIONFCN=0 -D ONESTEPFCN=1 -D TERMFCN=1 -D MULTI_INSTANCE_CODE=0 -D TID01EQ=0 -I /private/tmp/Bdoc17b_685977_71560/publish_examples4/tpaf4f9237/ex21882019/build_area_dir -I /private/tmp/Bdoc17b_685977_71560/publish_examples4/tpaf4f9237/ex21882019/build_area_dir/code_generation_area_dir/rtwdemo_packngo_grt_rtw -I /private/tmp/Bdoc17b_685977_71560/publish_examples4/tpaf4f9237/ex21882019/build_area_dir/matlab/extern/include -I /private/tmp/Bdoc17b_685977_71560/publish_examples4/tpaf4f9237/ex21882019/build_area_dir/matlab/simulink/include -fdebug-compilation-dir /private/tmp/Bdoc17b_685977_71560/publish_examples4/tpaf4f9237/ex21882019/build_area_dir -ferror-limit 19 -fmessage-length 0 -stack-protector 1 -fblocks -fobjc-runtime=macosx-10.9.0 -fencode-extended-block-signature -fmax-type-align=16 -fdiagnostics-show-option -o /tmp/Bdoc17b_685977_71560/publish_examples4/main-07d2ce.o -x c /private/tmp/Bdoc17b_685977_71560/publish_examples4/tpaf4f9237/ex21882019/build_area_dir/main.c
clang -cc1 version 8.0.0 (clang-800.0.42.1) default target x86_64-apple-darwin16.3.0
ignoring nonexistent directory "/Applications/Xcode8.1.0.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk/usr/local/include"
ignoring nonexistent directory "/Applications/Xcode8.1.0.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk/Library/Frameworks"
#include "..." search starts here:
#include <...> search starts here:
 /private/tmp/Bdoc17b_685977_71560/publish_examples4/tpaf4f9237/ex21882019/build_area_dir
 /private/tmp/Bdoc17b_685977_71560/publish_examples4/tpaf4f9237/ex21882019/build_area_dir/code_generation_area_dir/rtwdemo_packngo_grt_rtw
 /private/tmp/Bdoc17b_685977_71560/publish_examples4/tpaf4f9237/ex21882019/build_area_dir/matlab/extern/include
 /private/tmp/Bdoc17b_685977_71560/publish_examples4/tpaf4f9237/ex21882019/build_area_dir/matlab/simulink/include
 /Applications/Xcode8.1.0.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/8.0.0/include
 /Applications/Xcode8.1.0.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include
 /Applications/Xcode8.1.0.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk/usr/include
 /Applications/Xcode8.1.0.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk/System/Library/Frameworks (framework directory)
End of search list.
 "/Applications/Xcode8.1.0.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang" -cc1 -triple x86_64-apple-macosx10.9.0 -Wdeprecated-objc-isa-usage -Werror=deprecated-objc-isa-usage -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -discard-value-names -main-file-name rtwdemo_packngo.c -mrelocation-model pic -pic-level 2 -mthread-model posix -mdisable-fp-elim -masm-verbose -munwind-tables -target-cpu core2 -target-linker-version 274.1 -v -dwarf-column-info -debugger-tuning=lldb -resource-dir /Applications/Xcode8.1.0.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/8.0.0 -isysroot /Applications/Xcode8.1.0.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk -D MODEL=rtwdemo_packngo -D NUMST=1 -D NCSTATES=0 -D HAVESTDIO -D RT -D USE_RTMODEL -D UNIX -D MAT_FILE=0 -D INTEGER_CODE=0 -D MT=0 -D CLASSIC_INTERFACE=0 -D ALLOCATIONFCN=0 -D ONESTEPFCN=1 -D TERMFCN=1 -D MULTI_INSTANCE_CODE=0 -D TID01EQ=0 -I /private/tmp/Bdoc17b_685977_71560/publish_examples4/tpaf4f9237/ex21882019/build_area_dir -I /private/tmp/Bdoc17b_685977_71560/publish_examples4/tpaf4f9237/ex21882019/build_area_dir/code_generation_area_dir/rtwdemo_packngo_grt_rtw -I /private/tmp/Bdoc17b_685977_71560/publish_examples4/tpaf4f9237/ex21882019/build_area_dir/matlab/extern/include -I /private/tmp/Bdoc17b_685977_71560/publish_examples4/tpaf4f9237/ex21882019/build_area_dir/matlab/simulink/include -fdebug-compilation-dir /private/tmp/Bdoc17b_685977_71560/publish_examples4/tpaf4f9237/ex21882019/build_area_dir -ferror-limit 19 -fmessage-length 0 -stack-protector 1 -fblocks -fobjc-runtime=macosx-10.9.0 -fencode-extended-block-signature -fmax-type-align=16 -fdiagnostics-show-option -o /tmp/Bdoc17b_685977_71560/publish_examples4/rtwdemo_packngo-2b9eb2.o -x c /private/tmp/Bdoc17b_685977_71560/publish_examples4/tpaf4f9237/ex21882019/build_area_dir/code_generation_area_dir/rtwdemo_packngo_grt_rtw/rtwdemo_packngo.c
clang -cc1 version 8.0.0 (clang-800.0.42.1) default target x86_64-apple-darwin16.3.0
ignoring nonexistent directory "/Applications/Xcode8.1.0.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk/usr/local/include"
ignoring nonexistent directory "/Applications/Xcode8.1.0.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk/Library/Frameworks"
#include "..." search starts here:
#include <...> search starts here:
 /private/tmp/Bdoc17b_685977_71560/publish_examples4/tpaf4f9237/ex21882019/build_area_dir
 /private/tmp/Bdoc17b_685977_71560/publish_examples4/tpaf4f9237/ex21882019/build_area_dir/code_generation_area_dir/rtwdemo_packngo_grt_rtw
 /private/tmp/Bdoc17b_685977_71560/publish_examples4/tpaf4f9237/ex21882019/build_area_dir/matlab/extern/include
 /private/tmp/Bdoc17b_685977_71560/publish_examples4/tpaf4f9237/ex21882019/build_area_dir/matlab/simulink/include
 /Applications/Xcode8.1.0.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/8.0.0/include
 /Applications/Xcode8.1.0.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include
 /Applications/Xcode8.1.0.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk/usr/include
 /Applications/Xcode8.1.0.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk/System/Library/Frameworks (framework directory)
End of search list.
 "/Applications/Xcode8.1.0.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld" -demangle -dynamic -arch x86_64 -macosx_version_min 10.9.0 -syslibroot /Applications/Xcode8.1.0.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk -o a.out /tmp/Bdoc17b_685977_71560/publish_examples4/main-07d2ce.o /tmp/Bdoc17b_685977_71560/publish_examples4/rtwdemo_packngo-2b9eb2.o -lSystem /Applications/Xcode8.1.0.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/8.0.0/lib/darwin/libclang_rt.osx.a

Call the Integrated, Compiled Executable

Running the compile executable produces this output:

[ executionResult, executionOutput ] = system( './a.out' );
assert( ( 0 == executionResult ), executionOutput );
disp( executionOutput );
cd( originalFolder );
Input:	1.000000	Output:	42.000000
Input:	2.000000	Output:	84.000000
Input:	3.000000	Output:	126.000000
Input:	4.000000	Output:	168.000000
Input:	5.000000	Output:	210.000000

Considerations for Relocating Code to Other Platforms or Toolchains

This example shows how to use packNGo to relocate code and integrate custom code with generated code. These techniques ease relocating generated code to other platforms (for example, Linux® to Windows®). The sections of this example that show how to construct the build command provide information that supports code relocation.

The build command on the target platform (for example, Microsoft Visual C/C++ MSVC on Windows®) is as close as possible to the build tool flags and definitions in the build command on the source platform (for example, gcc on Linux®). Some flags and definitions are required (for example, list of source and dependency files). Other flags are optional (for example, compiler optimizations vary among compilers). For more information, see docid:rtw_ug.bqghet6.

elseif ismac
    % Code to run on Mac plaform
	disp('Platform (Macintosh) not supported.')
elseif ispc
    % Code to run on Windows platform
	disp('Platform (Windows) not supported.')
else
    disp('Platform not supported.')
end