Compile DLL with static library using gcc (mingw32)
php editor Baicao introduces you how to use gcc (mingw32) to compile a DLL with a static library. During the development process, it is often necessary to package static libraries into DLLs for easy calling in other projects. The method of using gcc (mingw32) to compile a DLL with a static library is relatively simple, just follow certain steps. First, make sure you have mingw32 and gcc compilers installed. Then, enter gcc -shared -o libname.dll libname.a on the command line to generate the DLL file. This way you can easily compile the static library into a DLL for use in other projects.
Question content
I have a static library generated by an external tool (i.e. cgo), let's call it libsecondary.a. I want to generate a dynamic library while including "libsecondary.a" as a dependency, I export a function called onprocessinit() in libsecondary.h and call it on the dll_process_attach event.
I tried generating the shared library but it doesn't seem to work x86_64-w64-mingw32-share-l. -lsecondary -static-libgcc -static-libstdc -static .\dllmain.c
The error output is dllmain.c:(.text 0x9b): Undefined reference to 'onprocessinit', what's going on?
This is the header file libsecondary.h
/* code generated by cmd/cgo; do not edit. */ /* package command-line-arguments */ #line 1 "cgo-builtin-export-prolog" #include <stddef.h> #ifndef go_cgo_export_prologue_h #define go_cgo_export_prologue_h #ifndef go_cgo_gostring_typedef typedef struct { const char *p; ptrdiff_t n; } _gostring_; #endif #endif /* start of preamble from import "c" comments. */ /* end of preamble from import "c" comments. */ /* start of boilerplate cgo prologue. */ #line 1 "cgo-gcc-export-header-prolog" #ifndef go_cgo_prologue_h #define go_cgo_prologue_h typedef signed char goint8; typedef unsigned char gouint8; typedef short goint16; typedef unsigned short gouint16; typedef int goint32; typedef unsigned int gouint32; typedef long long goint64; typedef unsigned long long gouint64; typedef goint64 goint; typedef gouint64 gouint; typedef size_t gouintptr; typedef float gofloat32; typedef double gofloat64; #ifdef _msc_ver #include <complex.h> typedef _fcomplex gocomplex64; typedef _dcomplex gocomplex128; #else typedef float _complex gocomplex64; typedef double _complex gocomplex128; #endif /* static assertion to make sure the file is being used on architecture at least with matching size of goint. */ typedef char _check_for_64_bit_pointer_matching_goint[sizeof(void*)==64/8 ? 1:-1]; #ifndef go_cgo_gostring_typedef typedef _gostring_ gostring; #endif typedef void *gomap; typedef void *gochan; typedef struct { void *t; void *v; } gointerface; typedef struct { void *data; goint len; goint cap; } goslice; #endif /* end of boilerplate cgo prologue. */ #ifdef __cplusplus extern "c" { #endif extern __declspec(dllexport) void onprocessinit(); #ifdef __cplusplus } #endif
This is dllmain.c
65be0f35ebbcbcThis is the exported golang function (the function I compiled using go build -buildmode=c-archive)
package main import "C" import ( "unsafe" "syscall" ) //export OnProcessInit func OnProcessInit() { const ( NULL = 0 MB_OK = 0 ) caption := "Hola" title := "desdegoo" ret, _, _ := syscall.NewLazyDLL("user32.dll").NewProc("MessageBoxW").Call( uintptr(NULL), uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(caption))), uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(title))), uintptr(MB_OK)) if ret != 1 { return } return } func main() {}
Solution
Wow, the answer is argument position,
x86_64-w64-mingw32 -shared -static-libgcc -static-libstdc -static .\dllmain.c .\libsecondary.a
If you type it backwards, it won't find the reference from libsecondary.a, OMG...
The above code will also get into a deadlock when loading, because syscall.NewLazyDLL calls LoadLibraryA, and it is locked in DLL_PROCESS_ATTACH, so the solution is to CreateThread and run the golang exported function inside the thread :)
The above is the detailed content of Compile DLL with static library using gcc (mingw32). For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Do not change the meaning of the original content, fine-tune the content, rewrite the content, do not continue. Redmi Note13RPro not only has excellent performance, but also has an even better user experience. In order to give everyone a more comfortable experience, Redmi Note13RPro is equipped with a font adjustment function to allow users to Ability to adjust fonts for your phone. If you want to know how to adjust the font size of Redmi Note13R Pro, then take a look. How to adjust the font size on Redmi Note13RPro? 1. Open the Settings app. 2. Scroll to the bottom and select "Display." 3. Click "Text Size". 4. On this screen, you can adjust the font size by dragging the slider or select a preset font size. 5. After the adjustment is completed, press "OK" to save

typedef struct is used in C language to create structure type aliases to simplify the use of structures. It aliases a new data type to an existing structure by specifying the structure alias. Benefits include enhanced readability, code reuse, and type checking. Note: The structure must be defined before using an alias. The alias must be unique in the program and only valid within the scope in which it is declared.

The phone Redmi Note13RPro has been very popular recently. Many consumers have purchased this phone. However, many users are using this phone for the first time, so they don’t know how to connect the Redmi Note13RPro to the computer. In this regard, the editor is here to explain to you Detailed tutorial introduction is provided. How to connect Redmi Note13RPro to the computer? 1. Use a USB data cable to connect the Redmi phone to the USB interface of the computer. 2. Open the phone settings, click Options, and turn on USB debugging. 3. Open the device manager on your computer and find the mobile device option. 4. Right-click the mobile device, select Update Driver, and then select Automatically search for updated drivers. 5. If the computer does not automatically search for the driver,

The Redmi Note13RPro mobile phone integrates a number of smart tools in terms of software functions. Among them, quickly and accurately extracting text content from pictures is one of them. The following editor will introduce to you how Redmi Note13RPro extracts text from pictures. How to extract text from pictures on Redmi Note13RPro? Use the Xiaomi QR code scanning function, open the QR code scanning application on your phone, click the picture icon, select a picture, and then click the "Recognize text" option on the right to successfully extract the text in the picture. Operate through mobile phone album. Find the picture for which text needs to be extracted in the mobile phone album, click "More" below the picture, and select "Extract Text". After successful recognition, you can copy or save the text as needed. Use WeChat mini programs. Open micro

Entire Huoba is a very fun casual puzzle game that everyone can experience on multiple platforms! The game provides countless exciting levels waiting to be unlocked. Each level is full of novelty and challenges, allowing you to experience an interesting adventure of answering questions and unlock new game content. So how to clear the entire level of Huoba? Today I bring you a complete guide to all levels of "Whole Life". If you encounter any problems in passing the level, you can refer to it! "The Whole Life" complete guide to all levels of "The Whole Life" The complete guide to all levels of "The Whole Life" Help her to watch the New Year's Eve, help the little sister to identify the scumbag wolf, and the grandmother helps the little girl escape from danger for outdoor adventure, pretending not to see Ah Piao and transforming back The humanoid helped the orangutan to become a humanoid. Someone in the back seat stayed calm and dealt with the female ghost. The night-shift taxi kept calm and dealt with the female ghost.

Entire Huoba is a very popular fun level-breaking game on Douyin. There are many levels in the game waiting for everyone to challenge! In the game level "Journey to the West Beauty", everyone needs to help the female character of "Journey to the West" return to her position. How to pass the level? The following is a guide for you to clear the level of "Journey to the West" brought to you by the beauty of Journey to the West. Friends who don't know how to pass the level, let's take a look. I hope it can help you. "Whole Life" Journey to the West beauty helps the female character of Journey to the West to return to her place. Strategy for clearing the stage. The beauty of Journey to the West helps the female character of Journey to the West to return to her place. 1. Click on the scroll to appear the female character. 2. The white-bone spirit and the peacock spirit need to click to switch actions. 3. The answer is as shown in the figure below: "The whole life" Let's Live" has a complete guide to all levels, helping her to be a watcher of the year, helping the little sister to identify the scumbag and the wolf, and the grandmother to help the little girl escape from danger and have outdoor adventures.

colspan is a table element attribute in HTML that specifies the number of columns that a cell spans. By using the colspan attribute, you can combine cells into wider columns, improving table readability, maintainability, and creating a more flexible layout.

Entire Huoba is a very popular fun level-breaking game on Douyin. There are many levels in the game waiting for everyone to challenge! In the game, you need to persuade all the neighbors to go downstairs in the level "Rescue the Neighbors". How do you pass the level? The following is the guide for saving the neighbors and urging all the neighbors to go downstairs to pass the level brought to you by the editor. Friends who don’t know how to pass the level, let’s take a look together. I hope it can help you. "Whole Life" Saving the Neighbors and Advising All the Neighbors to Go Downstairs Clearance Strategy Saving the Neighbors and Encouraging All the Neighbors to Go Downstairs 1. Someone downstairs needs first aid 2. The bookstore downstairs sells out-of-print learning materials 3. There are many cardboard boxes in the trash can downstairs 4. I Agree to the date, you go downstairs first 5. Your brother came to our community to promote 6. Someone discovered a new fishing spot 7. Drag the canned cat downstairs 8. Drag the fork to
