Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

x/tools/gopls: could not import C (no package for import C) when importing static libraries #40595

Closed
Zincr0 opened this issue Aug 5, 2020 · 5 comments

Comments

@Zincr0
Copy link

@Zincr0 Zincr0 commented Aug 5, 2020

What version of Go are you using (go version)?

$ go version
go version go1.14.6 linux/amd64

Does this issue reproduce with the latest release?

yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env

GO111MODULE="auto"
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/vscode/.cache/go-build"
GOENV="/home/vscode/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/goapp/app/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build968699808=/tmp/go-build -gno-record-gcc-switches"

What did you do?

I'm here from #35721 since it seems this particular case is not the same issue. Trying to import a static library using "import C" gives a "could not import C (no package for import C)" in vscode, the code however, can compile and run with no problems with go build.

sample go code:

package main

// #cgo CFLAGS: -g -Wall -I/usr/src/myapp
// #cgo LDFLAGS: /usr/src/myapp/libdoecho.a
// #include "libdoecho.h"
import "C"
import "unsafe"
import "fmt"

func test1() {
    aCstring := C.CString("Hello")
    res := C.doecho(aCstring)
    aGOstring := C.GoString(res)
    C.free(unsafe.Pointer(aCstring))
    C.freeThePointer(res)
    fmt.Println("%v", aGOstring)
}

sample .h code:

#ifndef libdoecho
#define libdoecho

#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>

char *doecho(const char *s);

void freeThePointer(char *s);

#endif /* libdoecho */

libdoecho.a is a library wich exposes the "doecho" and "freeThePointer" functions. A string enters and a string is returned.

What did you expect to see?

import C should not give the "could not import C (no package for import C)" error on vscode, since the code does compile and run without problems with go build.

What did you see instead?

A "could not import C (no package for import C)" error.

@ianlancetaylor
Copy link
Contributor

@ianlancetaylor ianlancetaylor commented Aug 5, 2020

What command did you run to produce that error?

@Zincr0
Copy link
Author

@Zincr0 Zincr0 commented Aug 5, 2020

I'm just opening the code on vscode
errorvscode

@heschik
Copy link
Contributor

@heschik heschik commented Aug 5, 2020

@heschik heschik changed the title could not import C (no package for import C) when importing static libraries x/tools/gopls: could not import C (no package for import C) when importing static libraries Aug 5, 2020
@gopherbot gopherbot added this to the Unreleased milestone Aug 5, 2020
@stamblerre stamblerre removed this from the Unreleased milestone Aug 5, 2020
@Zincr0
Copy link
Author

@Zincr0 Zincr0 commented Aug 5, 2020

I was preparing my system to get some logs when i copied the code to a new file and... there was no error, the same code on 2 files just with different test function name, the old one showing "could not import C" while the new one does not, so i deleted the old one and renamed the new one and bum! no more errors and the code now runs fine on vscode. Can this be a cache issue?, i updated recently to gopls 0.4.4. Sadly i can't get the logs now, but this issue may be useful to somebody else...

@ianlancetaylor
Copy link
Contributor

@ianlancetaylor ianlancetaylor commented Aug 6, 2020

Thanks. Closing the issue since it is working now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
5 participants
You can’t perform that action at this time.