Search code examples
c++linuxstaticlinkerlibstdc++

Do we use libstdc++.a and libstdc++.so at the same time when generating executable?


If I choose to do:

gcc my.cpp -lstdc++

It links with libstdc++.so, right? But there're some c/c++ initialization part of code(global/static variables/objects, atexit() functions, etc), seems they should also require linking to libstdc++.a file.

So my question is, does the linking command always uses libstdc++.a for some reason, even if I specified to link with .so file?

Thanks.


Solution

  • gcc my.cpp -lstdc++

    This is usually the wrong thing to do. Instead, you should do this:

    g++ my.cpp
    

    It links with libstdc++.so, right?

    Depends on how GCC was configured and installed, but most often yes.

    But there're some c/c++ initialization part of code(global/static variables/objects, atexit() functions, etc), seems they should also require linking to libstdc++.a file.

    This is false. Where did you get this mistaken impression from?

    So my question is, does the linking command always uses libstdc++.a

    No, not usually.