c++ - Variable dissapears in binary which is available in static lib -


i have problem mentioned. create object inside static lib, there when run nm on static lib, when link lib binary , run nm has disappeared. know problem has been asked before, not find answers. important me able retain variable in binary because trying implement version checks static libs in 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 _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 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 __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 _edata 0000000000601058 _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 

if nothing in program references object-file library, there no reason linker include object-file in executable. linker designed pick parts library needed resolve open dependencies.

when using static linking, options are:

  1. don't use libraries, pass relevant object files explicitly linker
  2. make sure @ least 1 of object files application depends on relevant object-file library.

perhaps dynamic linking can of assistance here, don't know sure.


Comments

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

jquery - appear modal windows bottom -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -