PostgreSQL C Language Extension with C++ Code

From: TalGloz <glozmantal(at)gmail(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: PostgreSQL C Language Extension with C++ Code
Date: 2018-08-12 17:05:38
Message-ID: 1534093538827-0.post@n3.nabble.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hi,

I've searched information about my problem in the archives and on the
internet, but it didn't help. I have this small myfunc.cpp

1: #include <postgres.h>
2: #include <utils/rel.h>
3: #include <postgres.h>
4: #include <fmgr.h>
5: #include <utils/array.h>
6: #include <utils/builtins.h>
7: #include <catalog/pg_type.h>
8: #include <string>
9: #include <vector>
10: #include <stdlib.h>
11: #include <stdint.h>
12: #include <iostream>
13: #include <fstream>
14: #include <seal/seal.h> // external compiled c++ library linked on
running 'make'
15:
16: #ifdef PG_MODULE_MAGIC
17: PG_MODULE_MAGIC;
18: #endif
19:
20: Datum sum_of_numbers(PG_FUNCTION_ARGS);
21: PG_FUNCTION_INFO_V1(sum_of_numbers);
22:
23: extern "C" {
24: int64_t sum_of_numbers(){
25: std::vector<int64_t> numbers {23, 445, 64};
26: auto sum = 0;
27: for (auto &item : numbers){
28: sum += item;
29: }
30: return sum;
31: }
32: }

And I compile without any problem suing this Makefiles:

1: MODULES = myfunc
2:
3: PG_CONFIG = /usr/pgsql-10/bin/pg_config
4: PGXS = $(shell $(PG_CONFIG) --pgxs)
5: INCLUDEDIR = $(shell $(PG_CONFIG) --includedir-server)
6: INCLUDE_SEAL = /usr/local/include/seal
7: INCLUDE_SEAL_LIB = /usr/local/lib/libseal.a
8: INCLUDE_CPPCODEC = /usr/local/include/cppcodec
9: #CFLAGS = -std=c11
10: #CFLAGS = --std=c++14 -fPIC -Wall -Werror -g3 -O0
11: include $(PGXS)
12: myfunc.so: myfunc.o
13: g++ -shared -o myfunc.so myfunc.o
14:
16: myfunc.o: myfunc.cpp
17: g++ --std=c++17 -fPIC -Wall -Werror -g3 -O0 -o myfunc.o -c
myfunc.cpp -I$(INCLUDEDIR) -L$(INCLUDE_SEAL_LIB) -I$(INCLUDE_SEAL)
-I$(INCLUDE_CPPCODEC)
18:

After copying the myfunc.so file to the PostgreSQL lib folder and executing:

1: CREATE OR REPLACE FUNCTION
2: sum_of_numbers() RETURNS integer AS 'myfunc'
3: LANGUAGE C STRICT;

I get this error:
*ERROR: incompatible library "/usr/pgsql-10/lib/myfunc.so": missing magic
block
HINT: Extension libraries are required to use the PG_MODULE_MAGIC macro.
SQL state: XX000*

I've added the files and line numbers for an easier discussion.

Thanks a lot,
Tal

myfunc.cpp <http://www.postgresql-archive.org/file/t351748/myfunc.cpp>
Makefiles.Makefiles
<http://www.postgresql-archive.org/file/t351748/Makefiles.Makefiles>

--
Sent from: http://www.postgresql-archive.org/PostgreSQL-general-f1843780.html

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2018-08-12 17:25:21 Re: PostgreSQL C Language Extension with C++ Code
Previous Message Stephen Frost 2018-08-11 22:53:36 Re: Replication failure, slave requesting old segments