LLVM Discussion Forums

Clang plugins on Windows

I am trying to build clang plugins on windows. I have a large cross platform (Linux/Windows) project and I am in the works of modernizing it. The project heavily relies on custom in-house build tools that analyse c++ code and generate some more. I was thinking that clang (as a compiler) supports both platforms and allows you to build plugins for it, so I would move the project from using platform specific compilers and custom build tools to a full clang toolchain. I would like to use clang for both compiling the project and rewrite the build tools as clang plugins. Seemed like a great idea.

  • I have cloned the llvm-project from github (llvm 12).
  • Then I have generated Visual Studio project files for the llvm-project with this cmake command
    cmake -G "Visual Studio 16" -DLLVM_ENABLE_PROJECTS="clang;lld" -DLLVM_EXPORT_SYMBOLS_FOR_PLUGINS=ON -DLLVM_TARGETS_TO_BUILD=X86 -DCLANG_BUILD_EXAMPLES=ON -DCMAKE_INSTALL_PREFIX=install ...\llvm-project\llvm
  • Then I have built the solution using the MSVC compiler, in release configuration for x64 architecture. The project compiles successfully. Both clang, lld and the example clang plugin, PrintFunctionNames have successfully been built.
  • Now it was time to invoke and test the example plugin. I have tried both ways to use the plugin:
clang -cc1 -load PrintFunctionNames.dll -plugin print-fns test.cpp
clang -c -Xclang -load -Xclang PrintFunctionNames.dll -Xclang -plugin -Xclang print-fns test.cpp

The error:
While these commands work, that is, the plugin is loaded and it runs as expected, using clang++ in the command results in a error: unable to find plugin 'print-fns'. Moreover, using the plugin and building a executable (removing the -c option) gives a linker error (1136) invalid or corrupt file.

I have tried to replicate the PrintFunctionNames plugin out of tree as well, following this github repository. While I have managed to build it out of tree, the errors are the same: it only works with clang and not clang++ and no binaries can be built while using the plugin due to the link error.

Am I doing something wrong? What could possibly be the reason behind the different results from using clang/clang++? I always thought that they are the same driver, just they have different presets for linking with the c/c++ stdlib. Does anyone have any hints on how to get plugins working? There seems to be no documentation on building plugins for Windows.

References:
export symbols option

I have also posted a question echoing my issues on stackoverflow. While I wouldn’t call what I got a “solution”, but I think I have resolved the linking errors, thus the plugin can be used while compiling sources. clang++ still refuses to work though.

Here is my stackoverflow thread