1

Ubuntu. vscode 1.62.1. go1.17.3. vscode go extension v0.29.0. delve v1.7.1.

I'm trying to a build a small app that uses Cgo, using vscode and vscode-go. Only one module imports "C".

My project structure has the root directory containing the "go.mod" and "main.go" files, and I have subpackages in subfolders. I also have "include" and "lib" directories that contain the C artifacts.

This is what I have so far in the C module:

package voltage

// #cgo CFLAGS: -g -Wall -Iinclude
// #cgo LDFLAGS: -Llib/linux -lvibesimple -lcurl -lssl -lvibecrypto -lvibeictk -lvibeserver
// #include <stdio.h>
// #include <errno.h>
// #include "veapi.h"
import "C"

func Encrypt(datatype string, data string) (result string) {
    return
}

func Decrypt(datatype string, data string) (result string) {
    return
}

In the "Problems" view, it shows the following two issues:

go list failed to return CompiledGoFiles. This may indicate failure to perform cgo processing; try building at the command line. See https://golang.org/issue/38990.

And:

could not import C (cgo preprocessing failed) (compile)

I read the cited issue, but I'm not sure what to do with that information.

I'm not sure how to move forward here.

1 Answer 1

4

The C compiler is not executed in the source directory but in a temporary directory only containing intermediate files, such as your go files compiled as static libraries (.a). Therefore, the LDFLAG -Llib/linux points to an unexisting directory.

To solve this issue, just replace that flag with -L${SRCDIR}/lib/linux.

Directly from the cgo docs:

When the cgo directives are parsed, any occurrence of the string ${SRCDIR} will be replaced by the absolute path to the directory containing the source file. This allows pre-compiled static libraries to be included in the package directory and linked properly.

The cgo tool will always invoke the C compiler with the source file's directory in the include path; i.e. -I${SRCDIR} is always implied.

Sign up to request clarification or add additional context in comments.

2 Comments

Ok, definitely helpful. I wish there was a "root" variable available. In my case, the source file is in a peer directory, so I have to reference "${SRCDIR}/../lib/inux", but at least it works. This removes the "could not import C" error, but the "go list failed to return CompiledGoFiles" message is still there. I've read the issue that cites, but I don't understand all of the implications.
Hmm, I've now moved forward and tried to add some code referencing the library, and now that "CompiledGoFiles" thing has gone away.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.