{"id":207115,"date":"2025-07-08T08:34:02","date_gmt":"2025-07-08T00:34:02","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207115\/"},"modified":"2025-07-08T08:34:02","modified_gmt":"2025-07-08T00:34:02","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8-cmake-%e5%b0%86-go-%e4%bb%a3%e7%a0%81%e7%bc%96%e8%af%91%e5%b9%b6%e9%93%be%e6%8e%a5%e5%88%b0-c","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207115\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528 CMake \u5c06 Go \u4ee3\u7801\u7f16\u8bd1\u5e76\u94fe\u63a5\u5230 C++"},"content":{"rendered":"<p><b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>\u5982\u4f55\u4f7f\u7528 CMake \u5c06 Go \u4ee3\u7801\u7f16\u8bd1\u5e76\u94fe\u63a5\u5230 C++<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-20 09:12:31<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u672c\u7bc7\u6587\u7ae0\u7ed9\u5927\u5bb6\u5206\u4eab\u300a\u5982\u4f55\u4f7f\u7528 CMake \u5c06 Go \u4ee3\u7801\u7f16\u8bd1\u5e76\u94fe\u63a5\u5230 C++\u300b\uff0c\u8986\u76d6\u4e86Golang\u7684\u5e38\u89c1\u57fa\u7840\u77e5\u8bc6\uff0c\u5176\u5b9e\u4e00\u4e2a\u8bed\u8a00\u7684\u5168\u90e8\u77e5\u8bc6\u70b9\u4e00\u7bc7\u6587\u7ae0\u662f\u4e0d\u53ef\u80fd\u8bf4\u5b8c\u7684\uff0c\u4f46\u5e0c\u671b\u901a\u8fc7\u8fd9\u4e9b\u95ee\u9898\uff0c\u8ba9\u8bfb\u8005\u5bf9\u81ea\u5df1\u7684\u638c\u63e1\u7a0b\u5ea6\u6709\u4e00\u5b9a\u7684\u8ba4\u8bc6(B \u6570)\uff0c\u4ece\u800c\u5f25\u8865\u81ea\u5df1\u7684\u4e0d\u8db3\uff0c\u66f4\u597d\u7684\u638c\u63e1\u5b83\u3002<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u6b63\u5728\u5c1d\u8bd5\u5c06 go \u4ee3\u7801\u7f16\u8bd1\u5230\u5e93\u4e2d\uff0c\u7136\u540e\u4f7f\u7528 cmake \u94fe\u63a5 c++ \u548c lib\u3002 <\/p>\n<p> \u6211\u5df2\u7ecf\u5c1d\u8bd5\u8fc7\u4f7f\u7528 gcc \u5236\u4f5c\u5171\u4eab\u5e93\uff0c\u5982\u4e0b https:\/\/medium.com\/swlh\/build-and-use-go-packages-as-c-libraries-889eb0c19838\u3002 <\/p>\n<pre>go build -o cas.so -buildmode=c-shared cmd\/c-mock-server\/main.go\ngcc -o main main.c .\/cas.so<\/pre>\n<p>\u6240\u4ee5\u73b0\u5728\u6211\u9700\u8981\u521b\u5efa\u5171\u4eab\u5e93\u5e76\u4f7f\u7528 cmake \u5c06\u5176\u4e0e c++ \u4ee3\u7801\u94fe\u63a5\uff0c\u4f46\u6211\u771f\u7684\u4e0d\u77e5\u9053\u5982\u4f55\u505a\u5230\u8fd9\u4e00\u70b9<\/p>\n<p> <\/p>\n<h2>\u6b63\u786e\u7b54\u6848<\/h2>\n<p> <\/p>\n<p>\u5728\u4f7f\u7528 cmake \u4e4b\u524d\u6784\u5efa\u9879\u76ee\u7ed3\u6784\uff1a<\/p>\n<pre># tree .\n.\n\u251c\u2500\u2500 cmakelists.txt\n\u251c\u2500\u2500 go\n\u2502&nbsp;&nbsp; \u251c\u2500\u2500 cmakelists.txt\n\u2502&nbsp;&nbsp; \u251c\u2500\u2500 go.mod\n\u2502&nbsp;&nbsp; \u2514\u2500\u2500 module.go\n\u2514\u2500\u2500 test.cpp\n<\/pre>\n<h2>\u6587\u4ef6<\/h2>\n<h3><code>.\/cmakelists.txt<\/code><\/h3>\n<pre>cmake_minimum_required(version 3.0)\nproject(test)\n\nadd_subdirectory(go)\n\nset(target test)\nset(srcs test.cpp)\n\nadd_executable(${target} ${srcs})\ntarget_link_libraries(${target} goshim pthread)\n<\/pre>\n<h3><code>.\/test.cpp<\/code><\/h3>\n<pre>#include &lt;iostream&gt;\n#include &lt;string&gt;\n#include \"go\/module.h\"\n\nint main() {\n    int res = add(1, 2);\n    std::cout &lt;&lt; res &lt;&lt; std::endl;\n\n    gostring str;\n    str.p = \"hello world\";\n    str.n = strlen(str.p);\n    print(str);\n\n    std::string s = \"go-module\";\n    char *cstr = new char[s.length()+1];\n    std::strcpy (cstr, s.c_str());\n\n    cstr = strfxn(cstr);\n    std::cout &lt;&lt; cstr &lt;&lt; std::endl;\n    delete[] cstr;\n\n    return 0;\n}\n<\/pre>\n<h3><code>.\/go\/cmakelists.txt<\/code><\/h3>\n<pre>cmake_minimum_required(version 3.0)\nproject(test_go)\n\nset(target shim_go)\n\nset(srcs module.go)\nset(lib module.so)\n\nadd_custom_command(output ${cmake_current_binary_dir}\/${lib}\n  depends ${srcs}\n  working_directory ${cmake_current_source_dir}\n  command env gopath=${gopath} go build -buildmode=c-archive\n  -o \"${cmake_current_binary_dir}\/${lib}\"\n  ${cmake_go_flags} .\/...\n  comment \"building go library\")\n\nadd_custom_target(${target} depends ${lib} ${header})\nadd_library(goshim static imported global)\nadd_dependencies(goshim ${target})\nset_target_properties(goshim\n  properties\n  imported_location ${cmake_current_binary_dir}\/${lib}\n  interface_include_directories ${cmake_current_binary_dir})\n<\/pre>\n<h3><code>.\/go\/module.go<\/code><\/h3>\n<pre>package main\n\nimport \"c\"\n\nimport (\n    \"sort\"\n    \"fmt\"\n)\n\n\/\/export add\nfunc add(a, b int) int {\n    return a + b\n}\n\n\/\/export sub\nfunc sub(a, b int) int {\n    return a - b\n}\n\n\/\/export print\nfunc print(str string) {\n    fmt.printf(\"go prints: %s\\n\", str)\n}\n\nfunc strfxn(input string) string {\n    return \"hello \" + input + \" world\"\n}\n\n\/\/export strfxn\nfunc strfxn(cinput *c.char) *c.char {\n    \/\/ c data needs to be manually managed in memory.\n    \/\/ but we will do it from c++.\n    input := c.gostring(cinput)\n    return c.cstring(strfxn(input))\n}\n\n\/\/export sort\nfunc sort(vals []int) {\n    sort.ints(vals)\n}\n\nfunc main() {\n    \/\/ we need the main function to make possible\n    \/\/ cgo compiler to compile the package as c shared library\n}\n<\/pre>\n<h3><code>.\/go\/go.mod<\/code><\/h3>\n<pre>total 16\nmodule demo\/module\n\ngo 1.16\n<\/pre>\n<h2>\u8fd0\u884c<\/h2>\n<pre># cmake .\n-- The C compiler identification is AppleClang 12.0.0.12000032\n-- The CXX compiler identification is AppleClang 12.0.0.12000032\n-- Detecting C compiler ABI info\n-- Detecting C compiler ABI info - done\n-- Check for working C compiler: \/Library\/Developer\/CommandLineTools\/usr\/bin\/cc - skipped\n-- Detecting C compile features\n-- Detecting C compile features - done\n-- Detecting CXX compiler ABI info\n-- Detecting CXX compiler ABI info - done\n-- Check for working CXX compiler: \/Library\/Developer\/CommandLineTools\/usr\/bin\/c++ - skipped\n-- Detecting CXX compile features\n-- Detecting CXX compile features - done\n-- Configuring done\n-- Generating done\n-- Build files have been written to: \/tmp\/demo\n\n#  make\n[ 33%] Building Go library\n[ 33%] Built target shim_go\n[ 66%] Building CXX object CMakeFiles\/test.dir\/test.cpp.o\n[100%] Linking CXX executable test\n[100%] Built target test\n\n# .\/test\n3\nGo prints: Hello World\nHello Go-Module World\n<\/pre>\n<p>\u4eca\u5929\u5173\u4e8e\u300a\u5982\u4f55\u4f7f\u7528 CMake \u5c06 Go \u4ee3\u7801\u7f16\u8bd1\u5e76\u94fe\u63a5\u5230 C++\u300b\u7684\u5185\u5bb9\u5c31\u4ecb\u7ecd\u5230\u8fd9\u91cc\u4e86\uff0c\u662f\u4e0d\u662f\u5b66\u8d77\u6765\u4e00\u76ee\u4e86\u7136\uff01\u60f3\u8981\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u7684\u5185\u5bb9\u8bf7\u5173\u6ce8\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-207115","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207115","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=207115"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207115\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207115"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207115"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207115"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}