【问题标题】:Is there a way to tell the compiler to ignore certain chunk of code in C?有没有办法告诉编译器忽略 C 中的某些代码块?
【发布时间】:2020-12-29 02:39:21
【问题描述】:

我在 C/CUDA 上编写了一段代码,它将根据用户选择的值给出某些输出。 用户可以编辑这个定义——#define WIDTH 128 以下代码将基于它执行

/*              INTERNAL PROCESSING, IGNORE THIS SECTION                */
//Here we are initializing our datatype according to the width user entered and a random value which we will use to fill in our vectors
#if (WIDTH==8)
        typedef int8_t  dtype;
        dtype num = 4;
        #define isStruct 0
#elif (WIDTH==16)
        typedef int16_t dtype;
        dtype num = 4;
        #define isStruct 0
#elif (WIDTH==32)
        typedef int32_t dtype;
        dtype num = 4;
        #define isStruct 0
#elif (WIDTH==64)
        typedef int64_t dtype;
        dtype num = 4;
        #define isStruct 0
#elif (WIDTH==128)
        typedef struct dtype{
                int temp1;
                int temp2;
                int temp3;
                int temp4;
        }dtype;
        dtype num={4,4,4,4};
        #define isStruct 1
#endif

稍后在代码中我创建了一个指针并使用 malloc 分配了一些空间,我用 'num' 中包含的值填充了这些空间

dtype *a_d;
a_d=(dtype *)malloc( some_size_I_chose);
for(....)
{
    a_d[i]=num;
}

最后,我只是想验证事情是否按计划进行,所以我编写了这个验证循环 -

               if(isStruct == 0)
                {
                        if( a_d[i] != num )
                        {
                                printf("Error Detected at position %d\n", i);
                                *dangerflag=1;
                        }
                }
                //FOR HANDLING STRUCTS FOR 128 BITS
                else if(isStruct == 1)
                {
                        if( (a_d[i].temp1 != num.temp1) && (a_d[i].temp2 != num.temp2) && (a_d[i].temp3 != num.temp3) && (a_d[i].temp4 != num.temp4))
                        {
                                printf("Error Detected at position %d\n", i);
                                *dangerflag=1;
                        }
                }

但是,如果用户输入除 128 之外的任何值,编译器就会打印出错误

error: expression must have class type

现在我可以看出,由于 # 语句是预处理指令,因此预处理器定义了数据类型(如果定义的值不是 128,则不是结构),因此编译器在编译期间会出错,因为第二块代码在我的验证部分涉及到struct相关的操作。一种解决方案是将所有其他数据类型定义为类似这样的结构,然后对程序的其余部分进行后续更改 -

#if (WIDTH==8)
        typedef struct dtype{
                int8_t  val;
                }dtype;
        dtype num = {4};

但是我想知道是否可以告诉编译器不要根据某些条件编译某些代码。可能吗? 有没有其他更好的方法来解决这个问题?

【问题讨论】:

  • 您可以将代码包装在相同的预处理器宏中以有条件地编译它。
  • Cuda 名称错误。不是C/Cuda,而是C++/Cuda(或者,至少是7年前。我无法想象它已经改变了。)
  • #if isStruct == 1
  • 旁白:分配中不需要的类型。 a_d=(dtype *)malloc( some_size_I_chose); --> a_d = malloc( sizeof *a_d); - 至少在 C 中。

标签: c cuda


【解决方案1】:

但是我想知道是否可以告诉编译器不要根据某些条件编译某些代码。有可能吗?

你确实已经做到了。

#if (WIDTH==8)
    // this part is only compiled according to the condition (WIDTH==8)
        typedef int8_t  dtype;
        dtype num = 4;
        #define isStruct 0
#elif (WIDTH==16)
    // this part is only compiled according to the condition (WIDTH==16)
        typedef int16_t dtype;
        dtype num = 4;
        #define isStruct 0
#elif (WIDTH==32)
    // this part is only compiled according to the condition (WIDTH==32)
        typedef int32_t dtype;
        dtype num = 4;
        #define isStruct 0
#elif (WIDTH==64)
    // this part is only compiled according to the condition (WIDTH==64)
        typedef int64_t dtype;
        dtype num = 4;
        #define isStruct 0
#elif (WIDTH==128)
    // this part is only compiled according to the condition (WIDTH==128)
        typedef struct dtype{
                int temp1;
                int temp2;
                int temp3;
                int temp4;
        }dtype;
        dtype num={4,4,4,4};
        #define isStruct 1
#endif

再做一次。

【讨论】:

    猜你喜欢
    • 2010-09-09
    • 1970-01-01
    • 2020-05-03
    • 1970-01-01
    • 2010-11-19
    • 2015-01-07
    • 1970-01-01
    • 2015-08-22
    • 1970-01-01
    相关资源
    最近更新 更多