microo8 / plgo

easily create postgresql extensions in golang; moved to gitlab.com/microo8/plgo
292 stars 23 forks source link

fatal error: postgres.h: No such file or directory #9

Closed velo closed 6 years ago

velo commented 6 years ago

I installed postgresql-server-dev-all

Then I run go get -u github.com/microo8/plgo/plgo.

After that, I downloaded the example from github https://github.com/microo8/plgo/blob/master/example/example_methods.go

When I run ~/go/bin/plgo . I got the following result:

Package github.com/microo8/plgo not installed
please install it with: go get -u github.com/microo8/plgo/... 

I follow the instructions and run go get -u github.com/microo8/plgo/...

$ go get -u github.com/microo8/plgo/...
# github.com/microo8/plgo
/root/go/src/github.com/microo8/plgo/pl.go:7:10: fatal error: postgres.h: No such file or directory
 #include "postgres.h"
          ^~~~~~~~~~~~
compilation terminated.
# github.com/microo8/plgo/test/types
/root/go/src/github.com/microo8/plgo/test/types/pl.go:7:10: fatal error: postgres.h: No such file or directory
 #include "postgres.h"
          ^~~~~~~~~~~~
compilation terminated.
# github.com/microo8/plgo/test/bgw
/root/go/src/github.com/microo8/plgo/test/bgw/background_worker.go:7:10: fatal error: postgres.h: No such file or directory
 #include "postgres.h"
          ^~~~~~~~~~~~
compilation terminated.

Am I missing something obvious?

microo8 commented 6 years ago

what prints pg_config --includedir-server?

velo commented 6 years ago
$ pg_config --includedir-server
/usr/include/postgresql/9.6/server
ls /usr/include/postgresql/9.6/server
access
bootstrap
c.h
catalog
commands
common
datatype
dynloader.h
executor
fe_utils
fmgr.h
foreign
funcapi.h
getaddrinfo.h
getopt_long.h
lib
libpq
mb
miscadmin.h
nodes
optimizer
parser
pg_config.h
pg_config_ext.h
pg_config_manual.h
pg_config_os.h
pg_getopt.h
pg_trace.h
pgstat.h
pgtar.h
pgtime.h
plperl.h
plpgsql.h
plpy_util.h
plpython.h
port
port.h
portability
postgres.h
postgres_ext.h
postgres_fe.h
postmaster
ppport.h
regex
replication
rewrite
rusagestub.h
snowball
storage
tcop
tsearch
utils
windowapi.h
aeijdenberg commented 6 years ago

@velo - did you manage to resolve this?

I spent the past few days trying to figure it out, and messing around with all of my postgres header filers etc with no luck.

Once I read the code for plgo though the cause was pretty clear! plgo tries to find its own source code by examining the env variable GOPATH - however as of Go I think 1.8, it is no longer necessary to set this, and Go itself defaults to $HOME/go. This code fails though, because it looks for the env variable which isn't set.

To make matter worse, the error it returns suggests updating with ./... which won't work, as it needs to build the examples, which in turn need to be built by plgo.

The referenced PR should fix most of these cases.

You can also workaround by setting GOPATH before invoking, e.g.:

GOPATH=$HOME/go plgo .
microo8 commented 6 years ago

Thanks for the info :) I didn't know that GOPATH isn't required anymore. I can try to make it so that it doesn't read its source code, but it will be embedded in plgo tool.

aeijdenberg commented 6 years ago

Hi @microo8 - not sure if you saw, but I submitted a small PR that should fix the current issue: https://github.com/microo8/plgo/pull/10

I considered sending a PR to embed the code instead, https://github.com/jteeuwen/go-bindata is one library that works well, however I decided not to as a user will still probably want pl.go installed in the right place so that code editors such as Visual Studio Code etc can pick up all of the public structures / methods etc for code completion - and that it would probably be worse to have the behaviour of the plgo to use a different file.

velo commented 6 years ago

That does the trick

GOPATH=$HOME/go plgo .