Skip to content

Conversation

@Akuli
Copy link
Owner

@Akuli Akuli commented Apr 14, 2025

This makes it easy to sprinkle a little bit of Jou code into a project written in C. If you do -o foo.a when compiling Jou code, foo.a will contain object files for your program and whatever it uses from the Jou standard library.

For example, I placed this to test.jou:

import "stdlib/io.jou"
import "stdlib/list.jou"
import "stdlib/mem.jou"


def main() -> int:
    args = List[byte*]{}
    args.append("Hello")
    args.append(" ")
    args.append("World!")
    args.append("\n")

    for i = 0; i < args.len; i++:
        printf("%s", args.ptr[i])

    free(args.ptr)
    return 0

And compiled it to .a and gave that to a C compiler:

akuli@akuli-desktop:~/jou$ jou -o test.a test.jou
akuli@akuli-desktop:~/jou$ file test.a
test.a: current ar archive
akuli@akuli-desktop:~/jou$ cc test.a
akuli@akuli-desktop:~/jou$ ./a.out
Hello World!

Here's what ended up in the .a file:

akuli@akuli-desktop:~/jou$ ar t test.a
_assert_fail_6462dea0597d2c04_75f89d3cb3be1ebe.o
test_dce56ce79d1cbb8c_c159ba627f5e1510.o
io_113a41539cd5dd30_ad9ac958397bd736.o
list_665829f653098331_b202293f74d46422.o
mem_1d45d615c5de45c1_4e6b4fbee1296e6b.o

If the Jou code contains link statements, you will need to specify the corresponding flags when compiling the .a file with a C compiler:

akuli@akuli-desktop:~/jou$ jou -o compiler.a compiler/main.jou 
akuli@akuli-desktop:~/jou$ cc compiler.a -L/usr/lib/llvm-19/lib -lLLVM-19
akuli@akuli-desktop:~/jou$ ./a.out examples/hello.jou 
Hello World

Fixes #773

@Akuli
Copy link
Owner Author

Akuli commented Apr 14, 2025

  • get CI green
  • add tests
  • add documentation

@Akuli Akuli marked this pull request as draft April 14, 2025 23:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Compiling to .a file

2 participants