Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How clang works on Windows?


I'm wondering How clang works on windows system.

I followed tutorial on this website and succeeded building binary.

here's my question.

When I build project as Debug Mode, it makes .exe, .pdb, .ilk.
Debugging also works fine too but when I step in some function (ex: std::vector<>::push_back), It finds vector source file on VisualStudio's.

I think, It should've not find source file because implementation of push_back should be belong to libclang.

Why this happening?

Testing Code is below

    auto answer = [](int n)
    {
        return 32 + n;
    };

    constexpr int response = answer(10);

and build command is

C:/Program Files/LLVM/bin/clang++.exe' -std=c++2a ./example_1.cpp -o example_1.exe --debug

FYI My system has VisualStudio 2015, and LLVM 9.0. vs code configuration is

    /* c_cpp_properties.json */
{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**",
                "C:/Program Files/LLVM/include/llvm-c",
                "C:/Program Files/LLVM/include/clang-c"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "windowsSdkVersion": "8.1",
            "compilerPath": "\"C:/Program Files/LLVM/bin/clang++.exe\"",
            "cStandard": "c11",
            "cppStandard": "c++20",
            "intelliSenseMode": "clang-x64"
        }
    ],
    "version": 4
}


    /* launch.json */
{
    "version": "0.2.0",
    "configurations": [
      {
        "name": "(Windows) Launch",
        "type": "cppvsdbg",
        "request": "launch",
        "program": "${workspaceFolder}/example_1.exe",  
        "args": [],
        "stopAtEntry": false,
        "cwd": "${workspaceFolder}",
        "environment": [],
        "externalConsole": true
      }
    ]
  }
}

    /* tasks.json */
{
    // See https://go.microsoft.com/fwlink/?LinkId=733558 
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "type": "shell",
            "label": "clang++.exe build active file",
            "command": "C:/Program Files/LLVM/bin/clang++.exe",
            "args": [
                "-std=c++2a",
                "./example_1.cpp",
                "-o",
                "example_1.exe",
                "--debug"
            ],
            "group": "build"
        }
    ]
}
like image 817
JayMuzie Avatar asked Jan 26 '23 16:01

JayMuzie


2 Answers

Clang is just a compiler, and a flexible one at that. The LLVM umbrella project which Clang is a part of also provides a linker (lld) and a C++ standard library (libc++) among other low-level things.

On Windows, you have two main options:

  1. Use clang-cl: compiles with a clang wrapper that understands cl.exe's optionsns. This links to the Visual C++ runtime/library and thus you really only replace the Visual Studio compiler, but not the library/runtime. The code you compile like this should be compatible with normal MSVC-compiled code.

  2. Use clang: this is the clang driver that understands GCC-like options. You would use it if you want to compile against the MinGW-w64 runtime.

Clang has an option to select its C++ standard library: -stdlib=libc++ selects the LLVM libc++. I doubt this works for clang-cl, but I haven't tried that to be honest. If you install Clang+libc++ through MSYS2, you can compile against libc++ and the MinGW-w64 runtime using the -stdlib option.


As to your concrete issue: you seem to be using the GNU-style interface for Clang, but it's detecting you're on Windows and compiles as if it were clang-cl. I think your best bet is to try a MinGW-w64-targeting clang (e.g. from MSYS2 as I suggested above).

As Clang is a flexible compiler, you could also specify the target with the -target commandline option. Unfortunately, I don't know the values you would need (nor the extra options to actually make it find the libc++ headers/libraries). That being said, I'm not sure libc++ works for a Visual Studio targeting Clang at all.

like image 114
rubenvb Avatar answered Jan 29 '23 22:01

rubenvb


Some notes from last year about a failed attempt to build a significant codebase on Windows with Clang under CMake (it builds on MacOS and two flavors of Linux):

  • To get CMake to generate on Windows, the coordinates of the needle in the multi-dimensional haystack are: x64 Native Command Prompt, NMake Makefiles, clang-cl.exe, CMAKE_CACHE_ARGS -D CMAKE_CXX_FLAGS:STRING=${CMAKE_CXX_FLAGS}. (The Packt book on CMake is decent for once, but doesn't explain most of this.)
  • @rubenvb was right to be sceptical of the -stdlib option on Windows; even the --help is wrong: “But actually using “-stdlib=” gives a warning about an unused argument,” bugs.llvm.org/show_bug.cgi?id=39878
like image 32
Flash Sheridan Avatar answered Jan 29 '23 20:01

Flash Sheridan