https://github.com/synthetic-borealis/cpp-utils.js
Detect C/C++ compilers and compile single source file programs in Node.js.
https://github.com/synthetic-borealis/cpp-utils.js
c cpp javascipt nodejs testing-tools typescript
Last synced: 4 months ago
JSON representation
Detect C/C++ compilers and compile single source file programs in Node.js.
- Host: GitHub
- URL: https://github.com/synthetic-borealis/cpp-utils.js
- Owner: synthetic-borealis
- License: mit
- Created: 2022-01-19T19:53:29.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2024-02-27T21:39:57.000Z (over 1 year ago)
- Last Synced: 2025-01-31T20:44:08.382Z (4 months ago)
- Topics: c, cpp, javascipt, nodejs, testing-tools, typescript
- Language: TypeScript
- Homepage:
- Size: 1.39 MB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# cpp-utils
[](https://github.com/synthetic-borealis/cpp-utils.js/blob/main/LICENSE)
[](https://badge.fury.io/js/cpp-utils)

[](https://codecov.io/gh/synthetic-borealis/cpp-utils.js)A collection of utility functions for C/C++ compilation from Node.js.
## Contents
1. [Requirements](#requirements)
2. [Installation](#installation)
3. [Usage](#usage)
1. [Compiler Detection](#compiler-detection)
2. [Compilation](#compilation)
4. [Examples](#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](./docs/API.md).
### 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
```typescript
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
```javascript
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());
});
```