[英]what happens to a static c function that is not called
if you have something like this: 如果你有这样的事情:
int _tmain(int argc, _TCHAR* argv[]) {
int i;
#if (num>99)
i = func();
#else
i= func2();
#endif
return 0;
}
static int func()
{
return 1;
}
static int func2()
{
return 2;
}
Is it reasonable to expect that depending on if num
is bigger or smaller then 99 ether func
or func2
will be removed from the runtime code? 是否合理期望取决于
num
是更大还是更小,那么将从运行时代码中删除99 ether func
或func2
?
Or would I rather need to also embed the functions in an #if
to achieve this goal? 或者我是否还需要在
#if
嵌入函数来实现这一目标?
This depends on linker,what does it with dead code is linker specific. 这取决于链接器,死代码与链接器特定有什么关系。 You should also include function definition under
#if
to make sure that it wont results into machine code. 您还应该在
#if
下包含函数定义,以确保它不会导致机器代码。
It depends on optimization level. 这取决于优化级别。 On linux you can check it youself using
readelf -s ./a.out | grep func2
在linux上,您可以使用
readelf -s ./a.out | grep func2
readelf -s ./a.out | grep func2
But I think you use windows, so you need some similar tool http://www.pe-explorer.com/ for example. 但我认为你使用的是windows,所以你需要一些类似的工具http://www.pe-explorer.com/ 。
Here is list of tools: Any tool/software in windows for viewing ELF file format? 以下是工具列表: Windows中用于查看ELF文件格式的任何工具/软件?
You would need to embed the function definitions also in an #if to achieve the goal. 您还需要在#if中嵌入函数定义以实现目标。
code may be something like this: 代码可能是这样的:
Let's say the variable "num" is getting populated form configuration. 假设变量“num”正在填充表单配置。
int _tmain(int argc, _TCHAR* argv[]) {
int i;
#if (num>99)
i = func();
#else
i= func2();
#endif
return 0;
}
#if(num>99)
static int func()
{
return 1;
}
#else
static int func2()
{
return 2;
}
#endif
Hope it helps. 希望能帮助到你。 Thank you!
谢谢!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.