Reputation: 11
I am trying to use a C static library file in a golang code using cgo module. I am working on a project in which I had to use C code with golang, so the solution was to use the cgo module. However, I was unable to do so, so I started trying to see whether I can do so in a separate smaller project. However the same issue came up as well when trying to use a static library file with golang code.
Here's my golang source code.
package main
import "fmt"
/*
#include<stdio.h>
#include "add.h"
#cgo CFLAGS: -I .
#cgo LDFLAGS: -fPIC -L. -laddLogic
void addT()
{
int a = 1;
int b = 2;
int c;
c = add(a,b);
printf("%d", c);
}
*/
import "C"
func main() {
fmt.Println("hello world")
C.addT()
}
Here's my one liner header file
int add(int a, int b);`
Here's my simple logic for the add method from which I made the static library file.
#include "add.h"
int add(int a, int b)
{
return a+b;
}
All of these three files names are - test.go, add.h and addLogic.lib. All of these files are in the same directory.
This is the error which is coming up.
# _/C_/static_lib/test
C:/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping incompatible ./addLogic.lib when searching for -laddLogic
C:/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping incompatible ./addLogic.lib when searching for -laddLogic
C:/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping incompatible .\addLogic.lib when searching for -laddLogic
C:/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -laddLogic
C:/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping incompatible ./addLogic.lib when searching for -laddLogic
collect2.exe: error: ld returned 1 exit status`
I made the static library file using the method and commands described in this link. I could have used visual studio, however it was not working on my system, so I used VS developer console to make the static lib file as described below.
https://www.zealfortechnology.com/2013/08/create-c-program-static-library-command-line-windows.html
Here is my golang version - go version go1.11.1 windows/amd64
My gcc compiler :-
gcc (tdm64-1) 10.3.0
Copyright (C) 2020 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
My Go environment Information
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\Divyansh\AppData\Local\go-build
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOOS=windows
set GOPATH=C:\Users\Divyansh\go
set GOPROXY=
set GORACE=
set GOROOT=C:\Go
set GOTMPDIR=
set GOTOOLDIR=C:\Go\pkg\tool\windows_amd64
set GCCGO=gccgo
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0 -fdebug-prefix-map=C:\Users\Divyansh\AppData\Local\Temp\go-build390078462=/tmp/go-build -gno-record-gcc-switches
The operating system being used is Windows 10
The only question on static overflow regarding skipping incompatible which I was able to find was this one - https://stackoverflow.com/questions/3119714/skipping-incompatible-libraries-at-compile However this was not able to solve my problem.
I was expecting that the static library files would be successfully linked and the go code would be built into an exe file when I run the command - go build test.go
Upvotes: 1
Views: 122
Reputation: 11
Ok, I found out that instead of using .lib files, if we use .a files then it links and works perfectly fine. These are the commands I used to convert a C source file to .a file.
First we converted the C source file to an object file :- gcc -c libaddLogic.c -o libaddLogic.o
Then, that object file was converted to a .a file.
ar rcs libaddLogic.a libaddLogic.o
This series of commands gave us a .a file which can then be statically linked.
However, what I would like to know now is that, why was I not able to use .lib file. What was the reason behind not being able to use .lib file using a .a file instead.
Upvotes: 0