{"id":15034755,"url":"https://github.com/arrieta/golang-cpp-basic-example","last_synced_at":"2025-10-09T01:35:13.910Z","repository":{"id":144267099,"uuid":"123183100","full_name":"arrieta/golang-cpp-basic-example","owner":"arrieta","description":"A simple example demonstrating how to call C++ from Go","archived":false,"fork":false,"pushed_at":"2022-03-11T00:35:43.000Z","size":8,"stargazers_count":99,"open_issues_count":1,"forks_count":21,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-09T22:52:09.512Z","etag":null,"topics":["cpp","golang-examples","shared-library"],"latest_commit_sha":null,"homepage":null,"language":"C++","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/arrieta.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}},"created_at":"2018-02-27T20:17:53.000Z","updated_at":"2025-02-23T20:27:15.000Z","dependencies_parsed_at":"2023-07-05T00:01:56.693Z","dependency_job_id":null,"html_url":"https://github.com/arrieta/golang-cpp-basic-example","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/arrieta%2Fgolang-cpp-basic-example","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arrieta%2Fgolang-cpp-basic-example/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arrieta%2Fgolang-cpp-basic-example/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arrieta%2Fgolang-cpp-basic-example/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/arrieta","download_url":"https://codeload.github.com/arrieta/golang-cpp-basic-example/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248125642,"owners_count":21051766,"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":["cpp","golang-examples","shared-library"],"created_at":"2024-09-24T20:26:13.964Z","updated_at":"2025-10-09T01:35:08.870Z","avatar_url":"https://github.com/arrieta.png","language":"C++","readme":"# Calling C++ from Go\n\nThis repo contains a simple example calling C++ from Go.\n\nSample run (tested on macOS High Sierra 10.13.3 running go version go1.10 darwin/amd64):\n\n```\n$ make\nclang++ -o liblibrary.so library.cpp library-bridge.cpp \\\n\t-std=c++17 -O3 -Wall -Wextra -fPIC -shared\n$ go run library.go\n[c++ bridge] LIB_NewFoo(42)\n[c++] Foo::Foo(42)\n[c++ bridge] LIB_NewFoo(42) will return pointer 0x42002e0\n[c++ bridge] LIB_FooValue(0x42002e0)\n[c++] Foo::value() is 42\n[go] 42\n[c++ bridge] LIB_DestroyFoo(0x42002e0)\n[c++] Foo::~Foo(42)\n```\n\nI can also build an executable\n\n```\n$ go build library.go\n$ ./library\n[c++ bridge] LIB_NewFoo(42)\n[c++] Foo::Foo(42)\n[c++ bridge] LIB_NewFoo(42) will return pointer 0x4500000\n[c++ bridge] LIB_FooValue(0x4500000)\n[c++] Foo::value() is 42\n[go] 42\n[c++ bridge] LIB_DestroyFoo(0x4500000)\n[c++] Foo::~Foo(42)\n```\n\n## Calling C++ from goroutines\n\nIn the `goroutines` folder I created a very simple example that dispatches\nmultiple cpu-intensive tasks, and many io-intensive tasks. It is pleasant that\nGo can coordinate the calls relatively well.\n\nSample run:\n\n```\n$ cd goroutines\n$ make fancy\nclang++ -o libfancy.so fancy.cpp \\\n\t-std=c++17 -O3 -Wall -Wextra -fPIC -shared\ngo build fancy.go\n$ ./fancy\n[go] dispatched all tasks\n[go] dispatching io_intensive\n[go] dispatching cpu_intensive( 45 )\n[go] dispatching cpu_intensive( 30 )\n[c++] starting to download NASA image on thread 0x7fff94f7d340.\n[c++] starting fib(45) on thread 0x7000006a0000.\n[go] dispatching cpu_intensive( 45 )\n[c++] starting fib(30) on thread 0x70000061d000.\n[go] dispatching cpu_intensive( 40 )\n[c++] starting fib(45) on thread 0x70000059a000.\n[c++] starting fib(40) on thread 0x7000008ac000.\n[go] dispatching cpu_intensive( 35 )\n[c++] starting fib(35) on thread 0x7000007a6000.\n[go] dispatching cpu_intensive( 35 )\n[go] dispatching cpu_intensive( 30 )\n[c++] starting fib(35) on thread 0x7000009b2000.\n[c++] starting fib(30) on thread 0x700000829000.\n[go] dispatching cpu_intensive( 30 )\n[c++] starting fib(30) on thread 0x700000a35000.\n[c++] fib(30) on thread 0x700000829000 took 0.003607 seconds.\n[go] dispatching cpu_intensive( 45 )\n[go] dispatching cpu_intensive( 45 )\n[c++] fib(30) on thread 0x70000061d000 took 0.003796 seconds.\n[go] dispatching cpu_intensive( 40 )\n[c++] starting fib(45) on thread 0x700000829000.\n[go] dispatching cpu_intensive( 40 )\n[c++] fib(30) on thread 0x700000a35000 took 0.003653 seconds.\n[go] dispatching cpu_intensive( 50 )\n[c++] starting fib(45) on thread 0x700000ab8000.\n[c++] starting fib(40) on thread 0x70000061d000.\n[c++] starting fib(40) on thread 0x70000092f000.\n[c++] starting fib(50) on thread 0x700000a35000.\n[go] dispatching cpu_intensive( 45 )\n[c++] starting fib(45) on thread 0x700000b3b000.\n[go] dispatching io_intensive\n[c++] starting to download NASA image on thread 0x700000bbe000.\n[go] dispatching cpu_intensive( 50 )\n[c++] starting fib(50) on thread 0x700000c41000.\n[go] dispatching cpu_intensive( 35 )\n[c++] starting fib(35) on thread 0x700000cc4000.\n[go] dispatching io_intensive\n[c++] starting to download NASA image on thread 0x700000d47000.\n[c++] fib(35) on thread 0x7000009b2000 took 0.063846 seconds.\n[go] dispatching cpu_intensive( 50 )\n[c++] starting fib(50) on thread 0x7000009b2000.\n[c++] fib(35) on thread 0x7000007a6000 took 0.067398 seconds.\n[go] dispatching cpu_intensive( 45 )\n[c++] starting fib(45) on thread 0x7000007a6000.\n[c++] fib(35) on thread 0x700000cc4000 took 0.054202 seconds.\n[c++] fib(40) on thread 0x70000061d000 took 0.707855 seconds.\n[c++] fib(40) on thread 0x7000008ac000 took 0.716816 seconds.\n[c++] fib(40) on thread 0x70000092f000 took 0.716797 seconds.\n[c++] fib(45) on thread 0x70000059a000 took 6.23767 seconds.\n[c++] fib(45) on thread 0x700000829000 took 6.23499 seconds.\n[c++] fib(45) on thread 0x7000006a0000 took 6.23971 seconds.\n[c++] fib(45) on thread 0x700000ab8000 took 6.2396 seconds.\n[c++] fib(45) on thread 0x700000b3b000 took 6.23648 seconds.\n[c++] fib(45) on thread 0x7000007a6000 took 6.1945 seconds.\n[c++] downloaded NASA image\n[c++] io_intensive on thread 0x7fff94f7d340 took 12.1647 seconds.\n[c++] downloaded NASA image\n[c++] io_intensive on thread 0x700000bbe000 took 23.5905 seconds.\n[c++] downloaded NASA image\n[c++] io_intensive on thread 0x700000d47000 took 32.3157 seconds.\n[c++] fib(50) on thread 0x700000a35000 took 33.1472 seconds.\n[c++] fib(50) on thread 0x700000c41000 took 33.1787 seconds.\n[c++] fib(50) on thread 0x7000009b2000 took 33.2035 seconds.\n[go] done (elapsed: 33.268694231 seconds)\n```","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Farrieta%2Fgolang-cpp-basic-example","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Farrieta%2Fgolang-cpp-basic-example","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Farrieta%2Fgolang-cpp-basic-example/lists"}