-static-libstdc ++可以在g ++上使用,但不能在纯gcc上使用? [英] -static-libstdc++ works on g++ but not on pure gcc?
问题描述
作为参考,我正在使用MinGW(GCC 5.3).使用
For the reference I'm using MinGW (GCC 5.3). When compiling a file with
g++ file.cc -static-libstdc++
它静态链接C ++标准库(libstdc++
)并生成1.9MB可执行文件.
it statically links the C++ standard lib (libstdc++
) and produces a 1.9MB executable.
但是正在运行
gcc -lstdc++ -static-libstdc++ file.cc
它仍然动态链接到libstdc++-6.dll
并生成34KB可执行文件.
it still dynamically links to libstdc++-6.dll
and produces a 34KB executable.
为什么-static-libstdc++
仅适用于g++
,而不适用于纯gcc
?
Why does -static-libstdc++
only work with g++
but not with pure gcc
?
推荐答案
-static-libstdc ++
-static-libstdc++
使用g ++程序链接C ++程序时,通常会自动链接 针对libstdc ++.如果libstdc ++可作为共享库使用,则-static 选项未使用,则此链接指向libstdc ++的共享版本. 通常没关系.不过,冻结版本有时会很有用 程序使用的libstdc ++不会一直到完全静态的链接. -static-libstdc ++选项指示g ++驱动程序静态链接libstdc ++, 不必静态链接其他库.
When the g++ program is used to link a C++ program, it normally automatically links against libstdc++. If libstdc++ is available as a shared library, and the -static option is not used, then this links against the shared version of libstdc++. That is normally fine. However, it is sometimes useful to freeze the version of libstdc++ used by the program without going all the way to a fully static link. The -static-libstdc++ option directs the g++ driver to link libstdc++ statically, without necessarily linking other libraries statically.
这清楚表明选项-static-libstdc++
仅对
g++
编译器驱动程序,而不是gcc
或其他任何驱动程序.
This makes clear that the option -static-libstdc++
is meaningful only to
the g++
compiler driver, not gcc
or any other.
另一方面,选项-l<name>
是有意义的,并且含义相同
所有GCC编译器驱动程序.在此基础上不足为奇:
On the other hand the option -l<name>
is meaningful and means the same thing
to all GCC compiler drivers. On that basis it is not surprising that:
gcc file.cc -lstdc++ -static-libstdc++
与以下含义相同:
gcc file.cc -lstdc++
然而,这种观察并不能真正说明为什么第一个
命令行动态链接libstdc++
:-
However, that observation does not truly illuminate why the first of those
commandlines dynamically links libstdc++
:-
-static-libstdc++
仅对g++
有意义,因为仅g++
链接
libstdc++
自动.因此,仅对于g++
才出现问题
自动链接的libstdc++
是否为动态版本
或静态版本.动态版本是默认版本:-static-libstdc++
坚持使用静态版本.
-static-libstdc++
is meaningful only to g++
because only g++
links
libstdc++
automatically. So it is only for g++
that the question arises
whether the automatically linked libstdc++
will be the dynamic version
or the static version. The dynamic version is the default: -static-libstdc++
insists on the static version.
g++
的 libstdc++
自动链接 libstdc++
的意思是:g++
静默
将-lstdc++
附加到您指定的 链接选项中(以及
还有很多其他用于C ++链接的样板).你可以揭示所有
通过请求详细链接(g++ ... -Wl,-v ...
)来制作样板.
The automatic linking of libstdc++
by g++
means this: g++
silently
appends -lstdc++
to whatever linkage options you specify (along with
quite a lot of other boiler-plate for a C++ linkage). You can reveal all
the boilerplate by requesting verbose linkage (g++ ... -Wl,-v ...
).
本身,附加的-lstdc++
将使链接器链接动态版本
根据其默认行为,为libstdc++
的值.唯一的不同
-static-libstdc++
是在-lstdc++
否则会出现的地方
被悄悄地传递给链接器,这些选项:
By itself, the appended -lstdc++
will cause the linker to link the dynamic version
of libstdc++
, per its default behaviour. The only difference made by
-static-libstdc++
is that in the place where -lstdc++
would otherwise
be silently passed to the linker, the options:
-Bstatic -lstdc++ -Bdynamic
却无声地传递给它.这些告诉链接器:
are silently passed to it instead. These tell the linker:
-
-Bstatic
:除非另行通知,否则不要链接动态库 -
-lstdc++
:链接libstdc++
-
-Bdynamic
:链接动态库,直到另行通知.
-Bstatic
: Do not link dynamic libraries until further notice-lstdc++
: Linklibstdc++
-Bdynamic
: Link dynamic libraries until further notice.
您将了解如何在不使用libstdc++
的情况下确保libstdc++
的静态链接的安全性
消除了对其他任何库的链接的副作用.
You see how that works to secure the static linkage of libstdc++
without
out side-effects on the linkage of any other library.
但是您还可以看到libstdc++
的自动链接,无论
动态地或静态地,对链接没有追溯作用
您指定自己的所有库中的任何一个.
But you can also see that the automatic linkage of libstdc++
, whether
dynamically or statically, has no retroactive effect on the linkage
of any libraries you have specified yourself.
因此,如果您的链接在任何样板之前已经包含-lstdc++
选项由编译器驱动程序静默附加,然后将链接libstdc++
与链接中该位置的任何-l<name>
相同
顺序.并且,如果无声附加的样板选项导致-lstdc++
稍后会在链接序列中重新出现,无论是单独出现还是与
环境:
Hence, if your linkage already includes -lstdc++
before any boiler-plate
options are silently appended by the compiler driver, then libstdc++
will be linked
in just the same way as any -l<name>
at that position in the linkage
sequence. And if silently appended boiler-plate options result in -lstdc++
reappearing later in the linkage sequence, whether by itself or with
the surroundings:
-Bstatic -lstdc++ -Bdynamic
然后,以后的外观将只是 redundant ,因为该库具有 已经被链接.
then the later appearance will simply be redundant, because the library has already been linked.
因此,gcc
并没有什么特别的结果:
So there is nothing peculiar about gcc
that results in:
gcc file.cc -lstdc++ -static-libstdc++
生成其中libstdc++
是动态链接的 程序.
producing a program in which libstdc++
is dynamically linked. So does
g++ file.cc -lstdc++ -static-libstdc++
或者实际上:
g++ file.cc -static-libstdc++ -lstdc++
因为生成的链接器命令行格式为:
because the generated linker commandline is of the form:
... file.o -lstdc++ ... -Bstatic -lstdc++ -Bdynamic ...
其中-Bstatic -lstdc++ -Bdynamic
为时已晚,没有任何区别.
where -Bstatic -lstdc++ -Bdynamic
is too late to make any difference.
签出:
file.cc
#include <iostream>
int main()
{
std::cout << "Hello World" << std::endl;
return 0;
}
正常编译和链接,并使用ldd
检查动态依赖项:
Compile and link normally and inspect the dynamic dependencies with ldd
:
$ g++ -o prog file.cc
$ ldd prog
linux-vdso.so.1 => (0x00007ffede76a000)
libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f42fa74c000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f42fa385000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f42fa07c000)
/lib64/ld-linux-x86-64.so.2 (0x0000558ab42bc000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f42f9e65000)
libstdc++.so
存在.
现在只使用-static-libstdc++
:
$ g++ -o prog file.cc -static-libstdc++
$ ldd prog
linux-vdso.so.1 => (0x00007fff448d7000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007fe5f7c71000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fe5f78aa000)
/lib64/ld-linux-x86-64.so.2 (0x0000556ebf272000)
libstdc++.so
不存在.
最后是-static-libstdc++ -lstdc++
:
$ g++ -o prog file.cc -static-libstdc++ -lstdc++
$ ldd prog
linux-vdso.so.1 => (0x00007ffd12de9000)
libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007fd5a1823000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fd5a145c000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007fd5a1153000)
/lib64/ld-linux-x86-64.so.2 (0x000055bbe31c3000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007fd5a0f3c000)
libstdc++.so
又回来了.
(当然,这是Linux,但是在Windows上会发现相同的东西.)
(This is Linux, of course, but you'll find the same thing on Windows).
因此,无论您是使用g++
还是gcc
进行链接,其原因都是
So whether you drive your linkage with g++
or gcc
, the reason that
{gcc|g++} file.cc -lstdc++ ...
将导致libstdc++
被动态链接,仅仅是因为
will result in libstdc++
being dynamically linked is simply that
{gcc|g++} file.cc -lfoo ...
会导致libfoo
动态链接,无论如何
什么是...
,前提是...
不包含选项-static
.
will cause libfoo
to be dynamically linked, if it can be, regardless of
what ...
is, provided only that ...
does not contain the option -static
.
这篇关于-static-libstdc ++可以在g ++上使用,但不能在纯gcc上使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!