Skip to content

Instantly share code, notes, and snippets.

@Andre-LA
Last active October 17, 2022 11:46
Show Gist options
  • Save Andre-LA/c4184369878c99d6e0c0d3130acf7d29 to your computer and use it in GitHub Desktop.
Save Andre-LA/c4184369878c99d6e0c0d3130acf7d29 to your computer and use it in GitHub Desktop.
nelua as C library
/* Generated by Nelua 0.2.0-dev */
/* Compile command: gcc "libmylib.c" -o "libmylib" -Wall -fwrapv -g -lm */
/* Compile hash: 3ubsTFvAUFqRHss7xDS1yGaiVwdM */
/* ------------------------------ DECLARATIONS ------------------------------ */
// moved to here:
#ifndef MYLIB_H
#define MYLIB_H
#ifdef __GNUC__
#pragma GCC diagnostic error "-Wimplicit-function-declaration"
#pragma GCC diagnostic error "-Wimplicit-int"
#pragma GCC diagnostic ignored "-Wincompatible-pointer-types"
#pragma GCC diagnostic ignored "-Wmissing-braces"
#pragma GCC diagnostic ignored "-Wmissing-field-initializers"
#pragma GCC diagnostic ignored "-Wtype-limits"
#pragma GCC diagnostic ignored "-Wunused-parameter"
#if defined(__clang__)
#pragma GCC diagnostic ignored "-Wunused"
#else
#pragma GCC diagnostic ignored "-Wunused-variable"
#pragma GCC diagnostic ignored "-Wunused-function"
#pragma GCC diagnostic ignored "-Wunused-but-set-variable"
#pragma GCC diagnostic ignored "-Wdiscarded-qualifiers"
#endif
#endif
#if __STDC_VERSION__ >= 201112L
#define nelua_static_assert _Static_assert
#else
#define nelua_static_assert(x, y)
#endif
nelua_static_assert(sizeof(void*) == 8, "Nelua and C disagree on architecture size");
// moved to top
// #ifndef MYLIB_H
// #define MYLIB_H
#ifdef _WIN32
#define nelua_cexport __declspec(dllexport) extern
#elif defined(__GNUC__)
#define nelua_cexport __attribute__((visibility ("default"))) extern
#else
#define nelua_cexport extern
#endif
nelua_cexport int mylib_sum(int a, int b);
/* ------------------------------ DEFINITIONS ------------------------------- */
#endif /* MYLIB_H */
#ifdef MYLIB_IMPL
int mylib_sum(int a, int b) {
return (a + b);
}
#endif /* MYLIB_IMPL */
-- note: the extension of this file is actually libmylib.nelua,
-- I changed to .lua just to activate Github syntax highlighting.
-- reference:
--
-- creating C libraries with nelua:
-- https://nelua.io/overview/#exporting-named-c-functions
-- https://github.com/edubart/nelua-lang/blob/master/tests/libmylib.nelua
-- https://github.com/edubart/nelua-lang/blob/master/tests/mylib_test.nelua
--
-- I used `-P noentrypoint` to not define the `main` function:
-- https://github.com/edubart/nelua-lang/blob/50989179b3f19c216dc438475e7acfe63b83c359/nelua/cgenerator.lua#L1651
--
-- about including C files:
-- https://stackoverflow.com/a/11334387
--
-- about single-header libraries:
-- https://nicolashollmann.de/blog/single-header-libraries/
--
-- command to transpile to C lib:
-- $ nelua libmylib.nelua -o libmylib -c -P noentrypoint
-- don't use GC and set prefix
## pragmas.nogc = true
## pragmas.unitname = 'mylib'
-- sets MYLIB_H, needs to be moved manually to the top
##[=[ cemitdecl[[
#ifndef MYLIB_H
#define MYLIB_H
]]
]=]
-- unset MYLIB_H
## cemitdef'#endif /* MYLIB_H */'
-- set MYLIB_IMPL, which "guards" the implementation
## cemitdef'#ifdef MYLIB_IMPL'
-- let's define a function!
local function sum(a: cint, b: cint): cint <cexport>
return a + b
end
-- unset MYLIB_IMPL, no functions should be declared after this
## cemitdef'#endif /* MYLIB_IMPL */'
#include <stdio.h>
#define MYLIB_IMPL
#include "libmylib.c"
int main() {
int x = mylib_sum(1, 2);
printf("-> %d\n", x);
return 0;
}
// command:
// $ gcc test.c -o test
//
// output:
// $ ./test
// -> 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment