No Targets in TargetRegistry

This is from https://stackoverflow.com/questions/48360685/no-targets-in-targetregistry

I have the following code, which should get the default llvm::Target.

auto const targetTriple = llvm::sys::getDefaultTargetTriple();
llvm_module.setTargetTriple(targetTriple);
std::string error;
auto const * target = llvm::TargetRegistry::lookupTarget(targetTriple, error);
if (target == nullptr) {
auto targets = llvm::TargetRegistry::targets();
size_t targetCount = 0;
for (auto const & _ : targets) {
++targetCount;
}
ERROR(Unknown, "llvm::TargetRegistry::lookupTarget failed for " + targetTriple + “. llvm::TargetRegistry::targets() contains " + std::to_string(targetCount) + " elements.”);
}

This code produces this error message:


llvm::TargetRegistry::lookupTarget failed for i686-pc-windows-msvc.
llvm::TargetRegistry::targets() contains 0 elements

Am I missing a step?

Not sure. But when doing this in the C-Api, you’ve to initialize/add the Targets first. It’ll not run with all buildin-targets by default.

As an example: LLVMInitializeX86Target

This is for the C-API, so i think similar things apply to the C++ API the C-API is based on.

Hi, have you resolved this issue? I’m running into the same problem as you…

Quick question about this, these functions are not in the included header files, so how can I access them from out-of-tree code?