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.

CodeBuildModel

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

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

Generate Code and CMake Configuration Files

Run code generation command.

slbuild('CodeBuildModel');
### Starting serial model reference code generation build.
### Model reference code generation target for CodeBuildRefModel is up to date.
### Starting build procedure for: CodeBuildModel
### Generated code for 'CodeBuildModel' is up to date because no structural, parameter or code replacement library changes were found.
### Successful completion of code generation for: CodeBuildModel

Build Summary

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

Generate the CMakeLists.txt configuration files.

buildFolder = RTW.getBuildDir('CodeBuildModel').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.

CodeBuildModel_files = unzip('CodeBuildModel.zip');

Identify folder containing unzipped code for the top component.

CodeBuildModel_top_component_folder = fileparts(CodeBuildModel_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(CodeBuildModel_top_component_folder, 'BuildMethod', defaultToolchain);

Check that executable file is produced.

dir(fullfile(CodeBuildModel_top_component_folder, '..', 'CodeBuildModel*'))
CodeBuildModel.exe      CodeBuildModel_grt_rtw  

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.
    templateMakefile = 'ert_vcx64.tmf';
else
    templateMakefile = 'ert_unix.tmf';
end

Build the executable file.

codebuild(CodeBuildModel_top_component_folder, 'BuildMethod', templateMakefile);

Build Shared Library

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

if ispc
    exportsFile = fullfile...
        (CodeBuildModel_top_component_folder, 'CodeBuildModel.def');
    fid = fopen(exportsFile, 'w');
    fwrite(fid, ['EXPORTS' newline]);
    fwrite(fid, ['CodeBuildModel_initialize' newline]);
    fwrite(fid, ['CodeBuildModel_step' newline]);
    fwrite(fid, ['CodeBuildModel_terminate' newline]);
    fclose(fid);
end

Build the shared library.

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

Build Static Library

codebuild(CodeBuildModel_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');
else
    cmakeCommand = fullfile(matlabroot, 'bin', computer('arch'), 'cmake', 'bin', 'cmake');
end

Using CMake, build the executable file.

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

See Also

|

Related Topics