Can't select target for triple

Hi everyone,
I’m trying to link LLVM to my application. When I install llvm on ubuntu everything works as it should. However, it doesn’t work when I compile it myself. It gives an error is Error = “Unable to find target for this triple (no targets are registered)” after checked “targets().begin() == targets().end()”. I also checked the triple data and it is normal (x86_64-unknown-linux-gnu). And the cmake command to build llvm is following;
cmake -G Ninja -DCMAKE_BUILD_TYPE=debug -DLLVM_BUILD_LLVM_DYLIB=true -DLLVM_TARGETS_TO_BUILD=“host” -DLLVM_PARALLEL_LINK_JOBS=1 …/llvm

What is the difference between these two cases?

You could try instead:

-DLLVM_TARGETS_TO_BUILD="X86" 

I honestly do not know whether host is valid.

Indeed, host is not a valid value:
https://llvm.org/docs/GettingStarted.html#local-llvm-configuration

I tried target=X86 and the result is same :frowning:

-DLLVM_TARGETS_TO_BUILD="host" definitely works:

What gives this error?

In Support/TargetRegistry.cpp, line is 65, version llvm-12.
It can’t pass the if (targets().begin() == targets().end()) { check.

const Target *TargetRegistry::lookupTarget(const std::string &TT,
                                           std::string &Error) {
  // Provide special warning when no targets are initialized.
  if (targets().begin() == targets().end()) {
    Error = "Unable to find target for this triple (no targets are registered)";
    return nullptr;
  }
  Triple::ArchType Arch = Triple(TT).getArch();
  auto ArchMatch = [&](const Target &T) { return T.ArchMatchFn(Arch); };
  auto I = find_if(targets(), ArchMatch);

  if (I == targets().end()) {
    Error = "No available targets are compatible with triple \"" + TT + "\"";
    return nullptr;
  }

  auto J = std::find_if(std::next(I), targets().end(), ArchMatch);
  if (J != targets().end()) {
    Error = std::string("Cannot choose between targets \"") + I->Name +
            "\" and \"" + J->Name + "\"";
    return nullptr;
  }

  return &*I;
}

Sorry, I meant: what are you doing that gives that error?

What is “everything” here?

What is “it” here? Compiling your program against LLVM? Linking LLVM into your program? Executing your program against some input? Compiling something else to use in your program?

What is your program and how it links to LLVM?

Nothing that you said so far relates to target triple handling. It’s quite possible that you just haven’t registered any target (ex. llvm::InitializeAllTargets()) in your application, and then TargetRegistry can’t find any to compare to.

I’m linking LLVM into my program with so (giving LD_LIBRARY_PATH). After linking my compiled llvm, I open the source code with VSCode and set the breakpoint at the line I mentioned above to get the result. The result is that there is no registered target. By the way, I don’t change my application and not set the target in two cases. The first case is my compiled llvm and the second is system llvm. If it runs with system llvm, everything is normal, and could find the target. I’m wondering how it could find the target while running with system llvm? And why could not find it with my compiled llvm?

Do you initialize all targets in your program, via llvm::InitializeAllTargets()?

If not, then it should not have any targets initialized and that assert would hit. I can’t think of another reason why that assert would hit, to be honest, as the error message clearly states.

System LLVM is compiled by the OS vendor / distribution, and each have different flags, include different projects, etc. LLVM isn’t a single library or a single program, but a very complicated compiler infrastructure, used by compilers, not end users.

If you want to build your own LLVM for your project, you need to understand how to build and use everything that you need.