如何配置pg_config / pgxs / make在Makefile中选择CPPFLAGS和CFLAGS来构建Postgres C / C ++扩展?

问题描述

我想添加编译器标志来构建我的Postgres C / C ++扩展。我已经尝试过标准的Makefile做法,但是pg_config不会选择我添加的任何编译器标志。

Makefile如下:

  1 # the extensions name
  2 EXTENSION     = extension_one
  3 DATA          = $(wildcard *--*.sql)            # script files to install
  4 TESTS         = $(wildcard test/sql/*.sql)      # use appropriate testfiles
  5 
  6 CFLAGS   = -std=c99
  7 CPPFLAGS = -std=c++17
  8 
  9 # find the sql and expected directories under test
 10 # load plpgsql into test db
 11 # load extension into test db
 12 # dbname
 13 REGRESS_OPTS  = --inputdir=test         \
 14                 --load-extension=extension_one \
 15                 --load-language=plpgsql
 16 REGRESS       = $(patsubst test/sql/%.sql,%,$(TESTS))
 17 OBJS          = $(patsubst %.c,%.o,$(wildcard src/*.c)) # object files
 18 # final shared library to be build from multiple source files (OBJS)
 19 MODULE_big    = $(EXTENSION)
 20 
 21 
 22 # postgres build stuff
 23 PG_CONfig = pg_config
 24 PGXS := $(shell $(PG_CONfig) --pgxs)
 25 include $(PGXS)

pg_config环境变量设置如下:

>pg_config
BINDIR = /usr/lib/postgresql/12/bin
DOCDIR = /usr/share/doc/postgresql-doc-12
HTMLDIR = /usr/share/doc/postgresql-doc-12
INCLUDEDIR = /usr/include/postgresql
PKGINCLUDEDIR = /usr/include/postgresql
INCLUDEDIR-SERVER = /usr/include/postgresql/12/server
LIBDIR = /usr/lib/x86_64-linux-gnu
PKGLIBDIR = /usr/lib/postgresql/12/lib
LOCALEDIR = /usr/share/locale
MANDIR = /usr/share/postgresql/12/man
SHAREDIR = /usr/share/postgresql/12
SYSconfdIR = /etc/postgresql-common
PGXS = /usr/lib/postgresql/12/lib/pgxs/src/makefiles/pgxs.mk
CONfigURE = '--build=x86_64-linux-gnu' '--prefix=/usr' '--includedir=/usr/include' '--mandir=/usr/share/man' '--infodir=/usr/share/info' '--sysconfdir=/etc' '--localstatedir=/var' '--disable-silent-rules' '--libdir=/usr/lib/x86_64-linux-gnu' '--libexecdir=/usr/lib/x86_64-linux-gnu' '--disable-maintainer-mode' '--disable-dependency-tracking' '--with-icu' '--with-tcl' '--with-perl' '--with-python' '--with-pam' '--with-openssl' '--with-libxml' '--with-libxslt' 'PYTHON=/usr/bin/python3' '--mandir=/usr/share/postgresql/12/man' '--docdir=/usr/share/doc/postgresql-doc-12' '--sysconfdir=/etc/postgresql-common' '--daTarootdir=/usr/share/' '--datadir=/usr/share/postgresql/12' '--bindir=/usr/lib/postgresql/12/bin' '--libdir=/usr/lib/x86_64-linux-gnu/' '--libexecdir=/usr/lib/postgresql/' '--includedir=/usr/include/postgresql/' '--with-extra-version= (Ubuntu 12.4-0ubuntu0.20.04.1)' '--enable-nls' '--enable-integer-datetimes' '--enable-thread-safety' '--enable-tap-tests' '--enable-debug' '--enable-dtrace' '--disable-rpath' '--with-uuid=e2fs' '--with-gnu-ld' '--with-pgport=5432' '--with-system-tzdata=/usr/share/zoneinfo' '--with-llvm' 'LLVM_CONfig=/usr/bin/llvm-config-10' 'CLANG=/usr/bin/clang-10' '--with-systemd' '--with-selinux' 'MKDIR_P=/bin/mkdir -p' 'TAR=/bin/tar' 'CFLAGS=-g -O2 -fstack-protector-strong -Wformat -Werror=format-security -fno-omit-frame-pointer' 'LDFLAGS=-Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,Now' '--with-gssapi' '--with-ldap' '--with-includes=/usr/include/mit-krb5' '--with-libs=/usr/lib/mit-krb5' '--with-libs=/usr/lib/x86_64-linux-gnu/mit-krb5' 'build_alias=x86_64-linux-gnu' 'CPPFLAGS=-Wdate-time -D_FORTIFY_SOURCE=2' 'CXXFLAGS=-g -O2 -fstack-protector-strong -Wformat -Werror=format-security'
CC = gcc
CPPFLAGS = -Wdate-time -D_FORTIFY_SOURCE=2 -D_GNU_SOURCE -I/usr/include/libxml2 -I/usr/include/mit-krb5
CFLAGS = -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Werror=vla -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -Wno-format-truncation -Wno-stringop-truncation -g -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -fno-omit-frame-pointer
CFLAGS_SL = -fPIC
LDFLAGS = -Wl,Now -L/usr/lib/llvm-10/lib -L/usr/lib/x86_64-linux-gnu/mit-krb5 -Wl,--as-needed
LDFLAGS_EX = 
LDFLAGS_SL = 
LIBS = -lpgcommon -lpgport -lpthread -lselinux -lxslt -lxml2 -lpam -lssl -lcrypto -lgssapi_krb5 -lz -ledit -lrt -lcrypt -ldl -lm 
VERSION = Postgresql 12.4 (Ubuntu 12.4-0ubuntu0.20.04.1)

由于pg_config显示“ CFLAGS”和“ CPPFLAGS”,并且官方文档(https://www.postgresql.org/docs/12/libpq-build.html)说要使用这些标志,这是我尝试过的,但是当我运行它们时,并不会拾取它们。注意:我已经尝试了正常情况下的“ =”和上面链接的文档中建议的“ + =”。没什么。

当我运行make时,我得到:

gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Werror=vla -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -Wno-format-truncation -Wno-stringop-truncation -g -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -fno-omit-frame-pointer -fPIC -I. -I./ -I/usr/include/postgresql/12/server -I/usr/include/postgresql/internal  -Wdate-time -D_FORTIFY_SOURCE=2 -D_GNU_SOURCE -I/usr/include/libxml2  -I/usr/include/mit-krb5  -c -o src/extension_one.o src/extension_one.c

注意:我在“数据库管理员”堆栈交换中问了这个问题,但现在意识到在这里提问可能更合适。

注意:如果您需要使用示例进行操作,以下是一个不错的Postgresql C扩展示例:Postgres_c_extension_demo

解决方法

我不是数据库专家,但是与Gentoo共享了很多编译天。这引起了我的好奇。 这是我的2美分:

长话短说,您应该在PG_CFLAGS中使用PG_CPPFLAGSMakefile(仅在使用c ++的情况下)

我怎么发现的?

$ pg_config --help

pg_config provides information about the installed version of PostgreSQL.

关键字正在提供有关已安装版本的信息。因此,多余的CFLAGS将不会显示在这里。

但是我注意到Makefile提取pg_config --pgxs的输出来扩展Makefile。我的发行版中的输出为: /usr/lib64/pgsql/pgxs/src/makefiles/pgxs.mk

所以我在编辑器中将其打开,这是一个代码段:

#   PG_CPPFLAGS -- will be prepended to CPPFLAGS
#   PG_CFLAGS -- will be appended to CFLAGS
#   PG_CXXFLAGS -- will be appended to CXXFLAGS
#   PG_LDFLAGS -- will be prepended to LDFLAGS