简体   繁体   English

未调用的静态c函数会发生什么

[英]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 funcfunc2

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.

相关问题 从被调用的时间到它返回的时间,在C中调用的函数会发生什么? - What happens to a function called in C from the time being called to the time it returns? 在函数内部初始化静态变量会发生什么 - What happens to initialization of static variable inside a function 什么是 C 中的“静态”函数? - What is a "static" function in C? 如果编译器内联通过函数指针调用的函数会发生什么 - What happens if compiler inlines a function which is called through a function pointer C 编程这个递归 function 究竟发生了什么 - C Programming what happens exactly with this recursive function C,函数完成后的变量会发生什么? - C, what happens with the variables of a function when it finish? 在C语言中取消引用静态变量时会发生什么情况? - What exactly happens when you dereference a static variable in C? 如果C中没有足够的内存用于静态分配,会发生什么情况? - What happens when there is not enough memory for a static allocation in C? 当非静态函数声明跟在静态函数声明之后会发生什么? - What happens when non-static function declaration follows static function declaration? 可以通过C中的函数指针调用静态函数吗? - Can a static function be called through a function pointer in C?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM