Linking to LLVM libs from new project

Hi all,

I have a new sample project set up in my llvm/projects directory, and
I'd eventually like my frontend to generate an LLVM IR tree. To do so,
I believe I need to link to the appropriate LLVM libraries. That's
where I'm stuck.

I can't seem to find any specific documentation that describes how to
set up a new project to link to LLVM libraries.

To get started, I added a couple lines to the tools/sample/main.cpp
file in my project:

#include "llvm/Module.h"
...
// Inside main
Module* module = new Module("myfirst");

Not much, but I just want to verify that I can successfully compile
and link using an LLVM IR class.

Presumably, I need to make some additions to configure.ac and the
tools/sample/Makefile, but I can't figure out what those changes
should be. Since the linker doesn't know how to get the library, I get
an error now when I run make:

$ make
llvm[2]: Compiling sample.c for Release build
llvm[2]: Building Release Archive Library libsample.a
llvm[2]: Compiling calc.cpp for Release build
llvm[2]: Building Release Archive Library libcalc.a
llvm[2]: Compiling main.cpp for Release build
main.cpp: In function 'int main(int, char**)':
main.cpp:16: warning: unused variable 'module'
llvm[2]: Linking Release executable Sample (without symbols)
Undefined symbols:
  "llvm::Module::Module(std::basic_string<char,
std::char_traits<char>, std::allocator<char> > const&)", referenced
from:
      _main in main.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
make[2]: *** [/opt/src-llvm/projects/myproject/Release/bin/Sample] Error 1
make[1]: *** [all] Error 1
make: *** [all] Error 1

Since there is no configure.ac file or Makefile listed with the
Kaleidoscope tutorial (I even looked around the svn repository for
one...), I took a look at what Stacker uses, and took the following
from the Stacker configure.ac file:

dnl **************************************************************************
dnl * Set the location of various third-party software packages
dnl **************************************************************************

dnl Location of LLVM source code
AC_ARG_WITH(llvmsrc,AS_HELP_STRING([--with-llvmsrc],[Location of LLVM
Source Code]),AC_SUBST(LLVM_SRC,[$withval]),AC_SUBST(LLVM_SRC,[`cd
${srcdir}/../..; pwd`]))
AC_CONFIG_COMMANDS([llvm_src],[],[llvm_src=$LLVM_SRC])

dnl Location of LLVM object code
AC_ARG_WITH(llvmobj,AS_HELP_STRING([--with-llvmobj],[Location of LLVM
Object Code]),AC_SUBST(LLVM_OBJ,[$withval]),AC_SUBST(LLVM_OBJ,[`cd
../..; pwd`]))
AC_CONFIG_COMMANDS([llvm_obj],[],[llvm_obj=$LLVM_OBJ])

Since I'm trying to use the Module class, I also tried adding this
line to tools/sample/Makefile:

LINK_COMPONENTS = llvmcore

After making those changes, the linking still fails.

$ make
llvm[2]: Compiling sample.c for Release build
llvm[2]: Building Release Archive Library libsample.a
llvm[2]: Compiling calc.cpp for Release build
llvm[2]: Building Release Archive Library libcalc.a
llvm-config: unknown component name: llvmcore
llvm-config: unknown component name: llvmcore
llvm[2]: Compiling main.cpp for Release build
main.cpp: In function 'int main(int, char**)':
main.cpp:16: warning: unused variable 'module'
llvm[2]: Linking Release executable Sample (without symbols)
Undefined symbols:
  "llvm::Module::Module(std::basic_string<char,
std::char_traits<char>, std::allocator<char> > const&)", referenced
from:
      _main in main.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
make[2]: *** [/opt/src-llvm/projects/myproject/Release/bin/Sample] Error 1
make[1]: *** [all] Error 1
make: *** [all] Error 1

Any tips or pointers would be greatly appreciated. It seems that
linking to the LLVM libraries is a pretty common thing that someone
starting a new project might want to do. :slight_smile: I'd love to figure out
how.

Cheers,
Lane

Er... so to answer my own question, for posterity, to set up a new
project (based on the sample project) to link to the core LLVM
library, all you need to do is add the following line to the relevant
make file:

LINK_COMPONENTS = core

So for my example below, I added the above line to tools/sample/Makefile.

Perhaps a mention of this could be added to the "Creating an LLVM Project" page?

Thanks,
Lane