Calling C + + from Golang with Swig--windows DLL (iii)

Source: Internet
Author: User

calling C + + from Golang with Swig --windows dll Three

Use the dynamic link library ( DLL) There are two main ways: one is to import the library through a link, call the function in the DLL directly in the code, and the other with the help of Loadlibrary/loadlibraryex, the getprocessaddress function calls the functions in the DLL indirectly in the code . These two modes of use correspond to two dynamic links, called: load-time dynamic Link (dynamically linked at load time ) and Run-time dynamic Link (runtime dynamically linked).

Dllgenerated after the project is compiled*.dllfile and the same name. Libfiles,. LibThe file is an import library that can be explicitly called in code by using an application project that is linked at load timeDLLand must be used to import the library*.liblinks. When the system launches a program that uses dynamic linking when it is loaded, it uses the information that the linker places in the file to locate theDLLname in (executable program import) (DLLexport function). Then the system searchesDLLfile. If the system cannot find the requiredDLL, the process is terminated and a dialog box is displayed to the user that reports the error. Otherwise, the system willDLLmap to the virtual address space of the process and increaseDLLThe reference count. The system calls the entry point function (end-point function). If the entry point function does not returnTRUE, the system terminates the process and reports an error. Finally, the system uses the importedDLLfunction Start Address to modify the function Address table. (SeeMSDN:

https://msdn.microsoft.com/en-us/library/windows/desktop/ms684184 (v=vs.85). aspx)

Use Run-time dynamic linking when an application callsLoadLibraryorLoadLibraryExfunction, the system tries to findDLL. If the search succeeds, the system willDLLThe module maps to the virtual address space of the process and increments the reference count. The system then calls theLoadLibraryorLoadLibraryExinvokes the entry point function in the thread context of the After successLoadlibrayorLoadLibraryExreturns a pointer toDLLhandle. Process can beGetProcAddress,FreeLibraryThis handle is used in function calls to identifyDLL. Process Callgetprocessaddressget aDLLthe address of the exported function. WhenDLLwhen the module is no longer needed, the process can callFreeLibraryorFreelibraryexandexitthread. These functions will reduce the module reference count and Unmap from the virtual address space of the processDLLcode. (SeeMSDN:https://msdn.microsoft.com/en-us/library/windows/desktop/ms682596 (v=vs.85). aspx)

You can use the above two dynamic links in the golang using c++ DLL go standard library "Syscall" provides the syscall. Loadlibrary,syscall. Getprocaddress,syscall. Syscall series functions for easy runtime dynamic linking, see godoc view syscall

when dll exported c++ swig implementation golang call C + + DLL . ( environment windows 7" 64 bit TDM-GCC x64)

with VS- The wizard creates a simple dynamic-link library Simple.dll

The main code is as follows:

Simple.h File

#ifdef Simple_exports

#define SIMPLE_API __declspec (dllexport)

#else

#define SIMPLE_API __declspec (dllimport)

#endif

This class is exported from the Simple.dll

Class Simple_api Csimple {

Public

Csimple (void);

Todo:add your methods here.

void SayHello ();

};

extern Simple_api int nsimple;

Simple_api int fnsimple (void);

Simple.cpp file :

Simple.cpp:Defines the exported functions for the DLL application.

//

#include "stdafx.h"

#include "Simple.h"

#include <iostream>

using namespace Std;

This is a example of an exported variable

/*simple_api*/int nsimple=0;

This is a example of an exported function.

/*simple_api*/int fnsimple (void)

{

Return 42;

}

This is the constructor of a class which has been exported.

See Simple.h for the class definition

Csimple::csimple ()

{

Return

}

void Csimple::sayhello ()

{

cout<< "Hello World" <<endl;

}

this simple DLL Exports a variable, a function, and a class that contains a constructor and a SayHello method. Use Dependency Walker to view Simple.dll files:

Simple.dll exported 5 function (variable), previously described c++ Name mangling undname.exe View undecorated name The extra function is csimple =

Next, create a simple go program to invoke Simple.dll, the project directory is D:\GoSimple, under which the directory is created Simple folder, copy the Simple.dll and Simple.h files to the D:\GoSimple\ Simple , then create an input file for the Swig program simple.i

on the command line, switch to D:\GoSimple\simple, executive swig-c++-go-cgo-intgosize simple.i

two files generated after successful simple_wrap.cxx and simple.go

Open simple.go, add Link item

#cgo CFLAGS:-I.

#cgo ldflags:-l. -lsimple

in the Create a simple main.go file in D:\GoSimple to call the DLL

Go build compiles, you can see a bunch of errors:

All is a link error and the definition cannot be found. The first implementation of Go call c++ dll go using the GCC compiler, GCC name mangling visual C + + compiler name mangling Different, you need to dll add a copy of the file according to g++ name mangling

according to DLL export function,Dependency Walker,go build error prompt, previously mentioned C + + name mangling related content, you can sort out the following relationships:

Name of function

Visual C + + decorated name

g++ Mangled Name

Nsimple

[Email protected]@3ha

Nsimple

Fnsimple

[Email protected] @YAHXZ

_z8fnsimplev

Csimple::sayhello

[Email protected]@ @QEAAXXZ

_zn7csimple8sayhelloev

Csimple::csimple

[Email Protected]@[email protected]

_zn7csimplec1ev

we need to use A. def file to Add a copy of the symbol used by g++ to the exported items section of the DLL file . add Simple.def to the simple DLL project ,

after recompiling, use Dependency Walker again to view Simple.dll

IncreaseDefThe purpose of the file is to add an alias to the function entry point, such as[email protected] @YAHXZand the_z8fnsimplevThe corresponding entry point is the same as0x0000102d,[email protected] @YAHXZis aVisual C + + mangled name,_z8fnsimplevis aGCC Mangled name. GolangUseGCCcompilingC + +code, forDLLwhen you add an export item, you can resolve a link error that is not defined by the function.

Update The Simple.dll of the D:\GoSimple\simple directory , as well as a copy of the D:\GoSimple Simple.dll

Go to build again , compile successfully.

Run

Note: Before 1.go build, you will need to copy a Simple.dll in D:\GoSimple , otherwise an error can occur:

2. The Simple.lib cannot be copied to the go program directory, otherwise an error will occur:

Calling C + + from Golang with Swig--windows DLL (iii)

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.