<small id='YP81i'></small><noframes id='YP81i'>

    <legend id='YP81i'><style id='YP81i'><dir id='YP81i'><q id='YP81i'></q></dir></style></legend>
      <bdo id='YP81i'></bdo><ul id='YP81i'></ul>

        <i id='YP81i'><tr id='YP81i'><dt id='YP81i'><q id='YP81i'><span id='YP81i'><b id='YP81i'><form id='YP81i'><ins id='YP81i'></ins><ul id='YP81i'></ul><sub id='YP81i'></sub></form><legend id='YP81i'></legend><bdo id='YP81i'><pre id='YP81i'><center id='YP81i'></center></pre></bdo></b><th id='YP81i'></th></span></q></dt></tr></i><div id='YP81i'><tfoot id='YP81i'></tfoot><dl id='YP81i'><fieldset id='YP81i'></fieldset></dl></div>

      1. <tfoot id='YP81i'></tfoot>

        如何强制 gcc 链接未使用的静态库

        时间:2023-09-19
            <tbody id='gOIeG'></tbody>

            <bdo id='gOIeG'></bdo><ul id='gOIeG'></ul>

            1. <tfoot id='gOIeG'></tfoot>
            2. <legend id='gOIeG'><style id='gOIeG'><dir id='gOIeG'><q id='gOIeG'></q></dir></style></legend>

                • <small id='gOIeG'></small><noframes id='gOIeG'>

                  <i id='gOIeG'><tr id='gOIeG'><dt id='gOIeG'><q id='gOIeG'><span id='gOIeG'><b id='gOIeG'><form id='gOIeG'><ins id='gOIeG'></ins><ul id='gOIeG'></ul><sub id='gOIeG'></sub></form><legend id='gOIeG'></legend><bdo id='gOIeG'><pre id='gOIeG'><center id='gOIeG'></center></pre></bdo></b><th id='gOIeG'></th></span></q></dt></tr></i><div id='gOIeG'><tfoot id='gOIeG'></tfoot><dl id='gOIeG'><fieldset id='gOIeG'></fieldset></dl></div>

                  本文介绍了如何强制 gcc 链接未使用的静态库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有一个程序和一个静态库:

                  I have a program and a static library:

                  // main.cpp
                  int main() {}
                  
                  // mylib.cpp
                  #include <iostream>
                  struct S {
                      S() { std::cout << "Hello World
                  ";}
                  };
                  S s;
                  

                  我想将静态库(libmylib.a)链接到程序对象(main.o),虽然后者不使用前者的任何符号直接.

                  I want to link the static library (libmylib.a) to the program object (main.o), although the latter does not use any symbol of the former directly.

                  以下命令似乎不适用于 g++ 4.7.它们将在没有任何错误或警告的情况下运行,但显然 libmylib.a 不会被链接:

                  The following commands do not seem to the job with g++ 4.7. They will run without any errors or warnings, but apparently libmylib.a will not be linked:

                  g++ -o program main.o -Wl,--no-as-needed /path/to/libmylib.a
                  

                  g++ -o program main.o -L/path/to/ -Wl,--no-as-needed -lmylib
                  

                  你有更好的想法吗?

                  推荐答案

                  使用 --whole-archive 链接器选项.

                  在命令行中之后的库不会丢弃未引用的符号.您可以通过在这些库之后添加 --no-whole-archive 来恢复正常的链接行为.

                  Libraries that come after it in the command line will not have unreferenced symbols discarded. You can resume normal linking behaviour by adding --no-whole-archive after these libraries.

                  在您的示例中,命令将是:

                  In your example, the command will be:

                  g++ -o program main.o -Wl,--whole-archive /path/to/libmylib.a
                  

                  一般来说,它将是:

                  g++ -o program main.o 
                      -Wl,--whole-archive -lmylib 
                      -Wl,--no-whole-archive -llib1 -llib2
                  

                  这篇关于如何强制 gcc 链接未使用的静态库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:GCC 中的函数静态变量是线程安全的吗? 下一篇:GCC C++“你好世界"程序->.exe 在 Windows 上编译

                  相关文章

                  最新文章

                  <i id='8PkLX'><tr id='8PkLX'><dt id='8PkLX'><q id='8PkLX'><span id='8PkLX'><b id='8PkLX'><form id='8PkLX'><ins id='8PkLX'></ins><ul id='8PkLX'></ul><sub id='8PkLX'></sub></form><legend id='8PkLX'></legend><bdo id='8PkLX'><pre id='8PkLX'><center id='8PkLX'></center></pre></bdo></b><th id='8PkLX'></th></span></q></dt></tr></i><div id='8PkLX'><tfoot id='8PkLX'></tfoot><dl id='8PkLX'><fieldset id='8PkLX'></fieldset></dl></div>

                  <legend id='8PkLX'><style id='8PkLX'><dir id='8PkLX'><q id='8PkLX'></q></dir></style></legend>

                  <small id='8PkLX'></small><noframes id='8PkLX'>

                  • <bdo id='8PkLX'></bdo><ul id='8PkLX'></ul>

                  1. <tfoot id='8PkLX'></tfoot>