-
Notifications
You must be signed in to change notification settings - Fork 2.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
C Blog example results in "./hello.c:1: error: include file 'stdio.h' not found" #14014
Comments
This comment was marked as outdated.
This comment was marked as outdated.
Same behavior on Windows 11 (x64):
|
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. |
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!
|
It also didn't work for me and @jeremybanka's solution did.
|
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 So, this is like Xcode's internal "Downloads/" directory, not where libraries themselves are ever actually located. |
and under WSL Ubuntu 24 I get slightly different error (but assume all same root issue of not having proper compiler tooling installed?)
Although what is weird is I can |
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 |
@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. |
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 |
Oh cool, it runs on Windows 11 too after downloading tinycc and adding "tinycc/win32/include" as an include. 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()); |
@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 1test.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 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 2when 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 3as 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 |
I have removed the header file and used forward declaration, plus
Can you people double-check it as I'm ready to get some sleep? I'm super tired right now 😮💨 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(); |
i have the same problems even I added
|
The fix for this will be part of Bun v1.1.29 You can run |
The latest canary
|
hey if anyone is still having error then upgrade bun by this - bun upgrade --canary |
Still have to add
|
did you upgrade it with bun upgrade --canary ?? or still having issue try to delete bun then re install OR wait for stable release |
Your
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. |
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 . |
Yes, with |
It means code ran ? |
Yes, with upgrading to |
Then why are planning to go to stable?? In stable you will issue again. |
Nope, I have just found the way; it's 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. |
Yes. Its not for production but it's a great project to showcase in resume |
On windows 11 with Bun 1.1.29 stable it's still the same. |
On windows, download tcc-0.9.27-win64-bin.zip from http://download.savannah.gnu.org/releases/tinycc/
Then, it works,
EDIT: bun version 1.1.29, so It doesn't work yet without doing this. |
now new version of bun has released just upgrade to 1.1.29 , the issue has resolved now |
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. |
I tried it and found that if you encounter this issue in the bun Docker container, installing dependencies can solve it.
|
TL;DR: fix is coming in the next stable release, for now do:
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:
Being in MacOS 14.5 (Apple M1) with Bun 1.1.28, I've tried the "basic" things to no avail:
xcode-select --install
-> "Command line tools are already installed."void hello() {
toint main() {
allows me to compile and run the C program as usual: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):
Edit: changing the file from
hello.js
tohello.ts
also didn't help.The text was updated successfully, but these errors were encountered: