주요 콘텐츠

이 페이지는 기계 번역을 사용하여 번역되었습니다. 영어 원문을 보려면 여기를 클릭하십시오.

coder.storageClass

R2026b

전역 변수에 스토리지 클래스 할당하기

설명

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);

코드를 생성합니다. 이 예제에서는 -globals 인수를 사용하여 myglobalone와 myglobaltwo의 데이터형과 초기값을 지정합니다. 또는 MATLAB® 전역 작업 공간에서 전역 변수를 정의할 수도 있습니다. 입력 인수 x의 유형을 지정하려면 -args 옵션을 사용하십시오.

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

1와 2의 초기값을 바탕으로, codegen는 myglobalone와 myglobaltwo의 유형이 double임을 결정합니다. codegen는 내보내진 변수인 myglobalone와 myglobaltwo를 정의하고 선언합니다. myglobalone를 1.0로, myglobaltwo를 2.0로 초기화하는 코드를 생성합니다.

myglobaltwo 및 myglobalone에 대해 생성된 코드를 보려면 View report 링크를 클릭하세요.

  • myglobaltwo는 addglobals_ex.h의 Exported data define 섹션에 정의되어 있습니다.

    /* Exported data define */
    
    /* Definition for custom storage class: ExportedDefine */
    #define myglobaltwo                    2.0
    
  • myglobalone는 addglobals_ex.c의 Variable Definitions 섹션에 정의되어 있습니다.

    /* Variable Definitions */
    /* Definition for custom storage class: ExportedGlobal */
    double myglobalone;
  • myglobalone는 addglobals_ex.h의 Variable Declarations 섹션에서 extern로 선언되어 있습니다.

    /* Variable Declarations */
    /* Declaration for custom storage class: ExportedGlobal */
    extern double myglobalone;
  • myglobalone는 addglobals_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';

코드를 생성합니다. 이 예제에서는 -globals 인수를 사용하여 myglobal의 유형과 초기값을 지정합니다. 또는 MATLAB 전역 작업 공간에서 전역 변수를 정의할 수도 있습니다. 가져온 전역 변수의 경우, 코드 생성 소프트웨어는 초기값을 사용하여 해당 변수의 유형만 결정합니다.

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

초기값 1를 바탕으로, codegen는 myglobal의 유형이 double임을 판단합니다. codegen는 가져온 전역 변수 myglobal를 선언합니다. myglobal를 정의하지도 않고, myglobal를 초기화하는 코드를 생성하지도 않습니다. myfile.c는 myglobal를 정의하고 초기화하는 코드를 제공합니다.

myglobal에 대해 생성된 코드를 보려면 View report 링크를 클릭하세요.

myglobal는 addglobal_im_data.h의 Variable 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';

코드를 생성합니다. 이 예제에서는 -globals 인수를 사용하여 전역 변수 myglobal의 데이터형과 초기값을 지정합니다. 또는 MATLAB 전역 작업 공간에서 전역 변수를 정의할 수도 있습니다. 가져온 전역 변수의 경우, 코드 생성 소프트웨어는 초기값을 사용하여 해당 변수의 유형만 결정합니다.

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

초기값 1를 바탕으로, codegen는 myglobal의 유형이 double임을 판단합니다. codegen는 가져온 전역 변수 myglobal를 선언합니다. myglobal를 정의하지도 않고, myglobal를 초기화하는 코드를 생성하지도 않습니다. myfile.c는 myglobal를 정의하고 초기화하는 코드를 제공합니다.

myglobal에 대해 생성된 코드를 보려면 View report 링크를 클릭하세요.

myglobal는 addglobal_imptr_data.h의 Variable 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.c의 Variable Definitions 섹션에서 변수를 정의합니다.

  • 헤더 파일 entry_point_name.h의 Variable Declarations 섹션에서 변수를 extern로 선언합니다.

  • entry_point_name_initialize.h 함수 내에서 변수를 초기화합니다.

'ExportedDefine'

헤더 파일 entry_point_name.h의 Exported data define 섹션에서 #define 지시문을 사용하여 변수를 선언합니다.

'ImportedExtern'

헤더 파일 entry_point_name_data.h의 Variable Declarations 섹션에서 변수를 extern로 선언합니다. 변수 정의는 외부 코드에서 제공해야 합니다.

'ImportedExternPointer'

헤더 파일 entry_point_name_data.h의 Variable Declarations 섹션에서 해당 변수를 extern 포인터로 선언합니다. 외부 코드에서는 유효한 포인터 변수를 정의해야 합니다.

  • 전역 변수에 스토리지 클래스를 지정하지 않으면, 선언 위치를 제외하고는 해당 변수가 'ExportedGlobal' 스토리지 클래스를 가진 것처럼 동작합니다. 'ExportedGlobal' 스토리지 클래스의 경우, 전역 변수는 entry_point_name.h 파일에 선언됩니다. 전역 변수에 스토리지 클래스가 지정되지 않은 경우, 해당 변수는 entry_point_name_data.h 파일에 선언됩니다.

데이터형: char

제한 사항

  • 전역 변수에 스토리지 클래스를 할당한 후에는 해당 전역 변수에 다른 스토리지 클래스를 할당할 수 없습니다.

  • 상수 전역 변수에는 스토리지 클래스를 할당할 수 없습니다.

확장 기능

모두 확장

C/C++ 코드 생성
MATLAB® Coder™를 사용하여 C 코드나 C++ 코드를 생성할 수 있습니다.

버전 내역

R2015b에 개발됨