{"id":28205874,"url":"https://github.com/aetopia/c-functions-in-nim","last_synced_at":"2025-07-09T08:04:13.505Z","repository":{"id":133154105,"uuid":"534884839","full_name":"Aetopia/C-Functions-in-Nim","owner":"Aetopia","description":null,"archived":false,"fork":false,"pushed_at":"2022-09-10T13:01:12.000Z","size":3,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-06-11T11:41:36.849Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Nim","has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Aetopia.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2022-09-10T03:58:03.000Z","updated_at":"2022-09-10T07:10:17.000Z","dependencies_parsed_at":null,"dependency_job_id":"e268d5b3-28c1-456a-8893-2b0c7bc1282f","html_url":"https://github.com/Aetopia/C-Functions-in-Nim","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/Aetopia/C-Functions-in-Nim","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Aetopia%2FC-Functions-in-Nim","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Aetopia%2FC-Functions-in-Nim/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Aetopia%2FC-Functions-in-Nim/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Aetopia%2FC-Functions-in-Nim/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Aetopia","download_url":"https://codeload.github.com/Aetopia/C-Functions-in-Nim/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Aetopia%2FC-Functions-in-Nim/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":264419453,"owners_count":23605197,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-05-17T10:08:38.276Z","updated_at":"2025-07-09T08:04:13.498Z","avatar_url":"https://github.com/Aetopia.png","language":"Nim","funding_links":[],"categories":[],"sub_categories":[],"readme":"# C Functions in Nim\n \nThis repository shows a simple example of how to use C functions natively in Nim.\n\n## What is Nim?\nA language that is as fast as C, expressive as Python and extensible as Lisp.\n\n### How is Nim is as fast as C?\nTo simply put, the Nim compiler more of indirectly compiles into machine code rather than directly.\nThis is because the Nim compiler translates Nim source code to C source code and that C source code is then compiled.\nThus, giving Nim, the speed of C.\n\n# How to use C functions in Nim?\n\n1. To begin, create a `.c` and `.h` file containing your functions.\n\n    \u003e **`funcs.h`**\n    ```c\n    #pragma once\n    #include \u003cstdio.h\u003e\n\n    // Prototypes\n    void foo();\n    void bar(char *input);\n    ```\n\n    \u003e **`funcs.c`**\n    ```c\n    #include \"funcs.h\"\n\n    void foo()\n    {\n        printf(\"No arguments passed from Nim \u003e Hello from C!\\n\");\n    }\n\n    void bar(char *input)\n    {\n        printf(\"Argument passed from Nim \u003e %s\\n\", input);\n    }\n    ```\n\n2. Now, you need to create some bindings in Nim itself, here is an example:\n\n    \u003e **`binds.nim`**\n    ```nim\n    # Include C file to compile.\n    {.compile: \"funcs.c\"}\n\n    # Bindings for our C Functions.\n    proc foo* : void {.importc: \"foo\", header: \"funcs.h\"}\n    proc bar*(input: cstring) : void {.importc: \"bar\", header: \"funcs.h\"}\n    ```\n    1. `{.compile: \"file.c\"}` \u003e Tells the Nim compiler to include this file during compilation.                 \n\n    2. `{.importc: \"func\", header: \"file.h\"}` \u003e Fetches the function itself. Assigning it to a procedure makes it usable in your Nim files.\n\n3. Now, you can simply invoke those procedures like any other Nim procedure/function!\n   \n    ```nim\n    # Fetch bindings\n    import binds\n\n    # Run Code.\n    if isMainModule:\n        foo() # -\u003e void foo()\n        bar(\"Hello from Nim!\") # -\u003e void bar(char *input)\n    ```\n\n4. Compile using:\n   ```powershell\n   nim --run -f c main.nim\n   ```\n\n5. Output\n    ```\n    No arguments passed from Nim \u003e Hello from C!\n    Argument passed from Nim \u003e Hello from Nim!\n    ```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faetopia%2Fc-functions-in-nim","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Faetopia%2Fc-functions-in-nim","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faetopia%2Fc-functions-in-nim/lists"}