-
-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add example of making C libraries in a test
- Loading branch information
Showing
12 changed files
with
91 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,4 +16,5 @@ neluacfg.lua | |
.neluacfg.lua | ||
/play | ||
nelua-lua | ||
/pkg | ||
/pkg | ||
/mylib_test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
-- Disable the garbage collector, | ||
-- because libraries should not and cannot use the garbage collector, | ||
-- this means that you are responsible to manually manage the memory. | ||
## pragmas.nogc = true | ||
-- Prefix used when declaring C symbols for this file. | ||
-- With this an exported function named 'sum' will be exported as 'mylib_sum' | ||
## pragmas.unitname = 'mylib' | ||
-- Compilation flags needed for compiling C libraries. | ||
## cflags '-shared -fPIC' | ||
|
||
print 'mylib - in top scope' | ||
|
||
local function sum(x: integer, y: integer): integer <cexport> | ||
print 'mylib - sum' | ||
return x + y | ||
end | ||
|
||
-- Initialize the library. | ||
-- This function is marked as entry point, initialization code should be done here. | ||
local function init() <entrypoint, codename 'mylib_init'> | ||
-- Import and call nelua_main on initialization. | ||
-- This function run all code in top scope and initializes global variables. | ||
local function nelua_main(argc: cint, argv: *cstring): cint <cimport,nodecl> end | ||
print 'mylib - init' | ||
nelua_main(0, nilptr) | ||
end | ||
|
||
-- Terminate the library. | ||
local function terminate() <cexport> | ||
print 'mylib - terminate' | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
-- Add the current folder to the linker's library search path, | ||
-- we expect that 'mylib' library is available in the current path. | ||
## cflags "-L. -Wl,-rpath,'$ORIGIN'" | ||
-- Link mylib C library. | ||
## linklib 'mylib' | ||
|
||
-- Import mylib functions. | ||
local function mylib_sum(x: integer, y: integer): integer <cimport> end | ||
local function mylib_init() <cimport> end | ||
local function mylib_terminate() <cimport> end | ||
|
||
-- Run example. | ||
mylib_init() | ||
local a = mylib_sum(1, 2) | ||
print('the sum is:') | ||
print(a) | ||
assert(a == 3) | ||
mylib_terminate() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters