在 Windows 上构建 LLVM 示例时缺少 CoTaskMemFree

Missing CoTaskMemFree when building LLVM example on Windows

我试图从根本上遵循 llvm Kaleidoscope example

我在 Windows。我按照网站上的说明从源代码构建了 llvm。花了好长时间终于搭建成功了(至少没有报错)

然后用我自己的代码运行宁这个命令:

$  clang-cl main.obj llvm/lib/LLVMCore.lib llvm/lib/LLVMSupport.lib /MDd -o build\test.exe

我的 main.cpp 代码中有这个:

#include "llvm/ADT/STLExtras.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Verifier.h"

std::unique_ptr<Module> module = llvm::make_unique<Module>("my cool jit", getGlobalContext());

我没有收到任何解析器错误,但我收到 linker 的错误,我这辈子都弄不明白:

LLVMSupport.lib(Path.obj) : error LNK2019: unresolved external symbol
__imp_CoTaskMemFree referenced in function "bool __cdecl 
llvm::sys::path::getKnownFolderPath(struct _GUID,class 
llvm::SmallVectorImpl<char> &)" (?
getKnownFolderPath@path@sys@llvm@@YA_NU_GUID@@AEAV?$SmallVectorImpl@D@3@@Z)

build\test.exe : fatal error LNK1120: 1 unresolved externals

我必须 link 到哪个库才能定义此函数?我可以在我构建的代码中看到实现。我是否需要以特定方式构建 llvm 才能将其导出?

编辑:

事实证明我需要更好地阅读 the clang-cl documentation 的内容:

To enable clang-cl to find system headers, libraries, and the linker when run from the command-line, it should be executed inside a Visual Studio Native Tools Command Prompt or a regular Command Prompt where the environment has been set up using e.g. vcvars32.bat.

事实证明,这解决了我的问题。我有点困惑,因为 clang-cl 似乎自动解析 sdk 包含和工具路径,但不是 lib 路径。我也不想使用 CMD 来驱动 clang,所以我使用 bash 我不能轻易 运行 vcvar32.bat 的地方。我通过基本上复制 vcvar32.bat$PATH$INCLUDE$LIB$LIBPATH 环境变量所做的事情并添加 Ole32.Lib 作为解决了我的问题clang-cl 的参数。然后它就像一个魅力。

您缺少 CoTaskMemFree 符号。快速浏览 Internet 表明您需要在 link 行上使用 Ole32 系统库。

我无法访问 Windows 机器进行测试,但在我的计算机上,我可以 运行 llvm-config --system-libs 并且它会提取所有必要的东西。添加 using namespace llvm; 并添加存根 main 函数后,我可以使用 (on OSX):

轻松构建此示例
c++ `llvm-config --cxxflags` main.cpp `llvm-config --ldflags --system-libs --libs core support`

我经常建议只指定 --libs 而不是猜测你需要什么,而是你的选择。