IT story

매크로 정의의 pragma

hot-time 2020. 8. 30. 19:50
반응형

매크로 정의의 pragma


pragma 문을 다른 문과 함께 매크로에 포함하는 방법이 있습니까?

나는 다음과 같은 것을 달성하려고 노력하고 있습니다.

#define DEFINE_DELETE_OBJECT(type)                      \
    void delete_ ## type_(int handle);                  \
    void delete_ ## type(int handle);                                                \
    #pragma weak delete_ ## type_ = delete_ ## type

부스트 솔루션 (웨이브 용으로 저장)이 있으면 괜찮습니다.


c99 또는 c ++ 0x를 사용하는 경우 다음과 같이 사용되는 pragma 연산자가 있습니다.

_Pragma("argument")

이는

#pragma argument

매크로에서 사용할 수 있다는 점을 제외하고 (c99 표준의 섹션 6.10.9 또는 c ++ 0x 최종위원회 초안의 16.9 참조)

예를 들면

#define STRINGIFY(a) #a
#define DEFINE_DELETE_OBJECT(type)                      \
    void delete_ ## type ## _(int handle);                  \
    void delete_ ## type(int handle);                   \
    _Pragma( STRINGIFY( weak delete_ ## type ## _ = delete_ ## type) )
DEFINE_DELETE_OBJECT(foo);

에 투입 할 때 gcc -E제공

void delete_foo_(int handle); void delete_foo(int handle);
#pragma weak delete_foo_ = delete_foo
 ;

_Pragma ( "argument")로 할 수있는 한 가지 좋은 점은 다음과 같은 일부 컴파일러 문제를 처리하는 데 사용하는 것입니다.

#ifdef _MSC_VER
#define DUMMY_PRAGMA _Pragma("argument")
#else
#define DUMMY_PRAGMA _Pragma("alt argument")
#endif

아니요, 이식 가능한 방법은 없습니다. 다시 말하지만, #pragma를 사용하는 이식 가능한 방법은 전혀 없습니다. 이 때문에 많은 C / C ++ 컴파일러는 pragma와 유사한 작업을 수행하기위한 자체 메서드를 정의하고 종종 매크로에 포함 할 수 있지만 모든 컴파일러에 대해 다른 매크로 정의가 필요합니다. 그 길을 갈 의향이 있다면, 종종 다음과 같은 일을하게됩니다.

#if defined(COMPILER_GCC)
#define Weak_b
#define Weak_e __attribute__((weak))
#elif defined(COMPILER_FOO)
#define Weak_b __Is_Weak
#define Weak_e
#endif

#define DEFINE_DELETE_OBJECT(type)                      \
    Weak_b void delete_ ## type_(int handle) Weak_e;    \
    Weak_b void delete_ ## type(int handle)  Weak_e;    

GCC와 같은 일부 컴파일러는 속성을 유형 서명에 추가로 추가하고 MSC와 같은 일부는 접두사로 추가하기 때문에 정의 Weak_b하고 Weak_e시작 및 끝 브라케팅 구문 으로 정의 하고 싶은 경우가 명확하지 않은 경우 (또는 적어도 MSC를 사용한 지 몇 년이 지났습니다.) 브라케팅 구조를 사용하면 전체 유형 서명을 컴파일러 구조로 전달해야하는 경우에도 항상 작동하는 것을 정의 할 수 있습니다.

Of course, if you try porting this to a compiler without the attributes you want, there's nothing you can do but leave the macros expand to nothing and hope your code still runs. In case of purely warning or optimizing pragmas, this is likely. In other cases, not so much.

Oh, and I suspect you'd actually need to define Weak_b and Weak_e as macros that take parameters, but I wasn't willing to read through the docs for how to create a weak definition just for this example. I leave that as an exercise for the reader.


is there some way to embed pragma statement in macro with other statements?

No, you cannot put preprocessor statements into preprocessor statements. You could, however, put it into an inline function. That defeats the C tag, though.

참고URL : https://stackoverflow.com/questions/3030099/pragma-in-define-macro

반응형