Linker cannot open input file 'windows.obj' (Why is it even looking?)

I’m trying to get Clang working with Code::Blocks.

I’ve installed CodeBlocks 20.03 with MinGW, LLVM 14.0.0-win64, Visual Studio 2022, and the latest WinSDK. All are in sub-folders of E:\Compilers\

My toolchain executables are:

  • C Compiler: clang.exe
  • C++ Compiler: clang++.exe
  • Linker for dynamic libs: clang++.exe
  • Linker for static libs: llvm-ar.exe
  • Debugger: GDB/CDB debugger: Default
  • Resource compiler: llvm-rc.exe
  • Make program: mingw32-make.exe

Search directories for the complier has a path to the Visual Studio’s include directory.

When I try building a basic console-only “Hello World” program, it builds successfully. But when I try building a basic Windows application (one that just instantiates a window with a window proc) it errors with the following build output:

-------------- Build: Debug in TestWinClang (compiler: LLVM Clang Compiler)---------------

clang++.exe -Weverything -m64 -g -v -fomit-frame-pointer -Wnon-virtual-dtor -Wbind-to-temporary-copy -Wextra-tokens -std=c++17 -IE:\Compilers\VisualStudio\VC\Tools\MSVC\14.31.31103\include -c E:\Development\TestWinClang\main.cpp -o obj\Debug\main.o
clang++.exe  -o bin\Debug\TestWinClang.exe obj\Debug\main.o  -m64  -lgdi32 -luser32 -lkernel32 -lcomctl32 -Wl,--subsystem,windows
clang version 14.0.0
Target: x86_64-pc-windows-msvc
Thread model: posix
InstalledDir: E:\Compilers\LLVM\bin
 (in-process)
 "E:\\Compilers\\LLVM\\bin\\clang++.exe" -cc1 -triple x86_64-pc-windows-msvc19.31.31107 -emit-obj -mrelax-all -mincremental-linker-compatible --mrelax-relocations -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name main.cpp -mrelocation-model pic -pic-level 2 -mframe-pointer=none -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -mllvm -treat-scalable-fixed-error-as-warning -gno-column-info -gcodeview -debug-info-kind=constructor -v "-fcoverage-compilation-dir=E:\\Development\\TestWinClang" -resource-dir "E:\\Compilers\\LLVM\\lib\\clang\\14.0.0" -I "E:\\Compilers\\VisualStudio\\VC\\Tools\\MSVC\\14.31.31103\\include" -internal-isystem "E:\\Compilers\\LLVM\\lib\\clang\\14.0.0\\include" -internal-isystem "E:\\Compilers\\VisualStudio\\VC\\Tools\\MSVC\\14.31.31103\\include" -internal-isystem "E:\\Compilers\\VisualStudio\\VC\\Tools\\MSVC\\14.31.31103\\atlmfc\\include" -internal-isystem "E:\\Compilers\\WinSDK\\Include\\10.0.22000.0\\ucrt" -internal-isystem "E:\\Compilers\\WinSDK\\Include\\10.0.22000.0\\shared" -internal-isystem "E:\\Compilers\\WinSDK\\Include\\10.0.22000.0\\um" -internal-isystem "E:\\Compilers\\WinSDK\\Include\\10.0.22000.0\\winrt" -internal-isystem "E:\\Compilers\\WinSDK\\Include\\10.0.22000.0\\cppwinrt" -Weverything -Wnon-virtual-dtor -Wbind-to-temporary-copy -Wextra-tokens -std=c++17 -fdeprecated-macro "-fdebug-compilation-dir=E:\\Development\\TestWinClang" -ferror-limit 19 -fno-use-cxa-atexit -fms-extensions -fms-compatibility -fms-compatibility-version=19.31.31107 -fdelayed-template-parsing -fcxx-exceptions -fexceptions -faddrsig -o "obj\\Debug\\main.o" -x c++ "E:\\Development\\TestWinClang\\main.cpp"
clang -cc1 version 14.0.0 based upon LLVM 14.0.0 default target x86_64-pc-windows-msvc
ignoring duplicate directory "E:\Compilers\VisualStudio\VC\Tools\MSVC\14.31.31103\include"
  as it is a non-system directory that duplicates a system directory
#include "..." search starts here:
#include <...> search starts here:
 E:\Compilers\LLVM\lib\clang\14.0.0\include
 E:\Compilers\VisualStudio\VC\Tools\MSVC\14.31.31103\include
 E:\Compilers\VisualStudio\VC\Tools\MSVC\14.31.31103\atlmfc\include
 E:\Compilers\WinSDK\Include\10.0.22000.0\ucrt
 E:\Compilers\WinSDK\Include\10.0.22000.0\shared
 E:\Compilers\WinSDK\Include\10.0.22000.0\um
 E:\Compilers\WinSDK\Include\10.0.22000.0\winrt
 E:\Compilers\WinSDK\Include\10.0.22000.0\cppwinrt
End of search list.
E:\Development\TestWinClang\main.cpp:8:10: warning: non-portable path to file '<Windows.h>'; specified path differs in case from file name on disk [-Wnonportable-system-include-path]
#include <windows.h>
         ^~~~~~~~~~~
         <Windows.h>
E:\Development\TestWinClang\main.cpp:14:7: warning: no previous extern declaration for non-static variable 'szClassName' [-Wmissing-variable-declarations]
TCHAR szClassName[ ] = _T("CodeBlocksWindowsApp");
      ^
E:\Development\TestWinClang\main.cpp:14:1: note: declare 'static' if the variable is not intended to be used outside of this translation unit
TCHAR szClassName[ ] = _T("CodeBlocksWindowsApp");
^
E:\Development\TestWinClang\main.cpp:33:29: warning: zero as null pointer constant [-Wzero-as-null-pointer-constant]
    wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
                            ^~~~
                            nullptr
E:\Compilers\LLVM\lib\clang\14.0.0\include\stddef.h:86:18: note: expanded from macro 'NULL'
#    define NULL 0
                 ^
E:\Development\TestWinClang\main.cpp:34:31: warning: zero as null pointer constant [-Wzero-as-null-pointer-constant]
    wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
                              ^~~~
                              nullptr
E:\Compilers\LLVM\lib\clang\14.0.0\include\stddef.h:86:18: note: expanded from macro 'NULL'
#    define NULL 0
                 ^
E:\Development\TestWinClang\main.cpp:35:33: warning: zero as null pointer constant [-Wzero-as-null-pointer-constant]
    wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
                                ^~~~
                                nullptr
E:\Compilers\LLVM\lib\clang\14.0.0\include\stddef.h:86:18: note: expanded from macro 'NULL'
#    define NULL 0
                 ^
E:\Development\TestWinClang\main.cpp:36:26: warning: zero as null pointer constant [-Wzero-as-null-pointer-constant]
    wincl.lpszMenuName = NULL;                 /* No menu */
                         ^~~~
                         nullptr
E:\Compilers\LLVM\lib\clang\14.0.0\include\stddef.h:86:18: note: expanded from macro 'NULL'
#    define NULL 0
                 ^
E:\Development\TestWinClang\main.cpp:40:27: warning: use of old-style cast [-Wold-style-cast]
    wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
                          ^        ~~~~~~~~~~~~~~~~
E:\Development\TestWinClang\main.cpp:57:12: warning: zero as null pointer constant [-Wzero-as-null-pointer-constant]
           NULL,                /* No menu */
           ^~~~
           nullptr
E:\Compilers\LLVM\lib\clang\14.0.0\include\stddef.h:86:18: note: expanded from macro 'NULL'
#    define NULL 0
                 ^
E:\Development\TestWinClang\main.cpp:59:12: warning: zero as null pointer constant [-Wzero-as-null-pointer-constant]
           NULL                 /* No Window Creation data */
           ^~~~
           nullptr
E:\Compilers\LLVM\lib\clang\14.0.0\include\stddef.h:86:18: note: expanded from macro 'NULL'
#    define NULL 0
                 ^
E:\Development\TestWinClang\main.cpp:66:35: warning: zero as null pointer constant [-Wzero-as-null-pointer-constant]
    while (GetMessage (&messages, NULL, 0, 0))
                                  ^~~~
                                  nullptr
E:\Compilers\LLVM\lib\clang\14.0.0\include\stddef.h:86:18: note: expanded from macro 'NULL'
#    define NULL 0
                 ^
E:\Development\TestWinClang\main.cpp:17:32: warning: unused parameter 'hPrevInstance' [-Wunused-parameter]
                     HINSTANCE hPrevInstance,
                               ^
E:\Development\TestWinClang\main.cpp:18:28: warning: unused parameter 'lpszArgument' [-Wunused-parameter]
                     LPSTR lpszArgument,
                           ^
E:\Development\TestWinClang\main.cpp:75:21: warning: implicit conversion loses integer precision: 'WPARAM' (aka 'unsigned long long') to 'int' [-Wshorten-64-to-32]
    return messages.wParam;
    ~~~~~~ ~~~~~~~~~^~~~~~
13 warnings generated.
clang++: error: linker command failed with exit code 1181 (use -v to see invocation)
LINK : warning LNK4044: unrecognized option '/-subsystem'; ignored
LINK : fatal error LNK1181: cannot open input file 'windows.obj'
Process terminated with status 1181 (0 minute(s), 3 second(s))
1 error(s), 13 warning(s) (0 minute(s), 3 second(s))

Why is it even looking for a windows.obj? There is no windows.cpp. Any help getting this working would be appreciated.

The cpp source code for the project is:

#include <tchar.h>
#include <windows.h>

/*  Declare Windows procedure  */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);

/*  Make the class name into a global variable  */
TCHAR szClassName[ ] = _T("CodeBlocksWindowsApp");

int WINAPI WinMain (HINSTANCE hThisInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR lpszArgument,
                     int nCmdShow)
{
    HWND hwnd;               /* This is the handle for our window */
    MSG messages;            /* Here messages to the application are saved */
    WNDCLASSEX wincl;        /* Data structure for the windowclass */

    /* The Window structure */
    wincl.hInstance = hThisInstance;
    wincl.lpszClassName = szClassName;
    wincl.lpfnWndProc = WindowProcedure;      /* This function is called by windows */
    wincl.style = CS_DBLCLKS;                 /* Catch double-clicks */
    wincl.cbSize = sizeof (WNDCLASSEX);

    /* Use default icon and mouse-pointer */
    wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
    wincl.lpszMenuName = NULL;                 /* No menu */
    wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */
    wincl.cbWndExtra = 0;                      /* structure or the window instance */
    /* Use Windows's default colour as the background of the window */
    wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;

    /* Register the window class, and if it fails quit the program */
    if (!RegisterClassEx (&wincl))
        return 0;

    /* The class is registered, let's create the program*/
    hwnd = CreateWindowEx (
           0,                   /* Extended possibilites for variation */
           szClassName,         /* Classname */
           _T("Code::Blocks Template Windows App"),       /* Title Text */
           WS_OVERLAPPEDWINDOW, /* default window */
           CW_USEDEFAULT,       /* Windows decides the position */
           CW_USEDEFAULT,       /* where the window ends up on the screen */
           544,                 /* The programs width */
           375,                 /* and height in pixels */
           HWND_DESKTOP,        /* The window is a child-window to desktop */
           NULL,                /* No menu */
           hThisInstance,       /* Program Instance handler */
           NULL                 /* No Window Creation data */
           );

    /* Make the window visible on the screen */
    ShowWindow (hwnd, nCmdShow);

    /* Run the message loop. It will run until GetMessage() returns 0 */
    while (GetMessage (&messages, NULL, 0, 0))
    {
        /* Translate virtual-key messages into character messages */
        TranslateMessage(&messages);
        /* Send message to WindowProcedure */
        DispatchMessage(&messages);
    }

    /* The program return-value is 0 - The value that PostQuitMessage() gave */
    return messages.wParam;
}


/*  This function is called by the Windows function DispatchMessage()  */

LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)                  /* handle the messages */
    {
        case WM_DESTROY:
            PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
            break;
        default:                      /* for messages that we don't deal with */
            return DefWindowProc (hwnd, message, wParam, lParam);
    }

    return 0;
}

You probably want to use clang-cl instead of clang++ when building on windows. That will have better defaults for that platform.

Thanks for the help.

The documentation for clang-cl.exe says,

clang-cl is an alternative command-line interface to Clang, designed for compatibility with the Visual C++ compiler, cl.exe.

Which makes it sound like it’s only used as a follow-on to VC++ compilation.

When I switched to clang-cl, I initially got errors that it didn’t recognize the symbol -lgdi32 etc. So I went into “Advanced Options”, removed the -l prefix for libraries and required the .lib extension, and added a linker search directory into …\WinSDK\Lib\10.0.22000.0\um\x64.

Now, the command line looks like this:

clang++.exe -Weverything -m64 -g -v -m64 -fomit-frame-pointer -Wnon-virtual-dtor -Wbind-to-temporary-copy -Wextra-tokens -std=c++17 -IE:\Compilers\VisualStudio\VC\Tools\MSVC\14.31.31103\include -c E:\Development\TestWinClang\main.cpp -o obj\Debug\main.o
clang-cl.exe  -o bin\Debug\TestWinClang.exe obj\Debug\main.o  -m64 -m64  gdi32.lib user32.lib kernel32.lib -Wl,--subsystem,windows

and I’m getting this linker error:

LINK : error LNK2001: unresolved external symbol WinMainCRTStartup
bin\Debug\TestWinClang.exe : fatal error LNK1120: 1 unresolved externals
clang-cl: error: linker command failed with exit code 1120 (use -v to see invocation)

Where do I go from here?