cpp-utils
v3.0.2
Published
Miscellaneous C/C++ compilation/compiler detection utility functions.
Downloads
32
Readme
cpp-utils
A collection of utility functions for C/C++ compilation from Node.js.
Contents
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());
});