用-static编译命令怎么会有这么大的内存差异?(C)

时间:2022-09-06 16:19:09

I am working on a task for the university, there is a webiste that checks my memory usage and it compiles the .c files with:

我正在为大学工作,有一个网站检查我的内存使用情况,它编译.c文件:

 /usr/bin/gcc -DEVAL -std=c11 -O2 -pipe -static -s -o program programname.c -lm

and it says my program exceeds the memory limits of 4 Mib which is a lot i think. I was told this command makes it use more memory that the standard compilation I use on my pc, like this:

并且它说我的程序超出了4 Mib的内存限制,这是我认为的很多。我被告知这个命令使它使用我在我的电脑上使用的标准编译更多的内存,如下所示:

 gcc myprog.c -o myprog

I launched the executable created by this one compilation with:

我启动了这个编译创建的可执行文件:

/usr/bin/time -v ./myprog

and under "maximum resident set size" it says 1708 kilobytes, which should be 1,6 Mibs. So how can it be that for the university checker my program goes over 4 Mibs? I have eliminated all the possible mallocs i have, I just left the essential ones but it still says it goes over the limit, what else should I improve? I'm almost thinking the wesite has an error or something...

在“最大居民规模”下,它表示1708千字节,应该是1,6 Mibs。那么对于大学检查员来说,我的计划如何超过4个Mibs呢?我已经消除了我所拥有的所有可能的mallocs,我只是离开了必要的那些,但它仍然说它超过了限制,我还应该改进什么?我差点以为wesite有错误或什么......

1 个解决方案

#1


0  

From GNU GCC Manual, Page 197:

从GNU GCC手册,第197页:

-static On systems that support dynamic linking, this overrides ‘-pie’ and prevents linking with the shared libraries. On other systems, this option has no effect.

-static在支持动态链接的系统上,这将覆盖'-pie'并阻止与共享库的链接。在其他系统上,此选项无效。

If you don't know about the pie flag quoted here, have a look at this section:

如果您不知道此处引用的饼图,请查看此部分:

-pie Produce a dynamically linked position independent executable on targets that support it. For predictable results, you must also specify the same set of options used for compilation (‘-fpie’, ‘-fPIE’, or model suboptions) when you specify this linker option.

-pie在支持它的目标上生成动态链接的位置无关可执行文件。对于可预测的结果,在指定此链接器选项时,还必须指定用于编译的相同选项集('-fpie',' - fPIE'或模型子选项)。

To answer your question: yes is it possible this overhead generated by the static flag, because in that case, the compiler can not do the basic optimization by merging stdlib's code with the one you've produced.

回答你的问题:是的,静态标志可能产生这种开销,因为在这种情况下,编译器无法通过将stdlib的代码与你生成的代码合并来进行基本优化。

As it was suggested in the comments you shall compile your code with the same flag of the website to have an idea of the real overhead of your program (be sure that your gcc version is the same of the website) and also you shall do some common manual optimization such constant folding, function inlining etc. A good reference to these optimizations could be this one

正如评论中建议的那样,您应该使用相同的网站标志编译您的代码,以了解您的程序的实际开销(确保您的gcc版本与网站相同),并且您还应该做一些常见的手动优化,例如常数折叠,函数内联等。这些优化的一个很好的参考可能就是这个

#1


0  

From GNU GCC Manual, Page 197:

从GNU GCC手册,第197页:

-static On systems that support dynamic linking, this overrides ‘-pie’ and prevents linking with the shared libraries. On other systems, this option has no effect.

-static在支持动态链接的系统上,这将覆盖'-pie'并阻止与共享库的链接。在其他系统上,此选项无效。

If you don't know about the pie flag quoted here, have a look at this section:

如果您不知道此处引用的饼图,请查看此部分:

-pie Produce a dynamically linked position independent executable on targets that support it. For predictable results, you must also specify the same set of options used for compilation (‘-fpie’, ‘-fPIE’, or model suboptions) when you specify this linker option.

-pie在支持它的目标上生成动态链接的位置无关可执行文件。对于可预测的结果,在指定此链接器选项时,还必须指定用于编译的相同选项集('-fpie',' - fPIE'或模型子选项)。

To answer your question: yes is it possible this overhead generated by the static flag, because in that case, the compiler can not do the basic optimization by merging stdlib's code with the one you've produced.

回答你的问题:是的,静态标志可能产生这种开销,因为在这种情况下,编译器无法通过将stdlib的代码与你生成的代码合并来进行基本优化。

As it was suggested in the comments you shall compile your code with the same flag of the website to have an idea of the real overhead of your program (be sure that your gcc version is the same of the website) and also you shall do some common manual optimization such constant folding, function inlining etc. A good reference to these optimizations could be this one

正如评论中建议的那样,您应该使用相同的网站标志编译您的代码,以了解您的程序的实际开销(确保您的gcc版本与网站相同),并且您还应该做一些常见的手动优化,例如常数折叠,函数内联等。这些优化的一个很好的参考可能就是这个