Tuesday, November 16, 2021

[SOLVED] Issue: C compiler used for C++ in CMake | CMake + git

Issue

I've been using CMake to build my next C++ project, and I had to link a library named 'GLFW' to my executable. I've been having strange issues with this:

Here is my project structure

.

GLFW is cloned into the 'dep' directory. The 'build' directory is my CMake build directory. The 'src' directory is a subdirectory to my top-level CMakeLists.txt and contains the source code.

Top-level CMakeLists.txt:

cmake_minimum_required(VERSION 3.20.1)
set(CMAKE_CXX_STANDARD 20)
project(OpenGL1)
add_subdirectory(dep/glfw)
add_subdirectory(src)
target_link_libraries(${PROJECT_NAME} glfw)

src/CMakeLists.txt:

add_executable(${PROJECT_NAME} main.cpp)

src/main.cpp:

#include <iostream>
int main() 
{
    std::cout<<"Hello, world!\n";
}

I configured my project like this (command ran from the root directory) cmake -G "Ninja" -S . -B build

The configuration works well, however the building does not: cmake --build build This causes a long list of errors:

cmake --build build
[1/127] Building C object dep/glfw/src/CMakeFiles/glfw.dir/monitor.c.obj
FAILED: dep/glfw/src/CMakeFiles/glfw.dir/monitor.c.obj
C:\dev\LLVM\bin\clang.exe -D_GLFW_USE_CONFIG_H -D_UNICODE -I../dep/glfw/include -I../dep/glfw/src -Idep/glfw/src -g -Xclang -gcodeview -O0 -D_DEBUG -D_DLL -D_MT -Xclang --dependent-lib=msvcrtd /clang:-Wall -MD -MT dep/glfw/src/CMakeFiles/glfw.dir/monitor.c.obj -MF dep\glfw\src\CMakeFiles\glfw.dir\monitor.c.obj.d -o dep/glfw/src/CMakeFiles/glfw.dir/monitor.c.obj -c ../dep/glfw/src/monitor.c
clang: error: no such file or directory: '/clang:-Wall'
[2/127] Building C object dep/glfw/src/CMakeFiles/glfw.dir/window.c.obj
FAILED: dep/glfw/src/CMakeFiles/glfw.dir/window.c.obj
C:\dev\LLVM\bin\clang.exe -D_GLFW_USE_CONFIG_H -D_UNICODE -I../dep/glfw/include -I../dep/glfw/src -Idep/glfw/src -g -Xclang -gcodeview -O0 -D_DEBUG -D_DLL -D_MT -Xclang --dependent-lib=msvcrtd /clang:-Wall -MD -MT dep/glfw/src/CMakeFiles/glfw.dir/window.c.obj -MF dep\glfw\src\CMakeFiles\glfw.dir\window.c.obj.d -o dep/glfw/src/CMakeFiles/glfw.dir/window.c.obj -c ../dep/glfw/src/window.c
clang: error: no such file or directory: '/clang:-Wall'
[3/127] Building C object dep/glfw/src/CMakeFiles/glfw.dir/context.c.obj
FAILED: dep/glfw/src/CMakeFiles/glfw.dir/context.c.obj
C:\dev\LLVM\bin\clang.exe -D_GLFW_USE_CONFIG_H -D_UNICODE -I../dep/glfw/include -I../dep/glfw/src -Idep/glfw/src -g -Xclang -gcodeview -O0 -D_DEBUG -D_DLL -D_MT -Xclang --dependent-lib=msvcrtd /clang:-Wall -MD -MT dep/glfw/src/CMakeFiles/glfw.dir/context.c.obj -MF dep\glfw\src\CMakeFiles\glfw.dir\context.c.obj.d -o dep/glfw/src/CMakeFiles/glfw.dir/context.c.obj -c ../dep/glfw/src/context.c
clang: error: no such file or directory: '/clang:-Wall'
[4/127] Building C object dep/glfw/src/CMakeFiles/glfw.dir/init.c.obj
FAILED: dep/glfw/src/CMakeFiles/glfw.dir/init.c.obj
C:\dev\LLVM\bin\clang.exe -D_GLFW_USE_CONFIG_H -D_UNICODE -I../dep/glfw/include -I../dep/glfw/src -Idep/glfw/src -g -Xclang -gcodeview -O0 -D_DEBUG -D_DLL -D_MT -Xclang --dependent-lib=msvcrtd /clang:-Wall -MD -MT dep/glfw/src/CMakeFiles/glfw.dir/init.c.obj -MF dep\glfw\src\CMakeFiles\glfw.dir\init.c.obj.d -o dep/glfw/src/CMakeFiles/glfw.dir/init.c.obj -c ../dep/glfw/src/init.c
clang: error: no such file or directory: '/clang:-Wall'
[5/127] Building C object dep/glfw/src/CMakeFiles/glfw.dir/input.c.obj
FAILED: dep/glfw/src/CMakeFiles/glfw.dir/input.c.obj
C:\dev\LLVM\bin\clang.exe -D_GLFW_USE_CONFIG_H -D_UNICODE -I../dep/glfw/include -I../dep/glfw/src -Idep/glfw/src -g -Xclang -gcodeview -O0 -D_DEBUG -D_DLL -D_MT -Xclang --dependent-lib=msvcrtd /clang:-Wall -MD -MT dep/glfw/src/CMakeFiles/glfw.dir/input.c.obj -MF dep\glfw\src\CMakeFiles\glfw.dir\input.c.obj.d -o dep/glfw/src/CMakeFiles/glfw.dir/input.c.obj -c ../dep/glfw/src/input.c
clang: error: no such file or directory: '/clang:-Wall'
[6/127] Building C object dep/glfw/src/CMakeFiles/glfw.dir/vulkan.c.obj
FAILED: dep/glfw/src/CMakeFiles/glfw.dir/vulkan.c.obj
C:\dev\LLVM\bin\clang.exe -D_GLFW_USE_CONFIG_H -D_UNICODE -I../dep/glfw/include -I../dep/glfw/src -Idep/glfw/src -g -Xclang -gcodeview -O0 -D_DEBUG -D_DLL -D_MT -Xclang --dependent-lib=msvcrtd /clang:-Wall -MD -MT dep/glfw/src/CMakeFiles/glfw.dir/vulkan.c.obj -MF dep\glfw\src\CMakeFiles\glfw.dir\vulkan.c.obj.d -o dep/glfw/src/CMakeFiles/glfw.dir/vulkan.c.obj -c ../dep/glfw/src/vulkan.c
clang: error: no such file or directory: '/clang:-Wall'
ninja: build stopped: subcommand failed.

I do not know if this is an issue with glfw's CMakeLists.txt or with my system, however when I remove the functions that rely on GLFW from my top-level CMakeLists.txt, there are no errors.

Note that I have tried using a git submodule instead, but have been getting the same error, so I used this way since it is more straight-forward.

I'm on windows and I'm using Ninja, I'm not using a git repo, the project is just some files on my PC. How can I fix these errors? Thanks in advance :) Feel free to ask for the contents of other files - there's just too many files to put them all here.


Solution

Your project is fine. You just happened to run into a bug within glfw: https://github.com/glfw/glfw/issues/1807

It's been fixed already, but not released yet. So your best options are to clone the master branch, or to manually apply whichever fix resolved the issue in your version of the source until 3.3.5 gets released.



Answered By - Frank