title: Undefined symbol in shared library for JNI, executable is working
body: <p>I am really unexperienced when it comes to C++ so I hope this just a small mistake that causes this error. Sorry that the post is so long but I tried to include all the important details.</p>
  
  <p>So here is what I am trying to do: I am using an external library that comes in the form of two folders with a makefile each. I am trying to create a JNI library with one additional cpp file and the JNI header in an .h file.</p>
  
  <p>So the folder structure is like this:
  -> libcds/src with a makefile that creates libcds.a<br>
  -> SSA2 with a makefile that creates SSA.a<br>
  -> myFolder with my cpp and h file and a makefile that should create the library  </p>
  
  <p>So here are the different makefiles:</p>
  
  <p>libcds/src:</p>
  
  <pre><code>CPP=g++
  
  CPPFLAGS=-O9 -w -DNDEBUG 
  
  INCL=-I../includes/
  
  CODERS_DIR=coders
  CODERS_OBJECTS=$(CODERS_DIR)/huff.o $(CODERS_DIR)/huffman_codes.o
  
  STATIC_PERMUTATION_DIR=static_permutation
  STATIC_PERMUTATION_OBJECTS=$(STATIC_PERMUTATION_DIR)/perm.o ...
  
  STATIC_BITSEQUENCE_DIR=static_bitsequence
  STATIC_BITSEQUENCE_OBJECTS=$(STATIC_BITSEQUENCE_DIR)/static_bitsequence.o ..
  
  STATIC_SEQUENCE_DIR=static_sequence
  STATIC_SEQUENCE_OBJECTS=$(STATIC_SEQUENCE_DIR)/static_sequence.o, ..., wt_coder.o
  
  UTILS_DIR=utils
  UTILS_OBJECTS=$(UTILS_DIR)/alphabet_mapper_none.o $(UTILS_DIR)/alphabet_mapper.o
  
  %.o: %.cpp
  
  $(CPP) $(CPPFLAGS) $(INCL) -c $&lt; -o $@
  
  all: lib 
  
  lib: pre $(CODERS_OBJECTS) $(STATIC_BITSEQUENCE_OBJECTS) $(STATIC_SEQUENCE_OBJECTS) $(UTILS_OBJECTS) $(STATIC_PERMUTATION_OBJECTS)
      ar rcs ../lib/libcds.a $(CODERS_OBJECTS) $(STATIC_BITSEQUENCE_OBJECTS) $(STATIC_SEQUENCE_OBJECTS) $(UTILS_OBJECTS) $(STATIC_PERMUTATION_OBJECTS)
  
  pre:
      cp basics.h ../includes/
      cp */*.h ../includes/
  </code></pre>
  
  <p>SSA2:</p>
  
  <pre><code>CC=g++
  
  CFLAGS=-w -O9 -DNDEBUG -I../libcds/includes/
  CODERS_DIR=../libcds/src/coders
  
  CODERS_OBJECTS=$(CODERS_DIR)/huff.o $(CODERS_DIR)/huffman_codes.o
  
  STATIC_BITSEQUENCE_DIR=../libcds/src/static_bitsequence
  
  STATIC_BITSEQUENCE_OBJECTS=$(STATIC_BITSEQUENCE_DIR)/static_bitsequence.o ...
  
  STATIC_SEQUENCE_DIR=../libcds/src/static_sequence
  
  STATIC_SEQUENCE_OBJECTS=$(STATIC_SEQUENCE_DIR)/static_sequence.o ...
  
  UTILS_DIR=../libcds/src/utils
  
  UTILS_OBJECTS=$(UTILS_DIR)/alphabet_mapper_none.o $(UTILS_DIR)/alphabet_mapper.o
  
  all: index
  
  # pattern rule for all objects files
  
  %.o: %.c *.h
  
      $(CC) -c $(CFLAGS) $&lt; -o $@
  
  %.o: %.cpp *.h
  
      $(CC) -c $(CFLAGS) $&lt; -o $@
  
  index: bitrankw32int.o bitarray.o  ds/ds.o ds/globals.o ds/helped.o ds/shallow.o ds/deep2.o ds/blind2.o SSA.o
  
      cp ../libcds/lib/libcds.a SSA.a
  
      ar rcs SSA.a SSA.o bitrankw32int.o bitarray.o ds/ds.o ds/globals.o ds/shallow.o ds/helped.o ds/deep2.o ds/blind2.o
  </code></pre>
  
  <p>My makefile:</p>
  
  <pre><code>CC=g++ -I $(JAVA_HOME)/include -I $(JAVA_HOME)/include/linux
  LD_LIBRARY_PATH=./
  JNI_NAME= indexlib
  
  all: test
  
  %.o: %.c *.h
      $(CC) -c $(CFLAGS) $&lt; -o $@
  
  test_index: 
      $(CC) run_queries.c -o testIndex SSA.a
  
  test_index_lib: run_queries.o
      ld -shared -soname lib$(JNI_NAME).so -o lib$(JNI_NAME).so run_queries.o SSA.a -lc
  </code></pre>
  
  <p>The executable testIndex works without issues.
  The resulting library, however can't be loaded by Java with the error message:</p>
  
  <blockquote>
    <p>undefined symbol: _ZNSt8ios_base4InitC1Ev</p>
  </blockquote>
  
  <p>When I use nm to find that symbol I get the following result:</p>
  
  <blockquote>
    <p>000131b0 W _ZN8wt_coderD2Ev<br>
             U _ZNKSt5ctypeIcE13_M_widen_initEv<br>
             U _ZNSo3putEc<br>
             U _ZNSo5flushEv<br>
             U _ZNSo9_M_insertImEERSoT_<br>
             U _ZNSt8ios_base4InitC1Ev<br>
             U _ZNSt8ios_base4InitD1Ev<br>
             U _ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_i<br>
             U _ZSt16__throw_bad_castv<br>
             U _ZSt4cout  </p>
    
    <p>And with --demangle it looks like this: 000131b0 W
    wt_coder::~wt_coder()<br>
             U std::ctype::_M_widen_init() const<br>
             U std::ostream::put(char)<br>
             U std::ostream::flush()<br>
             U std::ostream&amp; std::ostream::_M_insert(unsigned long)<br>
             U std::ios_base::Init::Init()<br>
             U std::ios_base::Init::~Init()<br>
             U std::basic_ostream >&amp; std::__ostream_insert</p>
    
    <blockquote>
      <p>(std::basic_ostream >&amp;, char const*, int)
               U std::__throw_bad_cast()<br>
               U std::cout</p>
    </blockquote>
  </blockquote>
  
  <p>So actually this could be related to <a href="https://stackoverflow.com/questions/4035445/linker-error-undefined-reference-to-stdctypechar-m-widen-init">Linker error: undefined reference to `std::ctype&lt;char&gt;::_M_widen_init()</a></p>