lld-link: Undefined symbol llvm::LLVMContext

Hi, I have my project set up using Visual Studio 2022. My platform toolset is set to LLVM (clang-cl) and the include path is set correctly. My code for now looks something like this:

#include <llvm/IR/IRBuilder.h>

static llvm::LLVMContext context;
static llvm::IRBuilder<> Builder();
static std::unique_ptr<llvm::Module> _module;
static std::map<std::string, llvm::Value*> namedValues;

But when I compile I get the following three errors (I use the APInt class somewhere else as well which is why one of the errors references it):

image

Are there any additional libraries that I need to link?

If this is still a problem, it would be helpful to say which libraries you already include in your link. I was able to get the posted code to link cleanly (using link.exe not lld-link) with LLVMCore.lib, LLVMSupport.lib, LLVMRemarks.lib, LLVMBinaryFormat.lib, and LLVMBitstreamReader.lib.

1 Like

Thanks for your reply, by adding the libraries you mentioned I was able to make two of the errors go away, but I still get a "undefined symbol ‘LLVMContext’ " error (LNK2001). I have tried adding every library prefixed ‘LLVM’ to the list but this did not fix the error.

Please post the entire error message, and ideally the full command line that gets the error. As I said the posted example works for me, so I’m not able to reproduce your problem at this point.

This is the full error message as shown by Visual Studio 2022

CodeGen::context refers to my LLVMContext variable which is inside my CodeGen class:

class CodeGen {
    public:
    static llvm::LLVMContext context;

Edit: It appears that removing the error wasn’t related to LLVM. One of my AST classes was statically accessing the context like this: CodeGen::context Removing this line also fixes the error.