Skip to content

Use Clang on Windows #2295

Description

@aminya
  • Node Version: All
  • Platform: Win 10
  • Compiler: Clang (clang-cli or clang)
  • Module: All

How can we use Clang on Windows to build native modules? I want to use a single open-source compiler for all the platforms. Writing compiler flags for each compiler is not efficient, and they also may have different performance.

This might be related to:

Activity

  1. rvagg commented on Dec 31, 2020

    @rvagg
    Member

    you could just not use node-gyp and do something manual in your package.json "build". node-gyp (& gyp) is designed to make it flexible across platforms but if you just want to enforce clang on your users then make your own build script that does everything and bypass node-gyp entirely.

    I'm not sure gyp has full clang for windows support, does it @nodejs/gyp? it'd just be a bunch of hacks on hacks to make that work for an addon wouldn't it?

  2. gengjiawen commented on Jan 5, 2021

    @gengjiawen
    Member

    For now, it's kind of buggy, not production ready. Maybe after when we resolve nodejs/node#35433.

  3. TrevorDev commented on May 5, 2021

    @TrevorDev

    @rvagg could you elaborate on "you could just not use node-gyp and do something manual in your package.json"

    I can use napi.h in my c++ files and use clang to generate a dll file on windows but I'm confused on what a .node module file is. Is there a way I can convert a dll to one?

  4. rvagg commented on May 5, 2021

    @rvagg
    Member

    @TrevorDev I can't give you an example off the top of my head, but there are plenty of Node.js addons that don't use node-gyp to build. cmake.js I think is doing their own thing. I've seen plain Makefile builds. The end goal is to build a library that can be dynamically loaded by node and .node is the convention for naming that binary libary. But how you build it is up to you. It just happens that node-gyp has integration in npm, but npm will also run scripts on install so just insert your build process in there. On windows, it's really just a .dll with a different name. I don't know the specifics of the build process but I think you can set an environment variable V to 1 and build someone's addon (via npm install or node-gyp rebuild or whatever) then gyp will show you a verbose compile output and you can see what it's doing. I assume this also works on Windows. You could learn from this process to make your own build script(s) that do what you want. Experiment and you may find a pattern that others can learn from!

  5. TrevorDev commented on May 5, 2021

    @TrevorDev

    Woot got it working 🚀, thanks for the info.

    For anyone else, this command worked for me:
    clang++ test.cpp -Inode_modules\node-addon-api\ -IC:\Users\username.cmake-js\node-x64\v10.16.3\include\node -o test.node -shared C:\Users\username.cmake-js\node-x64\v10.16.3\win-x64\node.lib

  6. aminya commented on May 5, 2021

    @aminya
    Author

    @TrevorDev I'm glad that you made it work.

    I have been working on a new project called cmake-ts. It provides a unified interface for cross-compiling prebuilds for other operating systems (including Windows). It fills in the gaps in the Node ecosystem providing an all-in-one solution for building/prebuilding native addons. So, stay tuned for that (it is still unstable).

    https://github.com/EmbeddedEnterprises/cmake-ts/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions