如何在C中将一个指令定义为另一个指令?
ex:我要定义
#define #warning #warn
我收到错误
error #41: expected an identifier
例如,对于编译器和目标,某些编译器可识别#warning
,而另一些则可识别#warn
。
我已经有办法做到这一点,但我想使其通用化
#if PLATFORMS_TOOLCHAIN == PLATFORMS_ticgt
#warn "RAM is not initialized at startup, test will possibly fail"
#else
#warning "RAM is not initialized at startup, test will possibly fail"
#endif
答案 0 :(得分:4)
无法创建自己的预处理程序指令。 <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.1/css/bootstrap.css" rel="stylesheet"/>
<div class="card">
<div class="card-body">
<div class="table-wrapper-2">
<table class="table table-striped table-bordered table-fixed" id="entityTable">
<thead>
<tr class="table-primary sticky-top">
<th scope="col" style="width: 3%">#</th>
<th scope="col" style="width: 19%">ID</th>
<th scope="col" style="width:19%">Col 1</th>
<th scope="col" style="width: 19%">Col 2</th>
<th scope="col" style="width: 19%">Col 3</th>
<th scope="col" style="width: 19%">Col 4</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let id of transactionId, let i = index" class="clickable-row" (click)="onTransactionIdChange(id)">
<td>{{i+1}}</td>
<td>{{id}}</td>
<td>{{col1[i]}}</td>
<td>{{col2[i]}}</td>
<td>{{col3[i]}}</td>
<td>{{col4[i]}}</td>
</tr>
<tr *ngFor="let id of transactionId, let i = index" class="clickable-row" (click)="onTransactionIdChange(id)">
<td>{{i+1}}</td>
<td>{{id}}</td>
<td>{{col1[i]}}</td>
<td>{{col2[i]}}</td>
<td>{{col3[i]}}</td>
<td>{{col4[i]}}</td>
</tr>
<tr *ngFor="let id of transactionId, let i = index" class="clickable-row" (click)="onTransactionIdChange(id)">
<td>{{i+1}}</td>
<td>{{id}}</td>
<td>{{col1[i]}}</td>
<td>{{col2[i]}}</td>
<td>{{col3[i]}}</td>
<td>{{col4[i]}}</td>
</tr>
<tr *ngFor="let id of transactionId, let i = index" class="clickable-row" (click)="onTransactionIdChange(id)">
<td>{{i+1}}</td>
<td>{{id}}</td>
<td>{{col1[i]}}</td>
<td>{{col2[i]}}</td>
<td>{{col3[i]}}</td>
<td>{{col4[i]}}</td>
</tr>
<tr *ngFor="let id of transactionId, let i = index" class="clickable-row" (click)="onTransactionIdChange(id)">
<td>{{i+1}}</td>
<td>{{id}}</td>
<td>{{col1[i]}}</td>
<td>{{col2[i]}}</td>
<td>{{col3[i]}}</td>
<td>{{col4[i]}}</td>
</tr>
<tr *ngFor="let id of transactionId, let i = index" class="clickable-row" (click)="onTransactionIdChange(id)">
<td>{{i+1}}</td>
<td>{{id}}</td>
<td>{{col1[i]}}</td>
<td>{{col2[i]}}</td>
<td>{{col3[i]}}</td>
<td>{{col4[i]}}</td>
</tr>
<tr *ngFor="let id of transactionId, let i = index" class="clickable-row" (click)="onTransactionIdChange(id)">
<td>{{i+1}}</td>
<td>{{id}}</td>
<td>{{col1[i]}}</td>
<td>{{col2[i]}}</td>
<td>{{col3[i]}}</td>
<td>{{col4[i]}}</td>
</tr>
<tr *ngFor="let id of transactionId, let i = index" class="clickable-row" (click)="onTransactionIdChange(id)">
<td>{{i+1}}</td>
<td>{{id}}</td>
<td>{{col1[i]}}</td>
<td>{{col2[i]}}</td>
<td>{{col3[i]}}</td>
<td>{{col4[i]}}</td>
</tr>
<tr *ngFor="let id of transactionId, let i = index" class="clickable-row" (click)="onTransactionIdChange(id)">
<td>{{i+1}}</td>
<td>{{id}}</td>
<td>{{col1[i]}}</td>
<td>{{col2[i]}}</td>
<td>{{col3[i]}}</td>
<td>{{col4[i]}}</td>
</tr>
<tr *ngFor="let id of transactionId, let i = index" class="clickable-row" (click)="onTransactionIdChange(id)">
<td>{{i+1}}</td>
<td>{{id}}</td>
<td>{{col1[i]}}</td>
<td>{{col2[i]}}</td>
<td>{{col3[i]}}</td>
<td>{{col4[i]}}</td>
</tr>
<tr *ngFor="let id of transactionId, let i = index" class="clickable-row" (click)="onTransactionIdChange(id)">
<td>{{i+1}}</td>
<td>{{id}}</td>
<td>{{col1[i]}}</td>
<td>{{col2[i]}}</td>
<td>{{col3[i]}}</td>
<td>{{col4[i]}}</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
伪指令仅允许您定义新的源符号。
您当前正在做的是在不同的编译器上处理不同指令的正确方法。
答案 1 :(得分:0)
通常这是不可能的,但是您可以使用#pragma
有效地生成_Pragma keyword
指令。
#define PRAGMA(...) _Pragma(#__VA_ARGS__)
PRAGMA(message "my message")
//as if you wrote: #pragma message "my message"
考虑到这一点,您的编译器可能具有一些编译指示,可让您模拟#warn
或#error
(#pragma GCC warning "message"
或#pragma GCC error "message"
)。
如果您的编译器没有这种编译指示,则总是可以通过扩展到相对上下文无关且不可编译的内容(例如char ERROR[-1];
(负数组大小是非法的))来使编译失败。