Building LLVM pass out of source - undefined symbols
<p>I'm attempting to build an LLVM pass using the instructions <a href="http://llvm.org/docs/WritingAnLLVMPass.html" rel="nofollow">here</a> and link it against the copy of LLVM installed by Julia. The pass is currently being compiled successfully, but cmake fails on linking with undefined symbol errors. </p>

<pre><code>[ 50%] Building CXX object VectorizePass/CMakeFiles/VectorizePass.dir/VectorizePass.cpp.o
[100%] Linking CXX shared module libVectorizePass.so
Undefined symbols for architecture x86_64:
    "llvm::raw_ostream::write_escaped(llvm::StringRef, bool)", referenced from:
  (anonymous namespace)::Hello::runOnFunction(llvm::Function&amp;) in VectorizePass.cpp.o
   "llvm::raw_ostream::write(char const*, unsigned long)", referenced from:
        llvm::raw_ostream::operator&lt;&lt;(llvm::StringRef) in VectorizePass.cpp.o
</code></pre>

<p>There are dozens of undefined symbol errors followed by </p>

<pre><code>"vtable for llvm::Pass", referenced from:
  llvm::Pass::Pass(llvm::PassKind, char&amp;) in VectorizePass.cpp.o
NOTE: a missing vtable usually means the first non-inline virtual 
    member function has no definition.
</code></pre>

<p><strong>VectorizePass.cpp</strong> </p>

<pre><code>#include "llvm/Pass.h"
#include "llvm/IR/Function.h"
#include "llvm/Support/raw_ostream.h"


using namespace llvm;

namespace {
    struct Hello : public FunctionPass {
        static char ID;
        Hello() : FunctionPass(ID) {}

        bool runOnFunction(Function &amp;F) override {
            errs() &lt;&lt; "Hello: ";
            errs().write_escaped(F.getName()) &lt;&lt; "\n";
            return false;
        }
    };
}

char Hello::ID = 0;
static RegisterPass&lt;Hello&gt; X("Hello",  "My Hello World Pass",  false, false);
</code></pre>

<p>This is exactly as it is in the tutorial. </p>

<p><strong>CMakeLists.txt (1)</strong></p>

<pre><code>add_library(VectorizePass MODULE VectorizePass.cpp)
SET(CMAKE_CXX_FLAGS "-std=c++11 -Wall -fno-rtti -D__STDC_CONSTANT_MACROS -D__STDC_LIMIT_MACROS -fPIC")
</code></pre>

<p><strong>CMakeLists.txt (2)</strong></p>

<pre><code>project(VectorizePass)

set(LLVM_DIR "/Users/user/julia5/deps/build/llvm-3.7.1/build_Release")
set(LLVM_TOOLS_BINARY_DIR "/Users/user/julia5/deps/build/llvm-3.7.1/build_Release/Release/bin")

include_directories(${LLVM_INCLUDE_DIRS})
include_directories("/Users/user/julia5/deps/build/llvm-3.7.1/build_Release/include")
include_directories("/Users/user/julia5/deps/srccache/llvm-3.7.1/include")
link_directories("/Users/user/julia5/deps/build/llvm-3.7.1/build_Release/Release/lib")
link_directories("/Users/user/julia5/deps/build/llvm-3.7.1/build_Release/lib")
link_directories(${LLVM_LINK_DIRS})
add_definitions(${LLVM_DEFINITIONS})


add_subdirectory(VectorizePass)
</code></pre>

<p>Clearly, for some reason CMake isn't finding the appropriate object files even though they <em>are</em> in the directories that I have in the <code>link_directories</code> statements. What am I missing? I'm fairly new to CMake so it may be something obvious. </p>

<p>I've attempted to <code>include(AddLLVM)</code> as suggested <a href="https://stackoverflow.com/questions/27863706/llvm-out-of-source-pass-build-loadable-modules-not-supported-on-linux">here</a> but CMake reports that it cannot find <code>AddLLVM</code>. <a href="https://stackoverflow.com/questions/33193757/issues-running-a-simple-out-of-source-llvm-pass">This</a> Stack post also suggests using a regular Makefile but that does not work when compiling out-of-source passes as all the paths in the regular LLVM <code>Makefile.common</code>/<code>Makefile.config</code> are relative and don't work at all. </p>