Go library: cannot find module providing package lib - go

I am creating 3 separate go projects: ace, aces-client and a library shared by both projects aceslib. I am having trouble including the shared library according to the go-documentation (https://golang.org/doc/code.html#Library)
all go files in aceslib share the package-name aceslib. I am including the library in ace and aces-client with import lib "aceslib". I can build the library with go build and it gets installed with go install, in the directory listing one can see that the file go/pkg/windows_amd64/aceslib.a gets created.
But when I try to build ace or ace-client go complains:
$ go build
build ace: cannot load aceslib: cannot find module providing package aceslib
My go setup:
$ go version
go version go1.12.9 windows/amd64
$ go env
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\Yulivee\AppData\Local\go-build
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOOS=windows
set GOPATH=C:\Users\Yulivee\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=C:\Users\Yulivee\go\src\ace\go.mod
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\Yulivee\AppData\Local\Temp\go-build024649150=/tmp/go-build -gno-record-gcc-switches
Directory Structure:
.
├── bin
│   ├── ace-client.exe
│   └── ace.exe
├── pkg
│   ├── mod
│   │   ├── cache
│   │   └── golang.org
│   └── windows_amd64
│   ├── ace
│   ├── aceslib.a
│   └── golang.org
└── src
├── ace
│   ├── README.md
│   ├── go.mod
│   ├── go.sum
│   └── main.go
├── ace-client
│   └── main.go
├── aceslib
│   ├── README.md
│   ├── crypto.go
│   ├── go.mod
│   └── utils.go
What am I missing?
Things I have tried, that did not help and lead to the same error:
$ go clean -i -x -cache -modcache
$ chmod 755 go/pkg/windows_amd64/aceslib.a
$ go get
build ace: cannot load aceslib: cannot find module providing package aceslib
$ rm -rf go/pkg/*

The directory structure and package names are a bit off - it should be a URL to your project's repo, like every other import that isn't from the stdlib. If you have no repo and want to use local references you'll need to add a replace directive to ace/go.mod like so:
replace aceslib => ../aceslib
As documented in the Modules docs.

IIRC, package names without directories are reserved for stdlib packages. So, you should first push the source tree down at least a level so you can access it by "dir/package".
Next, do you really want to treat these packages as separate modules? You have separate mod files under ace and aceslib packages. You may combine them under one module with a single go.mod at the project root and make life easier for yourself. That way you don't need redirects, etc.
You also have to include the modules with their directories:
import lib "dir/aceslib"

Related

CMake and correct add shared library

I'm trying to add a shared library to my project using CMake. The library is MsgPack for C++11. I must be doing something wrong because none of the examples here compile. I get this error:
error: no match for ‘operator<<’ (operand types are
‘MsgPack::Serializer’ and ‘std::unique_ptr’)
serializer << MsgPack__Factory(Array(std::move(arrayWith3Elements)));
My guess is that it has to do with my CMake syntax. The file system tree where I keep the compiled library looks like this:
/home/I/direcytory/MsgPack/
├── include
│   ├── Container.h
│   ├── ContainerHeader.h
│   ├── Core.h
│   ├── Data.h
│   ├── Element.h
│   ├── Header.h
│   ├── MsgPack.h
│   ├── MsgPackSocket.h
│   ├── netLink.h
│   ├── Number.h
│   ├── Primitive.h
│   ├── Socket.h
│   ├── StreamManager.h
│   └── Utf8.h
├── libnetLink.a
└── libnetLink.so
In CMake I'm doing this:
cmake_minimum_required(VERSION 3.0.2)
project(MyProject)
add_library(libnetLink SHARED IMPORTED)
SET_PROPERTY(TARGET libnetLink PROPERTY IMPORTED_LOCATION /home/I/Direcytory/MsgPack/libnetLink.so)
set(SOURCE_FILES main.cpp)
include_directories("/home/I/Direcytory/MsgPack/include")
target_link_libraries(MyProject libnetLink)
CMake doesn't report any errors, but my program won't compile.
It's worth noting, the above method has worked fine for me with other libraries. That's what has me confused.

SCons env.Install("path_to_folder") does not show folder files in dependency tree

In SCons when a folder is installed the dependency tree is not aware of the contents of the folder. This means that implicit relationships cannot be created.
env.Install("out/bin","path/to/folder")
env.Install("out/archive", Glob("out/bin/folder/library.lib"))
In this sample code the Glob returns [] because SCons is unaware the folder contains a file called library.lib.
The only workaround I've found for this is to walk the directory and install each individual file.
Does the SCons Install not have an option to do this for you?
I have run into this as well. I have not found any other solution than to walk the directories as you describe. Though the contents of the folder are copied, to SCons there is just one target, and just one source, unless you specify each one individually.
import os
def recursive_install(target, source, env):
source_dirname = os.path.dirname(source)
for root, dirnames, filenames in os.walk(source):
for filename in filenames:
env.Install(os.path.join(
target, os.path.relpath(root, os.path.dirname(source))),
os.path.join(root, filename))
env = Environment()
recursive_install("out/bin", "path/to/folder", env)
env.Install("out/archive", "out/bin/folder/library.lib")
Which when run produces...
>> scons --version
SCons by Steven Knight et al.:
script: v2.3.4, 2014/09/27 12:51:43, by garyo on lubuntu
engine: v2.3.4, 2014/09/27 12:51:43, by garyo on lubuntu
engine path: ['/usr/lib/scons/SCons']
Copyright (c) 2001 - 2014 The SCons Foundation
>> tree
.
├── path
│   └── to
│   └── folder
│   └── library.lib
└── SConstruct
3 directories, 2 files
>> scons
scons: Reading SConscript files ...
scons: done reading SConscript files.
scons: Building targets ...
Install file: "path/to/folder/library.lib" as "out/bin/folder/library.lib"
Install file: "out/bin/folder/library.lib" as "out/archive/library.lib"
scons: done building targets.
>> tree
.
├── out
│   ├── archive
│   │   └── library.lib
│   └── bin
│   └── folder
│   └── library.lib
├── path
│   └── to
│   └── folder
│   └── library.lib
└── SConstruct
7 directories, 4 files
SCons doesn't have an option for recursively installing all files under a top-folder, because that's considered to be a rare case (note, how SCons prefers in-source-tree builds, such that you'd have to exclude a lot of files from the Install call otherwise). If you are creating the "files to install" within the same build, the preferred method would be to use the emitted list of targets from your Builder, and put it as second argument to the Install method:
mylibs = env.AnyBuilder('library', sources)
env.Install("out/bin", mylibs)
Then you don't have to manually list all the target files again with a recursive os.walk as in Kenneth's answer.

Can't install Revel (Unrecognized import path)

I'm a beginner in Go and I would like to use the Revel web framework.
I installed: Git; Mercurial and even: Bazaar and CVS.
I checked my environment variables. I set environment variable GOPATH to D:\Go and added D:\Go\bin to PATH.But I still get these errors when I go get The Revel Framework (go get github.com/revel/revel)
**
package golang.org/x/net/websocket: unrecognized import path "golang.org/x/net/websocket"
package gopkg.in/fsnotify.v1: unrecognized import path "gopkg.in/fsnotify.v1"**
For instalation of GO into your home folder you need this environment variables:
.
├── bin
├── go (GO)
└── src
├── revel.project
│   ├── app
│   │   ├── controllers
│   │   ├── models
│   │   ├── routes
│   │   ├── tmp
│   │   └── views
│   │   ├── admin
│   │   ├── App
│   │   ├── errors
│   │   └── users
│   ├── conf
│   ├── messages
│   ├── public
│   │   ├── css
│   │   │   └── administrator
│   │   ├── img
│   │   ├── js
│   │   └── uploads
│   │   └── 1
│   ├── resources
│   ├── scripts
│   ├── test-results
│   └── tests
├── code.google.com
├── github.com
│   ├── revel
│   │   ├── cmd
│   │   ├── modules
│   │   └── revel
run this in your prompt path or modify if your folders are differents.
export GOARCH=amd64
export GOPATH=~/go
export GOBIN=~/go/go/bin
export GOROOT=~/go/go
export PATH=$PATH:$GOPATH/go/bin
export GOTOOLDIR=~/go/go/pkg/tool/linux_amd64
export CC="gcc"
export GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0"
export CXX=g++
export CGO_ENABLED=1
Where ~ is the name of you personal folder in the system (Bash Gnu Linux)

SWIG, perl, DynaLoader can't find boot_$Module on OS X

I am trying to build and install a SWIG-generated perl API on OS X 10.10.2. (It's for the FreeLing 3.1 language analysis toolkit.) I have generated and compiled the SWIG files, producing freeling.so.
But when I try to use freeling in a perl script, I get the error:
Can't find 'boot_freeling' symbol in /usr/local/lib/libfreeling.dylib at freeling.pm line 11.
But boot_freeling should be defined in the SWIG-generated freeling.so, not in libfreeling.dylib (the FreeLing package lib). (nm -U confirms this: _boot_freeling is defined in freeling.so; I'm assuming the leading underscore is just part of the object file format.)
I have made sure that freeling.so comes before libfreeling.dylib in LD_LIBRARY_PATH. I've also tried unshifting the path to freeling.so onto #DynaLoader::dl_library_path.
I suspect this is not a path problem, but something about building for OS X. In the past, I have built this on Ubuntu and it works fine. I have tweaked the gcc options (-bundle instead of -shared).
Additional info:
perl -V:dlext => dlext='bundle';
Building SOso-0.01.patch.txt produces:
blib
├── blib/arch
│   └── blib/arch/auto
│   └── blib/arch/auto/SOso
│   └── blib/arch/auto/SOso/SOso.bundle
├── blib/bin
├── blib/lib
│   ├── blib/lib/SOso.pm
│   └── blib/lib/auto
│   └── blib/lib/auto/SOso
├── blib/man1
├── blib/man3
└── blib/script
Makefile target:
freeling.bundle: freeling_perlAPI.cxx
g++ -v -bundle -o freeling.bundle freeling_perlAPI.cxx -lfreeling -lperl -lboost_system -I $(FREELINGDIR)/include -I $(BOOSTDIR)/include -I $(ICU4CDIR)/include -L $(FREELINGDIR)/libfreeling -I $(PERLDIR)/CORE -L $(LIBDIR) -L $(BOOSTDIR)/lib -L $(PERLDIR)/CORE -fPIC
Ok, promoting to answer :)
What do you get for perl -V:dlext ?
When you compile this module SOso-0.01.patch.txt what files are created in blib?
Well :) if your os/perl is configured to look for a freeling.bundle, I don't think its going to try to look at freeling.so .... so I'd try to do something about that ... rename the file to use the dlext

Go build cannot find package

Problem
I haven't been able to find a solution to this by looking at related questions. I can't tell what makes my Go environment different from the canonical setup.
go env returns
GOROOT="/usr/lib/go"
GOBIN=""
GOARCH="386"
GOCHAR="8"
GOOS="linux"
GOEXE=""
GOHOSTARCH="386"
GOHOSTOS="linux"
GOTOOLDIR="/usr/lib/go/pkg/tool/linux_386"
GOGCCFLAGS="-g -O2 -fPIC -m32 -pthread"
CGO_ENABLED="1"
tree $GOPATH returns
/home/USER/go
├── bin
├── pkg
│   └── linux_386
│   └── bitbucket.org
│   └── USER-NAME
│   └── PROJECT
│   └── my_package.a
└── src
└── bitbucket.org
└── USER-NAME
└── PROJECT
├── main
│   ├── main.go
└── my_package
└── my_package.go
(ALL-CAPS are substitutions)
main.go contains
package main
import (
"bitbucket.org/USER-NAME/PROJECT/my_package"
)
func main() {
my_package.Foo()
}
Calling go build in the main directory returns import "my_package": cannot find package
Volker pointed out that go env should have returned a GOPATH entry as well. The source of the env command corroborates that. However, running echo $GOPATH in bash or os.Getenv("GOPATH") in Go both return \home\USER\go. I'm not sure why the same isn't returned by go env.
Solution
I was running Go 1.0 when I was having this issue. The problem disappeared when I upgraded to Go 1.2.1.
You have a directory called main. This won't work. Change it.
Structure it like $GOPATH/src/bitbucket.com/youruser name/yourpackagename/{main.go, otherthing.go, otherpackagedirectory}.
"package main" doesn't have to be in it's own sub folder: it inherits the name of your Bitbucket project (username/myprojectname).
You did not set (or export) GOPATH. GOPATH Is much more important than GOROOT (at least in newe Go versions).

Resources