メインコンテンツ

このページの内容は最新ではありません。最新版の英語を参照するには、ここをクリックします。

coder.storageClass

グローバル変数にストレージ クラスを代入

説明

coder.storageClass(global_name, storage_class) は、グローバル変数 global_name にストレージ クラス storage_class を代入します。

グローバル変数へのストレージ クラスの代入は、グローバル変数を宣言する関数で行います。ストレージ クラスを複数の関数で代入する必要はありません。

coder.storageClass を使用するには Embedded Coder® ライセンスが必要です。C/C++ ライブラリまたは実行可能ファイルの生成に Embedded Coder プロジェクトまたは構成オブジェクトを使用する場合のみ、コード生成ソフトウェアで coder.storageClass の呼び出しが認識されます。

すべて折りたたむ

関数 addglobals_ex で、グローバル変数 myglobalone'ExportedGlobal' ストレージ クラスを代入し、グローバル変数 myglobaltwo'ExportedDefine' ストレージ クラスを代入します。

function y = addglobals_ex(x) %#codegen
% Define the global variables.
global myglobalone;
global myglobaltwo;

% Assign the storage classes.
coder.storageClass('myglobalone','ExportedGlobal');
coder.storageClass('myglobaltwo','ExportedDefine');
y = myglobalone + myglobaltwo + x;
end

ライブラリまたは実行可能ファイル用のコード構成オブジェクトを作成します。

cfg = coder.config('dll','ecoder', true);

コードを生成します。この例では、myglobalone および myglobaltwo の型と初期値を -globals 引数を使用して指定します。代わりに、MATLAB® グローバル ワークスペースでグローバル変数を定義することもできます。入力引数 x の型を指定するには、-args オプションを使用します。

codegen -config cfg -globals {'myglobalone', 1, 'myglobaltwo', 2} -args {1} addglobals_ex -report

初期値の 12 から、myglobalonemyglobaltwo の型は double であると codegen で特定されます。codegen により、エクスポートされる変数 myglobalonemyglobaltwo が定義および宣言されます。myglobalone1.0myglobaltwo2.0 に初期化するコードが生成されます。

myglobaltwomyglobalone の生成コードを表示するには、View report リンクをクリックします。

  • myglobaltwoaddglobals_ex.hExported data define セクションで定義されています。

    /* Exported data define */
    
    /* Definition for custom storage class: ExportedDefine */
    #define myglobaltwo                    2.0
    
  • myglobaloneaddglobals_ex.cVariable Definitions セクションで定義されています。

    /* Variable Definitions */
    /* Definition for custom storage class: ExportedGlobal */
    double myglobalone;
  • myglobaloneaddglobals_ex.hVariable Declarations セクションで extern として宣言されています。

    /* Variable Declarations */
    /* Declaration for custom storage class: ExportedGlobal */
    extern double myglobalone;
  • myglobaloneaddglobals_ex_initialize.c で初期化されています。

    /* Include Files */
    #include "addglobals_ex_initialize.h"
    #include "addglobals_ex.h"
    #include "addglobals_ex_data.h"
    
    /* Function Definitions */
    
    /*
     * Arguments    : void
     * Return Type  : void
     */
    void addglobals_ex_initialize(void)
    {
      myglobalone = 1.0;
      isInitialized_addglobals_ex = true;
    }

関数 addglobal_im で、グローバル変数 myglobal'ImportedExtern' ストレージ クラスを代入します。

function y = addglobal_im(x)

% Define the global variable.

global myglobal;

% Assign the storage classes.

coder.storageClass('myglobal','ImportedExtern');
y = myglobal + x;
end

インポートされる変数 myglobal を定義および初期化するファイル c:\myfiles\myfile.c を作成します。

#include <stdio.h>

/* Variable definitions for imported variables */ 
double myglobal = 1.0;

コード構成オブジェクトを作成します。myfile.c を含めるようにコード生成パラメーターを構成します。出力タイプが 'lib' の場合やソース コードのみを生成する場合は、このファイルを提供せずにコードを生成できます。それ以外の場合は、このファイルを提供する必要があります。

cfg = coder.config('dll','ecoder', true);
cfg.CustomSource = 'myfile.c';
cfg.CustomInclude = 'c:\myfiles';

コードを生成します。この例では、myglobal の型と初期値を -globals 引数を使用して指定します。代わりに、MATLAB グローバル ワークスペースでグローバル変数を定義することもできます。インポートされるグローバル変数については、コード生成ソフトウェアで初期値を使用して型のみを特定します。

codegen -config cfg -globals {'myglobal', 1} -args {1}  addglobal_im -report

初期値 1 から、myglobal の型は double であると codegen で特定されます。codegen により、インポートされるグローバル変数 myglobal が宣言されます。myglobal の定義や myglobal を初期化するコードの生成は行われません。myglobal を定義および初期化するコードは myfile.c から提供されます。

myglobal の生成コードを表示するには、View report リンクをクリックします。

myglobaladdglobal_im_data.hVariable Declarations セクションで extern として宣言されています。

/* Variable Declarations */
/* Declaration for custom storage class: ImportedExtern */
extern double myglobal;

関数 addglobal_imptr で、グローバル変数 myglobal'ImportedExternPointer' ストレージ クラスを代入します。

function y = addglobal_imptr(x)

% Define the global variable.

global myglobal;

% Assign the storage classes.

coder.storageClass('myglobal', 'ImportedExternPointer');
y = myglobal + x;
end

インポートされるグローバル変数 myglobal を定義および初期化するファイル c:\myfiles\myfile.c を作成します。

#include <stdio.h>

/* Variable definitions for imported variables */
double v = 1.0; 
double *myglobal = &v;

コード構成オブジェクトを作成します。myfile.c を含めるようにコード生成パラメーターを構成します。出力タイプが 'lib' の場合やソース コードのみを生成する場合は、このファイルを提供せずにコードを生成できます。それ以外の場合は、このファイルを提供する必要があります。

cfg = coder.config('dll','ecoder', true);
cfg.CustomSource = 'myfile.c';
cfg.CustomInclude = 'c:\myfiles';

コードを生成します。この例では、グローバル変数 myglobal の型と初期値を -globals 引数を使用して指定します。代わりに、MATLAB グローバル ワークスペースでグローバル変数を定義することもできます。インポートされるグローバル変数については、コード生成ソフトウェアで初期値を使用して型のみを特定します。

codegen -config cfg -globals {'myglobal', 1} -args {1} addglobal_imptr -report

初期値 1 から、myglobal の型は double であると codegen で特定されます。codegen により、インポートされるグローバル変数 myglobal が宣言されます。myglobal の定義や myglobal を初期化するコードの生成は行われません。myglobal を定義および初期化するコードは myfile.c から提供されます。

myglobal の生成コードを表示するには、View report リンクをクリックします。

myglobaladdglobal_imptr_data.hVariable Declarations セクションで extern として宣言されています。

/* Variable Declarations */
/* Declaration for custom storage class: ImportedExternPointer */
extern double *myglobal;

入力引数

すべて折りたたむ

global_name はグローバル変数の名前です。文字ベクトルとして指定します。global_name はコンパイル時の定数でなければなりません。

例: 'myglobal'

データ型: char

global_var に代入するストレージ クラス。storage_class は次の値のいずれかになります。

ストレージ クラス説明
'ExportedGlobal'
  • C ファイル entry_point_name.cVariable Definitions セクションで変数を定義します。

  • ヘッダー ファイル entry_point_name.hVariable Declarations セクションで extern として変数を宣言します。

  • 関数 entry_point_name_initialize.h で変数を初期化します。

'ExportedDefine'

ヘッダー ファイル entry_point_name.hExported data define セクションで #define 命令を使用して変数を宣言します。

'ImportedExtern'

ヘッダー ファイル entry_point_name_data.hVariable Declarations セクションで extern として変数を宣言します。外部コードで変数の定義を提供しなければなりません。

'ImportedExternPointer'

ヘッダー ファイル entry_point_name_data.hVariable Declarations セクションで extern ポインターとして変数を宣言します。外部コードで有効なポインター変数を定義しなければなりません。

  • グローバル変数にストレージ クラスを代入しない場合、宣言の場所を除き、変数は 'ExportedGlobal' ストレージ クラスの場合と同じように動作します。ストレージ クラスが 'ExportedGlobal' の場合、ファイル entry_point_name.h でグローバル変数が宣言されます。グローバル変数にストレージ クラスがない場合は、ファイル entry_point_name_data.h で変数が宣言されます。

データ型: char

制限

  • グローバル変数にストレージ クラスを代入した後に、そのグローバル変数に別のストレージ クラスを代入することはできません。

  • 定数のグローバル変数にストレージ クラスを代入することはできません。

拡張機能

すべて展開する

C/C++ コード生成
MATLAB® Coder™ を使用して C および C++ コードを生成します。

バージョン履歴

R2015b で導入