Golang Invoke Dynamic Library so

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed. Test dynamic Library test_so.h ' ' int test_so_func (int a,int b); "Test_so.c" #include "test_so.h" int test_so_func (int a,int b) { return a*b;} "Generate So" gcc-shared./test_so.c-o test_so.so "Copy so file to Golang project directory Golang project directory, establish load_so.h ' int do_test_so_func (int A,int b); "Load_so.c" #include "load_so.h" #include <dlfcn.h>int do_test_so_func (int a,int b) {void* handle; typedef int (*FPTR) (int,int); Handle = Dlopen ("./test_so.so", 1); Fptr fptr = (fptr) dlsym (handle, "Test_so_func"); int result = (*fptr) (A, b); return result;} ' Test.go ' package main/* #include "load_so.h" #cgo ldflags:-ldl*/import "C" import "FMT" Func Main () {FMT. Println ("20*30=", C.do_test_so_func (+)) fmt. Println ("Hello World")} ' compiles and runs. # # # #以上主要参考https://studygolang.com/articles/67, thank bloggers for sharing but for beginners, there may be some difficulty in configuring the project. Below do the following reference, only for beginners, don't spit groove!!!! [screenshot 2017-09-15 11.51.37.png] (https://static.studygolang.com/170915/570b45060b82862dfa053938d6e42543.png) Project structure configuration! [screenshot 2017-09-15 11.52.41.png] (https://static.studygolang.com/170915/4f85e598d785eb589bd28af515679fc4.png) Project Gopath Configuration! [screenshot 2017-09-15 11.53.03.png] (https://static.studygolang.com/170915/3e122db2f13b7b1576fa0f483908ce74.png) Run/debug configurations Configuration 2,181 reads  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.