Can I export the used code from a c program with many compiler flags?

蹲街弑〆低调 提交于 2021-01-27 11:42:35

问题


I would like to condense our linux driver code in to only the code that runs on the current kernel. It has parts that are ignored by if statements all the way back to kernele 2.4.x

Have you ever heard of a way to compile the code to an output which will be the working code with out all the stuff ignored by the c compiler if else statements?

I am wondering if we can run make something or gcc something that will simply result in all the code that is used for that build.

So like if I had this .c file below, then after running the make command I should have just the code I need for the newest kernel.

example.c

static somefunction .... {
    avar = 0;
#if (linux_ver >= 2.6.31)
    some newer code
#elseif (linux_ver >= 2.4.24)
    some older code
#else
    original code
#endif

}

after extracting / condensing, example.c would simply read as below

static somefunction .... {
    avar = 0;
    some newer code
}

回答1:


The tool you are after is sunifdef or (more recent) coan.

See also: Is there a C pre-processor which eliminates #ifdef blocks based on values defined or undefined?




回答2:


That's what the preprocessor directives do already. Try running the code through gcc -E (but prepare for a lot of output, as all #includes will be inlined).



来源:https://stackoverflow.com/questions/9271984/can-i-export-the-used-code-from-a-c-program-with-many-compiler-flags

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!