包括文件与不同的宏多次 [英] Include a file multiple times with different macros
问题描述
我得到了一个包含有不同数量的类型的数组工作泛型方法的文件(的基本思路在伪泛型用C <描述/ A>)。该类型可以通过设置键入
宏指定。它看起来像这样(只是它的一部分):
I got a file that contains generic methods for working with arrays of different number types (the basic ideas are described in Pseudo-generics in C). The type can be specified by setting a TYPE
macro. It looks like this (just a part of it):
array_analyzers.c
:
#ifndef TYPE
#error TYPE isn't defined
#endif
#define CONCAT(x, y) x ## y
#define GET_NAME(BASE, TYPE) CONCAT(BASE, TYPE)
TYPE GET_NAME(get_minimum_, TYPE) (TYPE nums[static 1], int len){
TYPE min = nums[0];
for (int i = 1; i < len; i++) {
if (nums[i] < min) {
min = nums[i];
}
}
return min;
}
#undef CONCAT
#undef GET_NAME
#undef TYPE
现在,我创建了一个头文件看起来像这样:
Now, I created a header file that looks like this:
array_analyzers.h
:
#ifndef TYPE
#error TYPE isn't defined
#endif
#define CONCAT(x, y) x ## y
#define GET_NAME(BASE, TYPE) CONCAT(BASE, TYPE)
TYPE GET_NAME(get_minimum_, TYPE) (TYPE nums[static 1], int len);
#undef CONCAT
#undef GET_NAME
#undef TYPE
最后,我想用这个从的main.c
:
#include <stdio.h>
#define TYPE int
#include "array_analyzers.h"
#define TYPE double
#include "array_analyzers.h"
int main(void){
int nums[] = {1, 2, 3};
printf("%i\n", get_minimum_int(nums, 3));
}
不过,我得到了以下错误消息:
However, I'm getting the following error message:
array_analyzers.c:2:2: error: #error TYPE isn't defined
到底什么是错在这里?当我第一次运行preprocessor并创建正确的内容单独的文件它的工作原理,但是这仅仅是可怕的。
What exactly is wrong here? It works when I run the preprocessor first and create separate files with the right contents, but that is just awful.
推荐答案
您不是唐的声明在范围内开始的循环中的int,但在一个for循环。如果你正在使用gcc,你可以添加-std = C99编译器选项来接受这个code。
You don not declare the int in the loop at the beginning of a scope, but in a for loop. If you are using gcc you can add -std=c99 compiler option to accept this code.
在此函数是一个标准的循环应该永久被接受。在这个函数int我是在范围的开始申报。
in this function is a standard loop that should alway be accepted. In this function int i is declared at the beginning of a scope.
int
ansi_c (){
int i;
for ( i = 0 ; i < 10; i++ ){
}
}
在这个函数是一个循环,只有接受时-std = C99或std = gnu99
加入。这个函数声明了一个循环内的int值。
and in this function is a loop that is only accepted when -std=c99 or std=gnu99 is added. This function declares the int inside a loop.
c99(){
for (int i = 0; i < 10; i++ ){
}
}
这篇关于包括文件与不同的宏多次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!