且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在Windows上使用CMake,Ninja和Clang进行构建

更新时间:2022-12-07 11:19:23

我终于找到了一种使我喜欢的工具使用的方式。这不是完美的方法,但是它比Florians方法将系统名称设置为 Generic (我已经使用了一段时间)的效果更好。



  terminal.integrated.shell.windows: C: \\MeineProgramme\\Visual_Studio\\2017\\BuildTools\\Common7\\Tools\\LaunchDevCmd.bat 

在VS Code中启动终端后,我需要调用相应的批处理文件,该文件设置了所需的环境变量(在我的情况下为 vcvars64。蝙蝠)。可以在

  C:\MeineProgramme\Visual_Studio\2017\BuildTools\VC\Auxiliary\中找到。 \构建

导航到构建目录后,使用以下选项运行CMake

  cmake .. -G Ninja -DCMAKE_CXX_COMPILER:PATH = C:\MeineProgramme\LLVM\bin\clang-cl.exe- DCMAKE_LINKER:PATH = C:\MeineProgramme\LLVM\bin\lld-link.exe 

这鼓励CMake使用我所有已安装的LLVM工具。不仅是 clang lld (请确保使用lld-link,它支持由 / ),还有 llvm-ar llvm-ranlib 。唯一使用的MS构建工具是我暂时不使用的资源编译器。



我认为到目前为止成功。



如果您有其他疑问,请随时与我联系或在下面发表评论。


Dear fellow C++ coders,

after using the Visual Studio toolchain for building on windows for a while, I decided to give Clang 5 a shot.

I installed the LLVM 5.0.0 binaries, the Ninja build environment, the VS 2017 Tools and CMake 3.9.3. The final aim is to be able to compile C and C++ applications for Windows using VS Code with the CMake integration as "IDE" and Clang with LLD as compiler and linker.

The compilation and execution of a simple program worked perfectly fine (screenshot of the respective terminal history). Clang automatically detected the standard lib for Windows within the VS Tools directories and produced an executable output.

The next step was setting up a simple build with Ninja (screenshot of ninja.build file and terminal history). The build process worked as expected and produced a working executable, just like before.

The problems begun when I started to integrate CMake into the process. My expectation is that CMake produces a ninja build file and runs it, correct? I tried the following CMakeLists file

cmake_minimum_required(VERSION 3.9)

project(Test)

add_executable(Test main.c)

and called CMake with cmake -G Ninja. The resulting output was disappointing and I don't understand enough to figure out respectively solve the problem myself.

-- The C compiler identification is Clang 5.0.0
-- The CXX compiler identification is Clang 5.0.0
-- Check for working C compiler: C:/Meine_Programme/LLVM/bin/clang.exe
-- Check for working C compiler: C:/Meine_Programme/LLVM/bin/clang.exe -- broken
CMake Error at C:/Meine_Programme/CMake/share/cmake-3.9/Modules/CMakeTestCCompiler.cmake:51 (message):
  The C compiler "C:/Meine_Programme/LLVM/bin/clang.exe" is not able to
  compile a simple test program.

  It fails with the following output:

   Change Dir: D:/Dateien/Downloads/Test/CMakeFiles/CMakeTmp



  Run Build Command:"C:/Meine_Programme/Ninja_Build/ninja.exe" "cmTC_eeb5c"

  [1/2] Building C object CMakeFiles\cmTC_eeb5c.dir\testCCompiler.c.obj

  FAILED: CMakeFiles/cmTC_eeb5c.dir/testCCompiler.c.obj 

  C:\Meine_Programme\LLVM\bin\clang.exe /nologo /DWIN32 /D_WINDOWS /W3 /MDd
  /Zi /Ob0 /Od /RTC1 /showIncludes
  /FoCMakeFiles\cmTC_eeb5c.dir\testCCompiler.c.obj
  /FdCMakeFiles\cmTC_eeb5c.dir\ -c testCCompiler.c

  clang.exe: error: no such file or directory: '/nologo'

  clang.exe: error: no such file or directory: '/DWIN32'

  clang.exe: error: no such file or directory: '/D_WINDOWS'

  clang.exe: error: no such file or directory: '/W3'

  clang.exe: error: no such file or directory: '/MDd'

  clang.exe: error: no such file or directory: '/Zi'

  clang.exe: error: no such file or directory: '/Ob0'

  clang.exe: error: no such file or directory: '/Od'

  clang.exe: error: no such file or directory: '/RTC1'

  clang.exe: error: no such file or directory: '/showIncludes'

  clang.exe: error: no such file or directory:
  '/FoCMakeFiles\cmTC_eeb5c.dir\testCCompiler.c.obj'

  clang.exe: error: no such file or directory:
  '/FdCMakeFiles\cmTC_eeb5c.dir\'

  ninja: build stopped: subcommand failed.





  CMake will not be able to correctly generate this project.
Call Stack (most recent call first):
  CMakeLists.txt:3 (project)


-- Configuring incomplete, errors occurred!
See also "D:/Dateien/Downloads/Test/CMakeFiles/CMakeOutput.log".
See also "D:/Dateien/Downloads/Test/CMakeFiles/CMakeError.log".

I guess that the problem is related to CMake calling clang with VS style options using slash instead of preceded by minus, like clang requires.

Thanks for helping me out guys, I appreciate it :-)

Just leave me a comment if you require further information.

Answer to Florians post

I tried Florians command but omitted the path to ninja for a shorter notation and it turned out to work just fine.

cmake -E env LDFLAGS="-fuse-ld=lld"  cmake -H. -G Ninja -Bbuild -DCMAKE_C_COMPILER:PATH="C:\MeineProgramme\LLVM\bin\clang.exe" -DCMAKE_CXX_COMPILER:PATH="C:\MeineProgramme\LLVM\bin\clang++.exe" -DCMAKE_C_COMPILER_ID="Clang" -DCMAKE_CXX_COMPILER_ID="Clang" -DCMAKE_SYSTEM_NAME="Generic"

CMake produced a ninja build file.

I ran ninja all to build the executable as Test. I renamed it to Test.exe and the program executed happily. So far... success!!! But much more complicated than I expected.

I finally found a way to use my favoured tools in a way that pleases me. It's not perfect, but it works better than Florians approach with setting the system name to Generic (which I've been using for some time now)

I first set up VS Code to use the VS developers terminal as its standard terminal. I did this by adding the following line to the VS Code preferences

"terminal.integrated.shell.windows": "C:\\MeineProgramme\\Visual_Studio\\2017\\BuildTools\\Common7\\Tools\\LaunchDevCmd.bat"

After starting the terminal within VS Code I need to call the respective batch file which sets the required environment variables (in my case vcvars64.bat). These can be found in

C:\MeineProgramme\Visual_Studio\2017\BuildTools\VC\Auxiliary\Build

After navigating to my build directory I run CMake with the following options

cmake .. -G Ninja -DCMAKE_CXX_COMPILER:PATH="C:\MeineProgramme\LLVM\bin\clang-cl.exe" -DCMAKE_LINKER:PATH="C:\MeineProgramme\LLVM\bin\lld-link.exe"

this encourages CMake to use all my installed LLVM tools. Not only clang and lld (make sure to use lld-link which supports the options led by a /), but also llvm-ar and llvm-ranlib. The only MS build tool used is the resource compiler which I don't use at the moment.

So far success I think.

Don't hesitate to contact me or comment below if you got further questions.