Main Content

Compile Code in Another Development Environment

This example shows how you can relocate generated code and compile it in another development environment. Use packNGo to create a ZIP file that contains generated source code, build information, and CMake configuration files. In your target development environment, use the codebuild function to build the final library or executable file.

Configure the Model

Open the model.


Do not automatically compile the code, generate a makefile, or package generated code and artifacts.

set_param('rtwdemo_codebuild', 'GenerateMakefile', 'off');
set_param('rtwdemo_codebuild', 'PackageGeneratedCodeAndArtifacts', 'off');

Generate Code and CMake Configuration Files

Run code generation command.

### Starting serial model reference code generation build
### Successfully updated the model reference code generation target for: rtwdemo_codebuild_ref
### Starting build procedure for: rtwdemo_codebuild
### Successful completion of code generation for: rtwdemo_codebuild

Build Summary

Code generation targets built:

Model                  Action          Rebuild Reason                           
rtwdemo_codebuild_ref  Code generated  rtwdemo_codebuild_ref.c does not exist.  

Top model targets built:

Model              Action          Rebuild Reason                                    
rtwdemo_codebuild  Code generated  Code generation information file does not exist.  

2 of 2 models built (0 models already up to date)
Build duration: 0h 0m 56.832s

Generate the CMakeLists.txt configuration files.

buildFolder = RTW.getBuildDir('rtwdemo_codebuild').BuildDirectory;
codebuild(buildFolder, 'BuildMethod', 'cmake');

Package Generated Code and CMake Configuration Files

Run packNGo, packaging the files hierarchically.

packNGo(buildFolder, 'packType', 'hierarchical', 'nestedZipFiles', false);

Switch to Another Development Environment

This is an optional step. For example, if your current development environment is a Linux computer, you can copy the zip file to a Windows computer and then perform the subsequent steps on that computer.

Unzip Source Code and Build Information Files

Unzip the files.

rtwdemo_codebuild_files = unzip('');

Identify folder containing unzipped code for the top component.

rtwdemo_codebuild_top_component_folder = fileparts(rtwdemo_codebuild_files{1});

Build Executable File Using Default Toolchain

Identify the default toolchain (based on compiler selected by mex -setup).

defaultToolchain = coder.make.getDefaultToolchain;

Build the executable file.

codebuild(rtwdemo_codebuild_top_component_folder, 'BuildMethod', defaultToolchain);

Check that executable file is produced.

dir(fullfile(rtwdemo_codebuild_top_component_folder, '..', 'rtwdemo_codebuild*'))

Build Executable File Using Default Template Makefile

Select a template makefile for the current platform.

if ispc
    % With ert_vcx64.tmf, you must have Microsoft Visual C++ installed. If
    % it is not installed, specify ert_lcc64.tmf to use lcc-win64 instead.
    templateMakefile = 'ert_vcx64.tmf';
    templateMakefile = 'ert_unix.tmf';

Build the executable file.

codebuild(rtwdemo_codebuild_top_component_folder, 'BuildMethod', templateMakefile);

Build Shared Library

On Windows, create a definition file that specifies exported symbols.

if ispc
    exportsFile = fullfile...
        (rtwdemo_codebuild_top_component_folder, 'rtwdemo_codebuild.def');
    fid = fopen(exportsFile, 'w');
    fwrite(fid, ['EXPORTS' newline]);
    fwrite(fid, ['rtwdemo_codebuild_initialize' newline]);
    fwrite(fid, ['rtwdemo_codebuild_step' newline]);
    fwrite(fid, ['rtwdemo_codebuild_terminate' newline]);

Build the shared library.

codebuild(rtwdemo_codebuild_top_component_folder, 'BuildVariant', 'SHARED_LIBRARY');

Build Static Library

codebuild(rtwdemo_codebuild_top_component_folder, 'BuildVariant', 'STATIC_LIBRARY');

Use CMake Configuration Files

With the CMakeLists.txt files, you can use the third-party tool CMake to generate makefiles or workspaces for a compiler environment of your choice.

% Compute the CMake command for the current platform.
if ispc
    cmakeCommand = fullfile(matlabroot, 'bin', computer('arch'), 'cmake', 'bin', 'cmake.exe');
    cmakeCommand = fullfile(matlabroot, 'bin', computer('arch'), 'cmake', 'bin', 'cmake');

Using CMake, build the executable file.

[status1, cmdout1] = system([cmakeCommand ' .']);
[status2, cmdout2] = system([cmakeCommand ' --build .']);

See Also


Related Topics