Narcissistic Passion Minified

    cpp-utils
    TypeScript icon, indicating that this package has built-in type declarations

    3.0.0 • Public • Published

    cpp-utils

    GitHub license npm version Unit Tests codecov

    A collection of utility functions for C/C++ compilation from Node.js.

    Contents

    1. Requirements
    2. Installation
    3. Usage
      1. Compiler Detection
      2. Compilation
    4. Examples

    Requirements

    • Either gcc (preferably with g++) or clang must be in your system path.
    • Node.js 16.x or above.

    Installation

    Run yarn add cpp-utils or npm i cpp-utils.

    Usage

    The documentation can be found here.

    Compiler Detection

    Compilers can be detected by calling checkFor[COMPILER]() where [COMPILER] is either Gcc, GPlus (g++), Clang, or ClangPlus (clang++), for example: checkForGcc(). You can also use checkForCompiler(compilerName) to detect supported compilers where the compiler executable name differs from the default, for example: checkForCompiler('x86_64-w64-mingw32-gcc').

    Compilation

    Use compileWith[COMPILER](inputFile, outputFile, link) to compile a C/C++ source file. Use the link parameter to indicate whether you want the output to be linked or not (by default it is set to true).

    Examples

    Example 1

    import {
      CompilerNotFoundError,
      checkForCompiler,
      checkForGcc,
      checkForGPlus,
      checkForClang,
      checkForClangPlus,
    } from 'cpp-utils';
    
    describe('Compiler detection', () => {
      it('Throws CompilerNotFoundError when a specified compiler cannot be found', () => {
        const falseCompiler = 'lol';
        return expect(checkForCompiler(falseCompiler)).rejects.toBeInstanceOf(CompilerNotFoundError);
      });
      it('Detects gcc', () => expect(checkForGcc()).resolves.toBeDefined());
      it('Detects g++', () => expect(checkForGPlus()).resolves.toBeDefined());
      it('Detects clang', () => expect(checkForClang()).resolves.toBeDefined());
      it('Detects clang++', () => expect(checkForClangPlus()).resolves.toBeDefined());
    });

    Example 2

    const fs = require('fs/promises');
    const {
      compileWithGcc,
      compileWithGPlus,
      compileWithClang,
      compileWithClangPlus,
    } = require('cpp-utils');
    
    const sourceFile = 'hello.c';
    const exeExtension = process.platform === 'win32' ? '.exe' : '';
    const exeFile = `hello${exeExtension}`;
    
    describe('Compilation', () => {
      afterEach(() => fs.unlink(exeFile));
      test('With gcc', () => expect(compileWithGcc(sourceFile, exeFile))
        .resolves.toBeDefined());
      test('With g++', () => expect(compileWithGPlus(sourceFile, exeFile))
        .resolves.toBeDefined());
      test('With clang', () => expect(compileWithClang(sourceFile, exeFile))
        .resolves.toBeDefined());
      test('With clang++', () => expect(compileWithClangPlus(sourceFile, exeFile))
        .resolves.toBeDefined());
    });

    Keywords

    Install

    npm i cpp-utils

    DownloadsWeekly Downloads

    1

    Version

    3.0.0

    License

    MIT

    Unpacked Size

    43.2 kB

    Total Files

    40

    Last publish

    Collaborators

    • synthetic.borealis