I have problem with import custom static lib c++ on window 10. My custom library use function c++ on Window (AtlCom.h, UIAutomation.h ) so I need use Visual Code to build them. I build dynamic library with c++ on Visual Code and it working . But when I change to use dynamic, I got error
Go version:
$ go version
go version go1.20.5 windows/amd64
Error message:
PS C:\Users\Administrator\Desktop\static_lib\window> go run .\main.go
# command-line-arguments
C:\Program Files\Go\pkg\tool\windows_amd64\link.exe: running gcc failed: exit status 1
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\ADMINI~1\AppData\Local\Temp\2\go-link-1866661389\000001.o: in function `_cgo_4edeb0dd26e3_Cfunc_hello_world':
/tmp/go-build/main.cgo2.c:49: undefined reference to `__imp_hello_world'
collect2.exe: error: ld returned 1 exit status
This is my c++ code :
// file pch.h
#ifndef PCH_H
#define PCH_H
#ifdef MY_WINDOW_EXPORTS
#define MY_API __declspec(dllexport)
#else
#define MY_API __declspec(dllimport)
#endif
// add headers that you want to pre-compile here
#include "framework.h"
#ifdef __cplusplus
extern "C" {
#endif
// Your prototype or Definition
MY_API const char* hello_world();
#ifdef __cplusplus
}
#endif
#endif //PCH_H
// pch.cpp: source file corresponding to the pre-compiled header
#include "pch.h"
// When you are using pre-compiled headers, this source file is necessary for compilation to succeed.
const char* hello_world()
{
const char *message = "hello world";
return message;
}
Golang code:
package main
/*
#cgo CFLAGS: -I.
#cgo LDFLAGS: -L./x64/Release -lmywindow
#include "pch.h"
*/
import "C"
import "fmt"
func main() {
msg := C.hello_world()
fmt.Println("Message from static lib : ", msg)
}
When use dynamic library, I copy binary to other computer and got error can't find atl library
So I want to build static library to run my code on other computer don't need install atl library
So please help me to fix it
extern "C"
in cpp as your comment, It still not work