{"id":20044056,"url":"https://github.com/rockfordwei/cswift","last_synced_at":"2025-05-05T08:32:49.076Z","repository":{"id":63920411,"uuid":"79238471","full_name":"RockfordWei/CSwift","owner":"RockfordWei","description":"C Module for Swift, Swift Script and Dynamic Library Call;Swift中直接调用C语言源程序的展示程序","archived":false,"fork":false,"pushed_at":"2018-01-23T15:33:08.000Z","size":19,"stargazers_count":50,"open_issues_count":0,"forks_count":7,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-16T04:19:36.055Z","etag":null,"topics":["c","clang","dll","dynamic-programming","hotfix","module","spm","swift","swift-script"],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/RockfordWei.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-01-17T15:00:23.000Z","updated_at":"2024-04-13T09:52:55.000Z","dependencies_parsed_at":"2023-01-14T14:00:51.453Z","dependency_job_id":null,"html_url":"https://github.com/RockfordWei/CSwift","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RockfordWei%2FCSwift","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RockfordWei%2FCSwift/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RockfordWei%2FCSwift/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RockfordWei%2FCSwift/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/RockfordWei","download_url":"https://codeload.github.com/RockfordWei/CSwift/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252466936,"owners_count":21752464,"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":["c","clang","dll","dynamic-programming","hotfix","module","spm","swift","swift-script"],"created_at":"2024-11-13T10:59:44.582Z","updated_at":"2025-05-05T08:32:48.659Z","avatar_url":"https://github.com/RockfordWei.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# C Module for Swift, Swift Script and Dynamic Library Call [简体中文](README.zh_CN.md)\n\nRockford Wei，2017-01-17\n\nLast update: 2018-01-23\n\nThis project demonstrates how to call a customized C library in your swift project.\nYou can clone it from github, or follow the instruction below to generate it step by step.\n\nFurthermore, this demo also shows how to use swift as a script, with examples of calling C api as dynamic libraries.\nThis practice makes it possible to patch hot fixes for server side swift without stopping the server, theoretically.\n\n## Introduction\n\nThere are two C files: CSwift.c and CSwift.h. The objective is to build a CSwift library and export it to Swift.\n\n## Quick Start\n\nPlease compile this project with Swift 4.0.3 toolchain.\n\n### Build \u0026 Test\n\n```\n$ git clone https://github.com/RockfordWei/CSwift.git\n$ cd CSwift\n$ swift build\n$ swift test\n```\n\nThe sources are written in C while the test program is written in Swift. So if all tests passed, then congratulations! You've already mastered the process to call C api in a Swift source.\n\n## Walk Through\n\nHowever, even without the sources above, you can still start everything from blank:\n\n### Start From An Empty Folder:\n\nAssume the objective library is still CSwift, then find an empty folder and try these commands in a terminal:\n\n```\nmkdir CSwift \u0026\u0026 cd CSwift \u0026\u0026 swift package init\nmkdir Sources/CSwift/include \u0026\u0026 rm Sources/CSwift/CSwift.swift\n```\n\nThe above commands will set up the empty project template\n\n### C Header File\n\nNow edit the header file `Sources/CSwift/include/Swift.h`:\n\n``` c\nextern int c_add(int, int);\n#define C_TEN 10\n```\n\n### C Source\n\nFinishing the implementation of the C body `Sources/CSwift/CSwift.c`:\n\n``` c\n#include \"include/CSwift.h\"\nint c_add(int a, int b) { return a + b ; }\n```\n\n### Module Map\n\nNext, we will setup an umbrella file for swift: \n`Sources/CSwift/include/module.modulemap`\n\n``` swift\nmodule CSwift [system] {\n  header \"CSwift.h\"\n  export *\n}\n```\n\n### Call C API in Swift\n\nNow let's check if the library works by editing a test script:\n`Tests/CSwiftTests/CSwiftTests.swift`\n\n``` swift\nimport XCTest\n@testable import CSwift\nclass CSwiftTests: XCTestCase {\n  func testExample() {\n    let three = c_add(1, 2)\n    XCTAssertEqual(three, 3)\n    XCTAssertEqual(C_TEN, 10)\n  }\n  static var allTests : [(String, (CSwiftTests) -\u003e () throws -\u003e Void)] {\n      return [  (\"testExample\", testExample)  ]\n  }\n}\n```\n\n### Test\n\nThe final step is the easiest one - build \u0026 test:\n\n```\n$ swift build\n$ swift test\n```\n\nIf success, then perfect!\n\n## Swift as Script and Call C lib dynamically\n\nBeside the above classic static build \u0026 run, Swift also provide an interpreter to execute swift source as scripts, just like a playground in a terminal.\nThis project also makes an example for swift script, and even more, introduces how to call the same C api dynamically in such a script.\n\n### Dynamic Link Library\n\nThe default linking object of Swift 4 is static, so it needs a bit modification to turn it into a dynamic one. \n\nTo do this, edit the Package.swift file, and add a line:\n\n``` swift\n.library(\n    name: \"CSwift\",\n    type: .`dynamic`,  // \u003c------------ Insert the dynamic type right here!\n    targets: [\"CSwift\"]),\n```\n\nPlease check a swift script `dll.swift.script`, actually it is a common swift with no difference to any other swift sources:\n\n``` swift\n// First thing first, make sure your dll path is an dynamic library in an ABSOLUTE path.\n// on Mac, the suffix is \".dylib\"; on Linux, it is \".so\"\nguard let lib = dlopen(dllpath,  RTLD_LAZY) else {\n  exit(0)\n}\n\n// declare the api prototype to call\ntypealias AddFunc = @convention(c) (CInt, CInt) -\u003e CInt\n\n// look up the function in the library\nguard let c_add = dlsym(lib, \"c_add\") else {\n  dlclose(lib)\n  exit(0)\n}\n\n// attache the function to the real API address\nlet add = unsafeBitCast(c_add, to: AddFunc.self)\n\n// call the C method, dynamically\nlet x = add(1, 2)\nprint(x)\n\n// release resources\ndlclose(lib)\n```\n\n### Run the Swift Script\n\nThis project also provides a bash script `dll.sh` to run the swift script above.\n```\n# step one, build the C library\nswift build\n\n# then test what OS it is: .dylib for apple and .so for linux\nif swift --version|grep apple\nthen\n  SUFFIX=dylib\nelse\n  SUFFIX=so\nfi\n\n# generate the full path of new library.\nDLL=$PWD/.build/debug/libCSwift.$SUFFIX\n\n# run the swift script and call the libray.\nswift dll.swift.script $DLL\n```\n\n## More Info\n\nIf Xcode is preferred, then try command `swift package generate-xcodeproj` before building.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frockfordwei%2Fcswift","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frockfordwei%2Fcswift","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frockfordwei%2Fcswift/lists"}