如何配置 pg_config/pgxs/make 以在 Makefile 中选择 CPPFLAGS 和 CFLAGS 来构建 Postgres C/C++ 扩展?
How to configure pg_config/pgxs/make to pick up CPPFLAGS and CFLAGS in the Makefile to build Postgres C/C++ extensions?
我想添加编译器标志来构建我的 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,-z,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,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,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 美分:
长话短说,您应该在 Makefile
中使用 PG_CFLAGS
或 PG_CPPFLAGS
(仅当使用 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
我想添加编译器标志来构建我的 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,-z,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,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,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 美分:
长话短说,您应该在 Makefile
PG_CFLAGS
或 PG_CPPFLAGS
(仅当使用 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