Justin
Justin

Reputation: 742

How do you compile C++ programs that include LLVM API headers?

I'm trying to use the C++ compiler to compile the following program:

#include <stdio.h>
#include "llvm/IR/LLVMContext.h"
#include "llvm/Support/SourceMgr.h"
#include "llvm/IR/Module.h"

int main( int argc, char* argv[] )
{
   if( argc < 2 )
      llvm::errs() << "Expected an argument - IR file name\n";

   llvm::LLVMContext &context = llvm::getGlobalContext();
   llvm::SMDiagnostic err;
   llvm::Module* module = llvm::ParseIRFile( argv[1], err, context );

   if( !mod )
   {
      err.print( argv[0], errs() );
      return 1;
   }

   return 0;
}

I'm trying to compile the program using the following command:

clang++ main.cpp -o main

However, when I compile, I'm getting the following compile error:

main.cpp:2:10: fatal error: 'llvm/IR/LLVMContext.h' file not found
#include "llvm/IR/LLVMContext.h"
         ^
1 error generated.

In this case, I'm not exactly sure how to link the LLVM API headers when compiling main.cpp with Clang.

Any help would be greatly appreciated.

Upvotes: 4

Views: 7323

Answers (2)

Jack
Jack

Reputation: 21

You can use the following command: g++ -std=c++11 main.cpp `llvm-config --system-libs --cppflags --ldflags --libs core` -o main

Where --libs and --system-libs flags are used for linking and --cppflags takes care of include paths.

Thank You

Upvotes: 2

Eli Bendersky
Eli Bendersky

Reputation: 273416

You need LLVM checked out or installed somewhere on your system. You can download a binary release (with headers and libraries you can build against) as explained here: http://llvm.org/releases/download.html#3.5

You can also check out LLVM from its SVN repository as explained here: http://llvm.org/docs/GettingStarted.html#checkout

Once you do that, I recommend looking at the llvm-clang-samples repository that comes with a Makefiles showing how to build sample programs vs. an up-to-date LLVM.

Upvotes: 1

Related Questions