C std library don't appear to be linked in object file

人走茶凉 提交于 2019-12-13 05:04:48

问题


I am using a program like this with math.h function "sin" and stdio.h function"printf" used

#include <stdio.h>
#include <math.h>

int main ()
{

    int x = sin(14);
    printf("hello");
    return 0;
}

And as stated by ephemient here that libc.so and libm.so (for math functions) should have been linked with the program , though when I run otool (similar to objdump) on the object file with the option "-L" that prints the shared libraries used, None of libc.so or libm.so are printed out

otool -L com_ex1.o

so what is the reason for this ? Am I using otool wrong? or the those libraries shouldn't appear as shared libraries ?


回答1:


Dynamic libraries are linked to the final executable, not to the object files, so you should run (e.g.)

otool -L com_ex1

This should show something like

com_ex1:
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1213.0.0)

because on OS X, the math library is part of libSystem:

$ ls -l /usr/lib/libm.dylib
lrwxr-xr-x  1 root  wheel  15  3 Jun 01:39 /usr/lib/libm.dylib@ -> libSystem.dylib



回答2:


You link the finished binaries, the intermidiate object files are not linked until they are linked together in a final binary along with the libraries used.

So when you generate an object file no linking occurs, thus it's logical that there is no evidence of a link to any library in the object file, because there was none.



来源:https://stackoverflow.com/questions/30694042/c-std-library-dont-appear-to-be-linked-in-object-file

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