2

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

2
  • Did you tried extern "C" in the source CPP file also?
    – XPD
    Commented Aug 8, 2023 at 6:45
  • I tried to add extern "C" in cpp as your comment, It still not work
    – Ho Ang Anh
    Commented Aug 8, 2023 at 7:21

1 Answer 1

0

Compared to call c-static-lib, calling cxx-static-lib in go has four differences:

  1. The header file must include extern "C" { ... } to be compatible with C++ compilers.

Like this:

#ifndef HI_H
#define HI_H

#ifdef __cplusplus
extern "C" {
#endif

void print_hi();

#ifdef __cplusplus
}
#endif

#endif // HI_H

  1. The source file must include the header file.

Like this:

#include <iostream>
#include "hi.h"

void print_hi() {
    std::cout << "hi, from C++\n";
}

  1. The LDFLAGS here has an extra -l stdc++ to link the C++ standard library.

Like this:

package main

/*
#cgo CFLAGS: -I ${SRCDIR}/lib
#cgo LDFLAGS: -L ${SRCDIR}/lib -l hi -l stdc++
#include "hi.h"
*/
import "C"

func main() {
    C.print_hi()
}

  1. Compile with g++

Like this:

g++ -c ./lib/hi.cpp -o ./lib/hi.o
ar rcs ./lib/libhi.a ./lib/hi.o

Source Code

Much More Details

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.