oven-sh / bun

Incredibly fast JavaScript runtime, bundler, test runner, and package manager – all in one
https://bun.sh
Other
74.18k stars 2.77k forks source link

C Blog example results in "./hello.c:1: error: include file 'stdio.h' not found" #14014

Closed franciscop closed 1 month ago

franciscop commented 1 month ago

TL;DR: fix is coming in the next stable release, for now do:

bun upgrade --canary

What is the type of issue?

Example code is not working

What is the issue?

I'm trying to follow the first C example but find this error:

image

Being in MacOS 14.5 (Apple M1) with Bun 1.1.28, I've tried the "basic" things to no avail:

image

I thought it'd work just like that from reading @Jarred-Sumner's Tweets:

So I thought it'd work by default just doing that. Am I missing something perhaps? Do I need to run bun in some special way? I don't see any extra instructions in the blog post like "add --experimental flag" or anything like that.

Where did you find it?

First example of https://bun.sh/blog/compile-and-run-c-in-js

Here are my files (they are copy/pasted from the example):

image

Edit: changing the file from hello.js to hello.ts also didn't help.

franciscop commented 1 month ago

Update: it wasn't

This might be due to my macOS version[1][2], I'm upgrading and will report in a bit (will also try to uninstall+reinstall xcode).

[1] https://stackoverflow.com/a/52511046/938236 (but I don't have the folder /Library/Developer/CommandLineTools/Packages/) [2] https://stackoverflow.com/a/61418461/938236

luffs commented 1 month ago

Same behavior on Windows 11 (x64):

PS C:\Users\xxx\Desktop\bunc> bun .\main.js
1 | import { cc } from "bun:ffi";
2 |
3 | export const {
4 |   symbols: { myRandom },
5 | } = cc({
        ^
error: 1 errors while compiling ./myRandom.c

./myRandom.c:1: error: include file 'stdio.h' not found
      at cc (bun:ffi:87:12)
      at C:\Users\xxx\Desktop\bunc\main.js:5:5

Bun v1.1.28 (Windows x64)
jeremybanka commented 1 month ago

Was trying this too. I found that this worked on macOS arm64.

import { cc } from "bun:ffi";

export const {
  symbols: { myRandom },
} = cc({
  source: "./myRandom.c",
  include: ["/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include"], // ❗ manually include the standard libs
  symbols: {
    myRandom: {
      returns: "int",
      args: [],
    },
  },
});

console.log("myRandom() =", myRandom());

It would be nice if this were documented or if bun would look in the most likely places for your OS to keep these libraries.

franciscop commented 1 month ago

I can confirm I upgraded everything (macOS 15.0, uninstall+install xcode) and still doesn't work. However @jeremybanka's solution does seem to work!

~People in StackOverflow threads are mentioning /Library/Developer/CommandLineTools/Packages/ as the potential cause of the lack of stdio.h, however I didn't have that neither before nor after reinstalling xcode. Is it possible this changed? Can either @jeremybanka or @luffs please confirm whether you have that folder or not? (ls /Library/Developer/CommandLineTools/Packages/ will say "No such file or directory" or display the contents).~

TomasHubelbauer commented 1 month ago

It also didn't work for me and @jeremybanka's solution did. /Library/Developer/CommandLineTools/Packages/ does not exist on my system:

ls: /Library/Developer/CommandLineTools/Packages/: No such file or directory

sw_vers says:

ProductName:        macOS
ProductVersion:     14.6.1
BuildVersion:       23G93
jeremybanka commented 1 month ago

ls /Library/Developer/CommandLineTools/Packages/

I also do not have this. ChatGPT reports that this isn't a likely folder to interact with directly as a developer, and it exists purely to hold the .pkg files used to install the command line tools.

So, this is like Xcode's internal "Downloads/" directory, not where libraries themselves are ever actually located.

stefanos82 commented 1 month ago

In my case, under Debian testing 64-bit, I get the following:

image

infrahead commented 1 month ago

and under WSL Ubuntu 24 I get slightly different error (but assume all same root issue of not having proper compiler tooling installed?)

error: 1 errors while compiling ./hello.c
In file included from ./hello.c:4:
/usr/include/stdio.h:28: error: include file 'bits/libc-header-start.h' not found

Although what is weird is I can gcc hello.c -o hello and then ./hello and it works fine (all I did was add a main()

Jarred-Sumner commented 1 month ago

This is a classic case of “works on my machine” not working properly

I had $SDKROOT setup on macOS, which causes us to add it to the include path automatically. Need to make it work without that environment variable

I’m not sure yet what’s going wrong on Linux or Windows. I did try it on multiple Linux machines and it worked

stefanos82 commented 1 month ago

@Jarred-Sumner I forgot to mention that I have TCC installed locally, using the latest commit from https://repo.or.cz/w/tinycc.git in case it plays a role...by the way, https://github.com/TinyCC/tinycc is a bit outdated comparing the original repo I shared above.

franciscop commented 1 month ago

This is a classic case of “works on my machine” not working properly

I had $SDKROOT setup on macOS, which causes us to add it to the include path automatically. Need to make it work without that environment variable

I’m not sure yet what’s going wrong on Linux or Windows. I did try it on multiple Linux machines and it worked

Thanks for looking into it! So as a solution (at least for macOS), I'm guessing on Bun's macOS side you'll make it so even without $SDKROOT it will work in a future Bun release, right? And for now we use the include: ["/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include"] workaround?

luffs commented 1 month ago

Oh cool, it runs on Windows 11 too after downloading tinycc and adding "tinycc/win32/include" as an include. Downloaded the repo from https://github.com/TinyCC/tinycc


import { cc } from "bun:ffi";

export const {
  symbols: { myRandom },
} = cc({
  source: "./myRandom.c",
  include: ["C:/Users/xxx/Desktop/tinycc/win32/include"],
  symbols: {
    myRandom: {
      returns: "int",
      args: [],
    },
  },
});

console.log("myRandom() =", myRandom());
stefanos82 commented 1 month ago

Another interesting finding from my side is the retention of header files in /tmp/bun-cc directory, which I suppose they should have gotten deleted after successful compilation + execution of FFI code?

image

billywhizz commented 1 month ago
error: 1 errors while compiling ./hello.c
In file included from ./hello.c:4:
/usr/include/stdio.h:28: error: include file 'bits/libc-header-start.h' not found

@infrahead you can work around this issue by adding this include path, but even after this (on Ubuntu 22.04) i am getting the "library c not found" issue that @stefanos82 is seeing. i have libtcc-dev installed on here so maybe that is the issue. will post here if i find a solution.

  include: [
    '/usr/include/x86_64-linux-gnu'
  ],

Update 1

test.c

#include <stdlib.h>

int test() {
    return 42;
}

bun-c.js

import { cc } from "bun:ffi";

export const {
  symbols: { test },
} = cc({
  source: "./test.c",
  include: [
    '/usr/include/x86_64-linux-gnu'
  ],
  symbols: {
    test: {
      returns: "int",
      args: [],
    },
  },
});

console.log("test() =", test());

On Ubuntu 22.04 when i try to compile with bun i can see it searching for a libc library using strace -e openat:

strace -e openat bun bun-c.js

...
openat(AT_FDCWD, "/usr/local/lib/tcc/libc.so", O_RDONLY) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/usr/lib/libc.so", O_RDONLY) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/lib/libc.so", O_RDONLY) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/usr/local/lib/libc.so", O_RDONLY) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/usr/local/lib/libc.so", O_RDONLY) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/usr/lib/libc.so", O_RDONLY) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/usr/local/lib/tcc/libc.a", O_RDONLY) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/usr/lib/libc.a", O_RDONLY) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/lib/libc.a", O_RDONLY) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/usr/local/lib/libc.a", O_RDONLY) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/usr/local/lib/libc.a", O_RDONLY) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/usr/lib/libc.a", O_RDONLY) = -1 ENOENT (No such file or directory)

1 | import { cc } from "bun:ffi";
2 | 
3 | export const {
4 |   symbols: { test },
5 | } = cc({
        ^
error: 1 errors while compiling ./test.c
tcc: error: library 'c' not found

      at cc (bun:ffi:87:12)
      at /media/andrew/OCZ/source2023/just-js/lo-bench/clang/bun-c.js:5:5

when i compile the C source file using tcc directly i don't see these searches happening and it compiles without any issues.

strace -e openat tcc -c -o foo.o test.c

Update 2

when i compile and link with tcc i see the following path is found when linking, but this doesn't seem to be searched for when compiling and relocating with bun. so i am guessing this needs to be added to search path on linux when trying to do relocation with libtcc in bun? :thinking:

strace -e openat tcc -o foo test.c 2> out.log

openat(AT_FDCWD, "/usr/local/lib/tcc/libtcc1.a", O_RDONLY) = 3

Update 3

as a workaround, if i do the following everything works for me on Ubuntu 22.04:

sudo cp /usr/local/lib/tcc/libtcc1.a /usr/local/lib/tcc/libc.a

it seems the embedded bun tcc is looking for libc at this path instead of at libtcc1.a where it is installed by default when you do:

sudo apt install -y libtcc-dev
stefanos82 commented 1 month ago

I have removed the header file and used forward declaration, plus main() function and now I get the following message:

hello.ts:1: error: type defaults to int

shell returned 1

Can you people double-check it as I'm ready to get some sleep? I'm super tired right now :face_exhaling:

extern int printf(const char *, ...);

void hello(void)
{
    printf("You can now compile and run C code inside Bun!\n");
}

int main(void)
{
    hello();
    return 0;
}
import { cc } from "bun:ffi";

export const {
    symbols: { hello }
} = cc({
    source: "./hello.c",
    symbols: {
        hello: {
            returns: "void",
            args: [],
        },
    },
});

hello();
liudonghua123 commented 1 month ago

i have the same problems even I added tcc in the PATH environment.

Liu.D.H  Temp   7ms  10:26 > cat > hello.c
#include <stdio.h>

void hello() {
  printf("You can now compile & run C in Bun!\n");
}

Liu.D.H  Temp   3.45s  10:27 > cat > hello.ts
import { cc } from "bun:ffi";

export const {
  symbols: { hello },
} = cc({
  source: "./hello.c",
  symbols: {
    hello: {
      returns: "void",
      args: [],
    },
  },
});

hello();

Liu.D.H  Temp   6.994s  10:27 >
Liu.D.H  Temp   0ms  10:27 > bun hello.ts
1 | import { cc } from "bun:ffi";
2 |
3 | export const {
4 |   symbols: { hello },
5 | } = cc({
        ^
error: 1 errors while compiling ./hello.c
./hello.c:1: error: include file 'stdio.h' not found

      at cc (bun:ffi:87:12)
      at C:\Users\Liu.D.H\AppData\Local\Temp\hello.ts:5:5

Bun v1.1.28 (Windows x64)

Liu.D.H  Temp   129ms  10:27 > gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=C:/Strawberry/c/bin/../libexec/gcc/x86_64-w64-mingw32/13.1.0/lto-wrapper.exe
OFFLOAD_TARGET_NAMES=nvptx-none
Target: x86_64-w64-mingw32
Configured with: ../configure --prefix=/R/winlibs64_stage/inst_gcc-13.1.0/share/gcc --build=x86_64-w64-mingw32 --host=x86_64-w64-mingw32 --enable-offload-targets=nvptx-none --with-pkgversion='MinGW-W64 x86_64-msvcrt-posix-seh, built by Brecht Sanders' --with-tune=generic --enable-checking=release --enable-threads=posix --disable-sjlj-exceptions --disable-libunwind-exceptions --disable-serial-configure --disable-bootstrap --enable-host-shared --enable-plugin --disable-default-ssp --disable-rpath --disable-libstdcxx-debug --disable-version-specific-runtime-libs --with-stabs --disable-symvers --enable-languages=c,c++,fortran,lto,objc,obj-c++ --disable-gold --disable-nls --disable-stage1-checking --disable-win32-registry --disable-multilib --enable-ld --enable-libquadmath --enable-libada --enable-libssp --enable-libstdcxx --enable-lto --enable-fully-dynamic-string --enable-libgomp --enable-graphite --enable-mingw-wildcard --enable-libstdcxx-time --enable-libstdcxx-pch --with-mpc=/d/Prog/winlibs64_stage/custombuilt --with-mpfr=/d/Prog/winlibs64_stage/custombuilt --with-gmp=/d/Prog/winlibs64_stage/custombuilt --with-isl=/d/Prog/winlibs64_stage/custombuilt --disable-libstdcxx-backtrace --enable-install-libiberty --enable-__cxa_atexit --without-included-gettext --with-diagnostics-color=auto --enable-clocale=generic --with-libiconv --with-system-zlib --with-build-sysroot=/R/winlibs64_stage/gcc-13.1.0/build_mingw/mingw-w64 CFLAGS='-I/d/Prog/winlibs64_stage/custombuilt/include/libdl-win32 -Wno-int-conversion  -march=nocona -msahf -mtune=generic -O2' CXXFLAGS='-Wno-int-conversion  -march=nocona -msahf -mtune=generic -O2' LDFLAGS='-pthread -Wl,--no-insert-timestamp -Wl,--dynamicbase -Wl,--high-entropy-va -Wl,--nxcompat -Wl,--tsaware'
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 13.1.0 (MinGW-W64 x86_64-msvcrt-posix-seh, built by Brecht Sanders)

Liu.D.H  Temp   276ms  12:07 >
Liu.D.H  Temp   7ms  12:12 > set PATH=D:\apps\tcc-0.9.27-win64-bin;%PATH%                                                       
Liu.D.H  Temp   7ms  12:12 > tcc -v
tcc version 0.9.27 (x86_64 Windows)

Liu.D.H  Temp   80ms  12:12 > bun hello.ts
1 | import { cc } from "bun:ffi";
2 |
3 | export const {
4 |   symbols: { hello },
5 | } = cc({
        ^
error: 1 errors while compiling ./hello.c
./hello.c:1: error: include file 'stdio.h' not found

      at cc (bun:ffi:87:12)
      at C:\Users\Liu.D.H\AppData\Local\Temp\hello.ts:5:5

Bun v1.1.28 (Windows x64)

Liu.D.H  Temp   118ms  12:12 >
Jarred-Sumner commented 1 month ago

The fix for this will be part of Bun v1.1.29

You can run bun upgrade --canary in the meantime to upgrade early.

liudonghua123 commented 1 month ago

The fix for this will be part of Bun v1.1.29

You can run bun upgrade --canary in the meantime to upgrade early.

The latest canary Bun v1.1.28-canary.2+181b8722e (Windows x64) build seems not working for me.

C:\Users\LIUD~1.H\AppData\Local\Temp>bun upgrade --canary
[176.33s] Upgraded.

Welcome to Bun's latest canary build!

Report any bugs:

    https://github.com/oven-sh/bun/issues

Changelog:

    https://github.com/oven-sh/bun/compare/cf4e9cb69ee862037b09f2cd6668be76d2aca78d...main

C:\Users\LIUD~1.H\AppData\Local\Temp>bun -v
1.1.28

C:\Users\LIUD~1.H\AppData\Local\Temp>where bun
C:\Users\Liu.D.H\AppData\Roaming\npm\bun
C:\Users\Liu.D.H\AppData\Roaming\npm\bun.CMD

C:\Users\LIUD~1.H\AppData\Local\Temp>notepad hello.c

C:\Users\LIUD~1.H\AppData\Local\Temp>notepad hello.ts

C:\Users\LIUD~1.H\AppData\Local\Temp>bun hello.ts
1 | import { cc } from "bun:ffi";
2 |
3 | export const {
4 |   symbols: { hello },
5 | } = cc({
        ^
error: 1 errors while compiling ./hello.c
./hello.c:1: error: include file 'stdio.h' not found

      at cc (bun:ffi:87:12)
      at C:\Users\Liu.D.H\AppData\Local\Temp\hello.ts:5:5

Bun v1.1.28-canary.2+181b8722e (Windows x64)

C:\Users\LIUD~1.H\AppData\Local\Temp>
pradeepbgs commented 1 month ago

hey if anyone is still having error then upgrade bun by this -

bun upgrade --canary

luffs commented 1 month ago

hey if anyone is still having error then upgrade bun by this -

bun upgrade --canary

Still have to add include: ["./tinycc/win32/include"], on Windows 11.

C:\Users\xxx\Desktop\bunc>bun main.js
1 | import { cc } from "bun:ffi";
2 |
3 | export const {
4 |   symbols: { myRandom },
5 | } = cc({
        ^
error: 1 errors while compiling ./myRandom.c
./myRandom.c:1: error: include file 'stdio.h' not found

      at cc (bun:ffi:87:12)
      at C:\Users\xxx\Desktop\bunc\main.js:5:5

Bun v1.1.28-canary.3+866a6d918 (Windows x64)
pradeepbgs commented 1 month ago

hey if anyone is still having error then upgrade bun by this - bun upgrade --canary

Still have to add include: ["./tinycc/win32/include"], on Windows 11.

did you upgrade it with bun upgrade --canary ??

or still having issue try to delete bun then re install OR wait for stable release

stefanos82 commented 1 month ago
error: 1 errors while compiling ./hello.c
In file included from ./hello.c:4:
/usr/include/stdio.h:28: error: include file 'bits/libc-header-start.h' not found

@infrahead you can work around this issue by adding this include path, but even after this (on Ubuntu 22.04) i am getting the "library c not found" issue that @stefanos82 is seeing. i have libtcc-dev installed on here so maybe that is the issue. will post here if i find a solution.

  include: [
    '/usr/include/x86_64-linux-gnu'
  ],

Update 1

test.c

#include <stdlib.h>

int test() {
    return 42;
}

bun-c.js

import { cc } from "bun:ffi";

export const {
  symbols: { test },
} = cc({
  source: "./test.c",
  include: [
    '/usr/include/x86_64-linux-gnu'
  ],
  symbols: {
    test: {
      returns: "int",
      args: [],
    },
  },
});

console.log("test() =", test());

On Ubuntu 22.04 when i try to compile with bun i can see it searching for a libc library using strace -e openat:

strace -e openat bun bun-c.js

...
openat(AT_FDCWD, "/usr/local/lib/tcc/libc.so", O_RDONLY) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/usr/lib/libc.so", O_RDONLY) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/lib/libc.so", O_RDONLY) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/usr/local/lib/libc.so", O_RDONLY) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/usr/local/lib/libc.so", O_RDONLY) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/usr/lib/libc.so", O_RDONLY) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/usr/local/lib/tcc/libc.a", O_RDONLY) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/usr/lib/libc.a", O_RDONLY) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/lib/libc.a", O_RDONLY) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/usr/local/lib/libc.a", O_RDONLY) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/usr/local/lib/libc.a", O_RDONLY) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/usr/lib/libc.a", O_RDONLY) = -1 ENOENT (No such file or directory)

1 | import { cc } from "bun:ffi";
2 | 
3 | export const {
4 |   symbols: { test },
5 | } = cc({
        ^
error: 1 errors while compiling ./test.c
tcc: error: library 'c' not found

      at cc (bun:ffi:87:12)
      at /media/andrew/OCZ/source2023/just-js/lo-bench/clang/bun-c.js:5:5

when i compile the C source file using tcc directly i don't see these searches happening and it compiles without any issues.

strace -e openat tcc -c -o foo.o test.c

Update 2

when i compile and link with tcc i see the following path is found when linking, but this doesn't seem to be searched for when compiling and relocating with bun. so i am guessing this needs to be added to search path on linux when trying to do relocation with libtcc in bun? 🤔

strace -e openat tcc -o foo test.c 2> out.log

openat(AT_FDCWD, "/usr/local/lib/tcc/libtcc1.a", O_RDONLY) = 3

Update 3

as a workaround, if i do the following everything works for me on Ubuntu 22.04:

sudo cp /usr/local/lib/tcc/libtcc1.a /usr/local/lib/tcc/libc.a

it seems the embedded bun tcc is looking for libc at this path instead of at libtcc1.a where it is installed by default when you do:

sudo apt install -y libtcc-dev

Your strace output was extremely helpful as it has helped me identify the missing libraries while executing the ts code:

openat(AT_FDCWD, "./hello.c", O_RDONLY) = 13
openat(AT_FDCWD, "/usr/local/lib/tcc/libc.so", O_RDONLY) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/usr/lib/libc.so", O_RDONLY) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/lib/libc.so", O_RDONLY) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/usr/local/lib/libc.so", O_RDONLY) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/usr/local/lib/libc.so", O_RDONLY) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/usr/lib/libc.so", O_RDONLY) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/usr/local/lib/tcc/libc.a", O_RDONLY) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/usr/lib/libc.a", O_RDONLY) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/lib/libc.a", O_RDONLY) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/usr/local/lib/libc.a", O_RDONLY) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/usr/local/lib/libc.a", O_RDONLY) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/usr/lib/libc.a", O_RDONLY) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/home/stefanos/code/ts/hello/hello.ts", O_RDONLY) = 13

This by itself speaks volumes to me as it looks at wrong directories for dynamic libraries with the wrong names!

I guess I'll have to wait until the next stable version.

pradeepbgs commented 1 month ago

First question did you upgrade bun by this - bun upgrade --canary ?? If not do this then run again and just give ./c file path there .

stefanos82 commented 1 month ago

Yes, with canary works as expected; how do I switch back to stable version though?

pradeepbgs commented 1 month ago

It means code ran ?

stefanos82 commented 1 month ago

Yes, with upgrading to canary it worked as expected.

pradeepbgs commented 1 month ago

Then why are planning to go to stable?? In stable you will issue again. BTW if u wanna go to stable - just do bun upgrade and if this don't work Remove bun then install again

stefanos82 commented 1 month ago

Nope, I have just found the way; it's bun upgrade --stable.

I tested your way and verified it works; meaning, when the next stable version gets released, this code will work as expected, but for now it won't.

pradeepbgs commented 1 month ago

Yes. BTW can you go through my project mayajs ?? Which is pinned in my profile. It's a simple http web server library built in nodejs net module.

Its not for production but it's a great project to showcase in resume

antlanc7 commented 1 month ago

On windows 11 with Bun 1.1.29 stable it's still the same. Just init a blank project and copy the base example. You get ./hello.c:1: error: include file 'stdio.h' not found. Am I supposed to download TinyCC by myself?

nassau-t commented 1 month ago

On windows, download tcc-0.9.27-win64-bin.zip from http://download.savannah.gnu.org/releases/tinycc/ Decompress it on c:\tcc, for example, then use this text as myRandom.js (look at c:/tcc references on include section)

import { cc } from "bun:ffi";

export const {
  symbols: { myRandom },
} = cc({
  source: "./myRandom.c",
  include: [
    'c:/tcc',
    'c:/tcc/include',
    'c:/tcc/lib'
  ],
  symbols: {
    myRandom: {
      returns: "int",
      args: [],
    },
  },
});

console.log("myRandom() =", myRandom());

Then, it works,

>bun myRandom.js
myRandom() = 83

EDIT: bun version 1.1.29, so It doesn't work yet without doing this.

pradeepbgs commented 1 month ago

On windows, download tcc-0.9.27-win64-bin.zip from http://download.savannah.gnu.org/releases/tinycc/ Decompress it on c:\tcc, for example, then use this text as myRandom.js (look at c:/tcc references on include section)

import { cc } from "bun:ffi";

export const {
  symbols: { myRandom },
} = cc({
  source: "./myRandom.c",
  include: [
    'c:/tcc',
    'c:/tcc/include',
    'c:/tcc/lib'
  ],
  symbols: {
    myRandom: {
      returns: "int",
      args: [],
    },
  },
});

console.log("myRandom() =", myRandom());

Then, it works,

>bun myRandom.js
myRandom() = 83

now new version of bun has released just upgrade to 1.1.29 , the issue has resolved now

antlanc7 commented 1 month ago

Even in 1.1.29 it doesn't work in windows, I have to include manually the header files folder from TinyCC, but I don't think it's the supposed way to do it, since it is not documented anywhere.

pradeepbgs commented 1 month ago

Even in 1.1.29 it doesn't work in windows, I have to include manually the header files folder from TinyCC, but I don't think it's the supposed way to do it, since it is not documented anywhere.

then its a bug i think , you should make a new issue about it.