title: Segmentation fault on icpc-compiled program
body: <p>I am having problems understanding the segmentation fault I receive when trying to run icpc-compiled programs.</p>
  
  <p>A simple example consists of the following files:</p>
  
  <pre class="lang-cpp prettyprint-override"><code>// Filename: include/lib1.h
  #include &lt;string&gt;
  
  namespace Lib1 {
    // Template initialization, T: int, double
    template&lt; typename T&gt;
      T function1( T x, T y );
  
    // Give me the version
    std::string VERSION(void);
  }
  </code></pre>
  
  <pre class="lang-cpp prettyprint-override"><code>// Filename: include/lib2.h
  #include &lt;string&gt;
  
  namespace Lib2 {
    // Give me the version
    std::string VERSION(void);
  }
  </code></pre>
  
  <pre class="lang-cpp prettyprint-override"><code>// Filename: src/main.cpp
  #include &lt;iostream&gt;
  #include &lt;string&gt;
  
  #include "lib1.h"
  #include "lib2.h"
  
  int main( int argc, char* argv[] ) {
    std::cout &lt;&lt; "Lib1::VERSION() :" &lt;&lt; Lib1::VERSION()
              &lt;&lt; std::endl;
    std::cout &lt;&lt; "Lib2::VERSION() :" &lt;&lt; Lib2::VERSION()
              &lt;&lt; std::endl;
  
    double x = 1., y = 2.;
  
    std::cout &lt;&lt; "Lib1::function1(x, y): "
              &lt;&lt; Lib1::function1(x, y)
              &lt;&lt; std::endl;
  
    return 0;
  }
  </code></pre>
  
  <pre class="lang-cpp prettyprint-override"><code>// Filename: src/lib1/lib1.cpp
  #include &lt;string&gt;
  
  #include "lib1.h"
  
  template&lt; typename T &gt;
  T Lib1::function1( T x, T y ) {
    return x * y;
  }
  
  std::string Lib1::VERSION(void) {
    return std::string("v0.0.2");
  }
  
  // Instantiation for dynamic library
  template double Lib1::function1(double, double);
  template int Lib1::function1(int, int);
  </code></pre>
  
  
  
  <pre><code>// Filename: src/lib2/lib2.cpp
  #include &lt;string&gt;
  
  #include "lib2.h"
  
  std::string Lib2::VERSION(void) {
    return std::string("v0.0.1");
  }
  </code></pre>
  
  <p>In this simple, stupid example, when I compile the files using</p>
  
  <pre><code>clang++ -Wall -c -fPIC -I include -o liblib1.o src/lib1/lib1.cpp
  clang++ -Wall -shared -o liblib1.so liblib1.o
  clang++ -Wall -c -fPIC -I include -o liblib2.o src/lib2/lib2.cpp
  clang++ -Wall -shared -o liblib2.so liblib2.o
  clang++ -Wall -o main.out -I include -L ./ -llib1 -llib2 src/main.cpp
  </code></pre>
  
  <p>the program runs fine (provided that I modify my <code>LD_LIBRARY_PATH</code> environment variable properly. However, when I use</p>
  
  <pre><code>icpc -Wall -c -fPIC -I include -o liblib1.o src/lib1/lib1.cpp
  icpc -Wall -shared -o liblib1.so liblib1.o
  icpc -Wall -c -fPIC -I include -o liblib2.o src/lib2/lib2.cpp
  icpc -Wall -shared -o liblib2.so liblib2.o
  icpc -Wall -o main.out -I include -L ./ -llib1 -llib2 src/main.cpp
  </code></pre>
  
  <p>then the program gives me:</p>
  
  <pre><code>[1]    27397 segmentation fault (core dumped) LD_LIBRARY_PATH=./:$LD_LIBRARY_PATH ./main.out
  </code></pre>
  
  <p>I would appreciate if you helped me understand and solve this problem. When I did some research on the web, I came across some sources talking about memory access problems and such, but I am not doing anything fancy right now. Moreover, I tried using <code>ddd</code> (I am not fluent/good in <code>gdb</code>) and running the program there, but the program exists with the segfault immidiately after the program start. I cannot even trace the program (yes, prior to running <code>ddd</code>, I used <code>-debug -g</code> switches).</p>