为 C 库生成 Odin 绑定
This generator makes it possible to quickly generate C library bindings for the Odin Programming Language.
Features:
If you find this generator helpful and want to say thanks, then please consider donating.
Discuss and ask questions on my Discord server.
lib/libclang.lib into the generator's 'libclang' folderbin/libclang.dll into the root of the generator (next to where the bindgen executable will end up).apt install libclang-dev on Ubuntu/Debian/Mint. Anything from clang version 16 and newer should work.[!NOTE] libclang is used for analysing the C headers and deciding what Odin code to output.
odin build src -out:bindgen.exe (replace .exe with .bin on mac/Linux).h files) of the library you want to generate bindings for.bindgen the_folderthe_folder/the_folderbindgen.sjson file to. See how in the next section, or look in the examples folder.Add a bindgen.sjson to your bindings folder. I.e. inside the folder you feed into bindgen. Below is an example. See the examples folder for more advanced examples.
NOTE: Config uses the function/type names as found in header files.
…Please look through the list of configuration options listed above and see if they help you. Also, see the the examples folder for additional inspiration.
If you fail to make any progress on generating bindings for a certain library, then submit an issue on this GitHub page and provide the headers in a zip. I'll try to help if I can find some time.
If the source header is called raylib.h then add a a file called raylib_footer.odin next to it
and put your code in there.
Use imports_file in bindgen.sjson. See examples/raylib
In bindgen.sjson:
bit_setify = {
"Enum_To_Turn_Into_Bitset" = "New_Enum_Type_Name"
}This will replace the type Enum_To_Turn_Into_Bitset (an enum) with a bit_set. The type will look like this:
This will create a type Enum_To_Turn_Into_Bitset :: bit_set[New_Enum_Type_Name; i32].
The members that Enum_To_Turn_Into_Bitset had when it was an enum will be moved into a new enum called New_Enum_Type_Name. Within that enum the members will have their values converted using a log2 procedure. The log2 procedure turns for example 2 into 1 and 4 into 2. The bit_set itself will use these numbers to target a specific bit within its backing type.
If the generator is processing include/some_folder/header.h and it can't find some other header include/some_folder/something.h, then add include to the include search path by adding he following to bindgen.sjson:
clang_include_paths = [
"include"
]If you want to fix issues or add features, then you can create a Pull Request to this repository.
Check out the Issues tab and see if there is something you could help with.
To learn more about how the program works, start by looking in the src/main.odin file. That file loads the bindgen configuration file and then runs procedures in src/translate_collect.odin, src/translate_macros.odin, src/translate_process.odin and src/output.odin. All those files have some comments that try to explain what they do.
Big thanks to Xandaron for figuring out a lot of the libclang stuff.
This generator was inspired by floooh's Sokol bindgen: https://github.com/floooh/sokol/tree/master/bindgen
暂无开放 Issues,或尚未同步最近议题。