{"id":16572738,"url":"https://github.com/lemire/swiftcallingcheader","last_synced_at":"2025-10-29T02:30:22.275Z","repository":{"id":66054959,"uuid":"69494827","full_name":"lemire/SwiftCallingCHeader","owner":"lemire","description":"Calling a C header from Swift (example)","archived":false,"fork":false,"pushed_at":"2024-09-26T14:57:56.000Z","size":9,"stargazers_count":9,"open_issues_count":0,"forks_count":2,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-02-01T21:11:12.438Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Swift","has_issues":true,"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/lemire.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}},"created_at":"2016-09-28T19:06:09.000Z","updated_at":"2024-09-26T14:58:00.000Z","dependencies_parsed_at":"2023-10-31T22:00:12.340Z","dependency_job_id":null,"html_url":"https://github.com/lemire/SwiftCallingCHeader","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lemire%2FSwiftCallingCHeader","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lemire%2FSwiftCallingCHeader/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lemire%2FSwiftCallingCHeader/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lemire%2FSwiftCallingCHeader/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lemire","download_url":"https://codeload.github.com/lemire/SwiftCallingCHeader/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":238758231,"owners_count":19525730,"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":"2024-10-11T21:28:28.719Z","updated_at":"2025-10-29T02:30:16.993Z","avatar_url":"https://github.com/lemire.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"## Calling a C header from Swift (example) \n\nI could not find a simple example of Swift code calling a C header. So I made one.\n\nUsage:\n\n```\nswift build\n.build/debug/SomeSwift\n```\nor\n\n```\nswift build --configuration release\n.build/release/SomeSwift\n```\n\n## Explanation:\n\n- Each subdirectory in \"Sources\" is effectively a subpackage.\n- Create a package with your C code in it (``Sources/SomeC``).\n- Create your Swift package (``Sources/SomeSwift``).\n- Then establish a dependency in your ``Package.swift`` file : ``targets: [Target(name: \"Bitset\", dependencies: [\"SwiftBitsetC\"]),]``.\n- In your C package, add an include directory with your header file. If you have C source code, put it directly in your C code package directory (``Sources/SomeC``). You need at least one source file (``somec.c``).\n- In your Swift code, just do ``import SomeC`` and then you can call the C function as in ``SomeC.sayHello()``.\n- Official apple document about this topic: https://www.swift.org/documentation/articles/wrapping-c-cpp-library-in-swift.html\n\nThat's it!\n\n## For Xcode users (Mac only)\n\n```bash\n$ swift package generate-xcodeproj\ngenerated: ./SwiftCallingCHeader.xcodeproj\n$ open ./SwiftCallingCHeader.xcodeproj\n```\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flemire%2Fswiftcallingcheader","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flemire%2Fswiftcallingcheader","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flemire%2Fswiftcallingcheader/lists"}