title: Clang Linking error: undefined reference to function calls added by LLVM pass
body: <p>So I am following this tutorial <a href="https://www.cs.cornell.edu/~asampson/blog/llvm.html" rel="nofollow noreferrer">https://www.cs.cornell.edu/~asampson/blog/llvm.html</a> to make a pass that instruments a program by adding calls to an external function (which is logop in rtlib.c). But unlike the tutorial I am trying to instrument a larger code-base which is masstree: <a href="https://github.com/kohler/masstree-beta" rel="nofollow noreferrer">https://github.com/kohler/masstree-beta</a>.</p>
  
  <p>So as instructed for masstree I run ./configure first but then I edit the generated Makefile to use clang (instead of gcc/g++) and run my pass. I also add rtlib.c in the masstree source files so that it gets converted to rtlib.o with the rest of the masstree source files. Here is the relevant part of the modified Makefile with my changes highlighted with an arrow (I also added the $(rtlib) to link it with other .o files to generate executables):</p>
  
  <pre><code>AR = ar
  rtlib = rtlib.o  &lt;===
  CC2 = clang -w -v -Xclang -load -Xclang  /.../llvm-3.4/Release+Asserts/lib/SkeletonPass.so `llvm-config --cflags` 
  CXX2 = clang++ -v -w -Xclang -load -Xclang /.../llvm-3.4/Release+Asserts/lib/SkeletonPass.so -std=c++11 `llvm-config --cppflags --libs --cflags --cxxflags core --ldflags` &lt;===
  CC = clang -v &lt;===
  CXX = clang++ -v -std=c++11 &lt;===
  CPPFLAGS = 
  CXXFLAGS = -g -W -Wall -O3
  DEPSDIR := .deps
  DEPCFLAGS = -MD -MF $(DEPSDIR)/$*.d -MP
  ifeq ($(strip $(MEMMGR)), )
    MEMMGR = 
  endif
  ifneq ($(strip $(KEYSWAP)), )
    CPPFLAGS += -DKEYSWAP
  endif
  ifneq ($(strip $(NOPREFETCH)), )
    CPPFLAGS += -DNOPREFETCH
  endif
  ifneq ($(strip $(NOSUPERPAGE)), )
    CPPFLAGS += -DNOSUPERPAGE
  endif
  LIBS =  -lpthread -lm
  LDFLAGS = 
  
  all: test_atomics mtd mtclient mttest
  
  %.o: %.c config.h $(DEPSDIR)/stamp
      $(CXX2) $(CPPFLAGS) $(CXXFLAGS) $(DEPCFLAGS) -include config.h -c -o $@ $&lt;
  
  %.o: %.cc config.h $(DEPSDIR)/stamp
      $(CXX2) $(CPPFLAGS) $(CXXFLAGS) $(DEPCFLAGS) -include config.h -c -o $@ $&lt;
  
  %.S: %.o
      objdump -S $&lt; &gt; $@
  
  libjson.a: json.o string.o straccum.o str.o msgpack.o \
      clp.o kvrandom.o compiler.o memdebug.o kvthread.o
      @/bin/rm -f $@
      $(AR) cru $@ $^
  
  KVTREES = query_masstree.o \
      value_string.o value_array.o value_versioned_array.o \
      string_slice.o
  
  mtd: mtd.o log.o checkpoint.o file.o misc.o $(rtlib) $(KVTREES) \
      kvio.o libjson.a
      $(CXX) $(CXXFLAGS) -o $@ $^ $(MEMMGR) $(LDFLAGS) $(LIBS)
  
  mtclient: mtclient.o misc.o testrunner.o kvio.o $(rtlib) libjson.a
      $(CXX) $(CXXFLAGS) -o $@ $^ $(LDFLAGS) $(LIBS)
  
  mttest: mttest.o misc.o checkpoint.o $(rtlib) $(KVTREES) testrunner.o \
      kvio.o libjson.a
      $(CXX) $(CXXFLAGS) -o $@ $^ $(MEMMGR) $(LDFLAGS) $(LIBS)
  
  test_string: test_string.o string.o $(rtlib) straccum.o compiler.o
      $(CXX) $(CXXFLAGS) -o $@ $^ $(MEMMGR) $(LDFLAGS) $(LIBS)
  
  test_atomics: test_atomics.o string.o straccum.o kvrandom.o $(rtlib) \
      json.o compiler.o kvio.o
      $(CXX) $(CXXFLAGS) -o $@ $^ $(MEMMGR) $(LDFLAGS) $(LIBS)
  
  jsontest: jsontest.o string.o straccum.o json.o compiler.o $(rtlib)
      $(CXX) $(CXXFLAGS) -o $@ $^ $(MEMMGR) $(LDFLAGS) $(LIBS)
  
  msgpacktest: msgpacktest.o string.o straccum.o json.o compiler.o msgpack.o $(rtlib)
      $(CXX) $(CXXFLAGS) -o $@ $^ $(MEMMGR) $(LDFLAGS) $(LIBS)
  
  scantest: scantest.o compiler.o misc.o $(rtlib) $(KVTREES) libjson.a
      $(CXX) $(CXXFLAGS) -o $@ $^ $(MEMMGR) $(LDFLAGS) $(LIBS)
  </code></pre>
  
  <p>I use CC2 and CXX2 to generate the instrumented .o files while CC and CXX to link them into executables. Here is the error I get when I run make:</p>
  
  <blockquote>
    <p>mtd.o: In function <code>main':
    /home/.../masstree-beta-master/mtd.cc:730: undefined reference to</code>logop'
    /home/.../masstree-beta-master/mtd.cc:730: undefined reference to <code>logop'
    /home/.../masstree-beta-master/mtd.cc:732: undefined reference to</code>logop'
    /home/.../masstree-beta-master/mtd.cc:732: undefined reference to <code>logop'
    /home/.../masstree-beta-master/mtd.cc:736: undefined reference to</code>logop'
    mtd.o:/home/.../masstree-beta-master/mtd.cc:736: more undefined references to `logop' follow
    clang: error: linker command failed with exit code 1 (use -v to see invocation)
    make: *** [mtd] Error 1.</p>
  </blockquote>
  
  <p>Any Idea on why is the reference to my function logop (which is in rtlib.c) undefined even though I add rtlib.o in the linking part to generate executables?</p>
  
  <p>Thank you so much!</p>