本文介绍了二进制中的变量disapears在静态库中可用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有提到的问题。我在静态库中创建一个对象,当我在静态库中运行 nm 时,但是当我将lib链接到一个二进制文件并运行 nm 它已经消失。我知道这个问题以前被问过,但我找不到任何答案。对我来说重要的是能够在二进制中保留该变量,因为我们试图对二进制中的静态库执行版本检查。请帮忙。谢谢。示例代码如下:



test.h

  #ifndef TEST_H 
#define TEST_H

class Test
{
public:
Test();
};

extern Test * gpTest;

#endif

test.cpp

  #includetest.h )
{
gpTest = this;
}

测试测试;

main.cpp

  #includetest.h
#include< iostream>

using namespace std;

Test * gpTest = NULL;

int main()
{
return 0;
}

BUILD

  g ++ -c test.cpp -o test.o 
ar cr test.a test.o
g ++ main.cpp -o app -L / home / duminda / intest / test.a

test.a

  0000000000000054 t全局构造函数键入_ZN4TestC2Ev 
000000000000002b t __static_initialization_and_destruction_0 int,int)
0000000000000016 T Test :: Test()
0000000000000000 T Test :: Test()
U __gxx_personality_v0
U gpTest
0000000000000000 B test

nm -C app
$ b <$ c $ p> 0000000000600e10 d _DYNAMIC
0000000000600fe8 d _GLOBAL_OFFSET_TABLE_
0000000000400754 t全局构造函数键入gpTest
0000000000400858 R _IO_stdin_used
w _Jv_RegisterClasses
0000000000400717 t __static_initialization_and_destruction_0(int,int)
U std :: ios_base :: Init :: Init()@@ GLIBCXX_3.4
U std :: ios_base :: Init ::〜 Init()@@ GLIBCXX_3.4
0000000000601050 b std :: __ ioinit
0000000000600df0 d __CTOR_END__
0000000000600de0 d __CTOR_LIST__
0000000000600e00 D __DTOR_END__
0000000000600df8 d __DTOR_LIST__
0000000000400968 r __FRAME_END__
0000000000600e08 d __JCR_END__
0000000000600e08 d __JCR_LIST__
0000000000601038 A __bss_start
U __cxa_atexit @@ GLIBC_2.2.5
0000000000601028 D __data_start
0000000000400810 t __do_global_ctors_aux
0000000000400670 t __do_global_dtors_aux
0000000000601030 D __dso_handle
w __gmon_start__
U __gxx_personality_v0 @@ CXXABI_1.3
0000000000600ddc d __init_array_end
0000000000600ddc d __init_array_start
0000000000400770 T __libc_csu_fini
0000000000400780 T __libc_csu_init
U __libc_start_main @@ GLIBC_2.2.5
0000000000601038 A _edata
0000000000601058 A _end
0000000000400848 T _fini
00000000004005a0 T _init
0000000000400620 T _start
000000000040064c t call_gmon_start
0000000000601038 b completed.6096
0000000000601028 W data_start
0000000000601040 b dtor_idx.6098
00000000004006e0 t frame_dummy
0000000000601048 B gpTest
000000000040070c T main


解决方案

寻找:D。看起来链接器标志 - 整个归档是确保所有符号在静态库,无论是否由应用程序访问或不进入应用程序。虽然我仍然无法让这个工作,我想我在正确的道路。


I have the problem mentioned. I create an object inside a static lib, it is there when I run nm on the static lib, but when i link the lib to a binary and run nm it has disappeared. I know this problem has been asked before, but I could not find any answers. It is important for me to be able to retain that variable in the binary because we are trying to implement version checks for static libs in a binary. Please help. Thank you. Example code follows:

test.h

#ifndef TEST_H
#define TEST_H

class Test
{   
    public:
        Test();
};

extern Test* gpTest;

#endif

test.cpp

#include "test.h"

Test::Test()
{
    gpTest = this;
}   

Test test;

main.cpp

#include "test.h"
#include <iostream>

using namespace std;

Test* gpTest = NULL;

int main()
{
    return 0;
}

BUILD

g++ -c test.cpp -o test.o
ar cr test.a test.o
g++ main.cpp -o app -L/home/duminda/intest/test.a

nm -C test.a

0000000000000054 t global constructors keyed to _ZN4TestC2Ev
000000000000002b t __static_initialization_and_destruction_0(int, int)
0000000000000016 T Test::Test()
0000000000000000 T Test::Test()
                 U __gxx_personality_v0
                 U gpTest
0000000000000000 B test

nm -C app

0000000000600e10 d _DYNAMIC
0000000000600fe8 d _GLOBAL_OFFSET_TABLE_
0000000000400754 t global constructors keyed to gpTest
0000000000400858 R _IO_stdin_used
                 w _Jv_RegisterClasses
0000000000400717 t __static_initialization_and_destruction_0(int, int)
                 U std::ios_base::Init::Init()@@GLIBCXX_3.4
                 U std::ios_base::Init::~Init()@@GLIBCXX_3.4
0000000000601050 b std::__ioinit
0000000000600df0 d __CTOR_END__
0000000000600de0 d __CTOR_LIST__
0000000000600e00 D __DTOR_END__
0000000000600df8 d __DTOR_LIST__
0000000000400968 r __FRAME_END__
0000000000600e08 d __JCR_END__
0000000000600e08 d __JCR_LIST__
0000000000601038 A __bss_start
                 U __cxa_atexit@@GLIBC_2.2.5
0000000000601028 D __data_start
0000000000400810 t __do_global_ctors_aux
0000000000400670 t __do_global_dtors_aux
0000000000601030 D __dso_handle
                 w __gmon_start__
                 U __gxx_personality_v0@@CXXABI_1.3
0000000000600ddc d __init_array_end
0000000000600ddc d __init_array_start
0000000000400770 T __libc_csu_fini
0000000000400780 T __libc_csu_init
                 U __libc_start_main@@GLIBC_2.2.5
0000000000601038 A _edata
0000000000601058 A _end
0000000000400848 T _fini
00000000004005a0 T _init
0000000000400620 T _start
000000000040064c t call_gmon_start
0000000000601038 b completed.6096
0000000000601028 W data_start
0000000000601040 b dtor_idx.6098
00000000004006e0 t frame_dummy
0000000000601048 B gpTest
000000000040070c T main
解决方案

I found what I was looking for :D. It seems that linker flag --whole-archive is there to make sure all symbols in static lib regardless of whether they are accessed by app or not get into app. Though I still have trouble getting this to work, I guess I am on the right path.

这篇关于二进制中的变量disapears在静态库中可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 05:59