PostgreSQL ECPG database connection issue - postgresql

I am trying to connect to PostgreSQL database using ecpg program and I am getting below error.
cc testecpg.c
/tmp/ccSzqgA7.o: In function `main':
testecpg.c:(.text+0x5d): undefined reference to `ECPGconnect'
testecpg.c:(.text+0x62): undefined reference to `ECPGget_sqlca'
testecpg.c:(.text+0x70): undefined reference to `sqlprint'
collect2: error: ld returned 1 exit status
testecpg.c file generated after executing ecpg testecpg.pgc
/* Processed by ecpg (4.11.0) */
/* These include files are added by the preprocessor */
#include "/opt/rh/rh-postgresql95/root/usr/include/ecpglib.h"
#include "/opt/rh/rh-postgresql95/root/usr/include/ecpgerrno.h"
#include "/opt/rh/rh-postgresql95/root/usr/include/sqlca.h"
/* End of automatic include section */
#line 1 "testecpg.pgc"
#include <stdio.h>
#include "/opt/rh/rh-postgresql95/root/usr/include/libpq-fe.h"
int main(void)
{
/* exec sql begin declare section */
#line 6 "testecpg.pgc"
char * dbname = "dbname" ;
#line 7 "testecpg.pgc"
char * db = "dbname#hostname:5432" ;
#line 8 "testecpg.pgc"
char * user = "user" ;
#line 9 "testecpg.pgc"
char * passwd = "password" ;
#line 10 "testecpg.pgc"
const char * target = "dbname#hostname:5432" ;
/* exec sql end declare section */
#line 11 "testecpg.pgc"
/* exec sql whenever sqlerror sqlprint ; */
#line 12 "testecpg.pgc"
{ ECPGconnect(0, 0, target , user , passwd , NULL, 0);
#line 13 "testecpg.pgc"
if (sqlca.sqlcode < 0) sqlprint();}
#line 13 "testecpg.pgc"
printf("connection succssfull");
}
Is there any library to be included or any step I have missed?

You forgot to link with the ECPG library.
On Unix systems, that would look somewhat like
cc -o testecpg testecpg.c -lecpg
You have to add the appropriate -I and -L options so that the compiler can find the include files and the libraries.

Related

Expand C preprocessor ternary macro for printing in pragma message?

Judging by this Print numeric value of a define that's based on other macros via pragma message? , what I want is likely not possible; then again, I'm not doing arithmetic here - so here is my example:
#include <stdio.h>
enum my_sizes { SIZE_BITS_8 = 0, SIZE_BITS_16 = 1, SIZE_BITS_32 = 2 };
#define XSTR(x) STR(x)
#define STR(x) #x
#define CHOSEN_SIZE_BITS SIZE_BITS_32
#define CHOSEN_SIZE_BYTES \
( CHOSEN_SIZE_BITS==SIZE_BITS_8 ? 1 \
: CHOSEN_SIZE_BITS==SIZE_BITS_16 ? 2 \
: CHOSEN_SIZE_BITS==SIZE_BITS_32 ? 4 \
: 0 )
#pragma message( "CHOSEN_SIZE_BITS " XSTR(CHOSEN_SIZE_BITS) " CHOSEN_SIZE_BYTES " XSTR(CHOSEN_SIZE_BYTES) )
int main() {
printf("Hello, world! bytes: %d!\r\n", CHOSEN_SIZE_BYTES);
return 0;
}
In https://replit.com/languages/c, this produces:
> clang-7 -pthread -lm -o main main.c
main.c:14:9: warning: CHOSEN_SIZE_BITS SIZE_BITS_32
CHOSEN_SIZE_BYTES ( SIZE_BITS_32==SIZE_BITS_8 ? 1 :
SIZE_BITS_32==SIZE_BITS_16 ? 2 :
SIZE_BITS_32==SIZE_BITS_32 ? 4 : 0 )
[-W#pragma-messages]
#pragma message( "CHOSEN_SIZE_BITS " XSTR(CHOSEN_SIZE_...
^
1 warning generated.
> ./main
Hello, world! bytes: 4!
>
What I wanted instead, was the printout of pragma message to be:
main.c:14:9: warning: CHOSEN_SIZE_BITS 2 CHOSEN_SIZE_BYTES 4
Note that XSTR would have expanded CHOSEN_SIZE_BITS to a number, in case it was defined via #define CHOSEN_SIZE_BITS 2, but it seemingly doesn't if an enum value is used in the define; and of course, the whole ternary expression is included in the pragma message printout - even if the C printout (via printf) confirms that the CHOSEN_SIZE_BYTES does indeed end up with the expected value 4.
So, is it somehow possible to get the printout that I want from the above macros - and if so, how?
To calculate something in preprocessor you have to calculate it in preprocessor. Ternary is evaluated at runtime. In preprocessor you have macro expansions and ## operator and then some more macro expansions and ## operator. Nothing more.
The following code:
#define SIZE_BITS_8 0
#define SIZE_BITS_16 1
#define SIZE_BITS_32 2
#define XSTR(x) STR(x)
#define STR(x) #x
#define BITS_TO_BYTE_0() 1
#define BITS_TO_BYTE_1() 2
#define BITS_TO_BYTE_2() 4
#define BITS_TO_BYTE_IN(SIZE_BITS) \
BITS_TO_BYTE_##SIZE_BITS()
#define BITS_TO_BYTE(SIZE_BITS) \
BITS_TO_BYTE_IN(SIZE_BITS)
#define CHOSEN_SIZE_BITS SIZE_BITS_32
#define CHOSEN_SIZE_BYTES BITS_TO_BYTE(CHOSEN_SIZE_BITS)
#pragma message( "CHOSEN_SIZE_BITS " XSTR(CHOSEN_SIZE_BITS) " CHOSEN_SIZE_BYTES " XSTR(CHOSEN_SIZE_BYTES) )
Outputs the following diagnostic on GCC:
<source>:22:9: note: '#pragma message: CHOSEN_SIZE_BITS 2 CHOSEN_SIZE_BYTES 4'
22 | #pragma message( "CHOSEN_SIZE_BITS " XSTR(CHOSEN_SIZE_BITS) " CHOSEN_SIZE_BYTES " XSTR(CHOSEN_SIZE_BYTES) )

bitbake patching is failing

I am trying to add few checks in watchdog.c file in below package, but getting patch failure error as below, manual patch works fine without any issue, also patch is created from the file of this pkg only so there is no issue with version mismatch or older version.
https://sourceforge.net/projects/watchdog/files/watchdog/5.13/
ERROR: Command Error: exit status: 1 Output:
Applying patch watchdog.patch
patching file src/watchdog.c
Hunk #2 succeeded at 948 with fuzz 1.
Hunk #3 FAILED at 1057.
1 out of 3 hunks FAILED -- rejects in file src/watchdog.c
Patch watchdog.patch does not apply (enforce with -f)
ERROR: Function failed: patch_do_patch
Tried to search the answers, but
Bitbake recipe not applying patch as expected
Why does this patch applied with a fuzz of 1, and fail with fuzz of 0?
Hunk #1 FAILED at 1. What's that mean?
Above posts mention about pkg or improper patch issue, which doesnt look to be for my pkg, requesting for help on this.
diff --git a/src/watchdog.c b/src/watchdog.c
index 8a09632..bb4a189 100644
--- a/src/watchdog.c
+++ b/src/watchdog.c
## -570,6 +570,7 ##
struct stat s;
struct icmp_filter filt;
filt.data = ~(1<<ICMP_ECHOREPLY);
+ int check = FALSE;
#if USE_SYSLOG
char *opts = "d:i:n:Ffsvbql:p:t:c:r:m:a:";
## -947,6 +948,11 ##
(void) fclose(fp);
}
+ check = check_product();
+ if(check == TRUE)
+ {
+ setup_files();
+ }
/* set signal term to set our run flag to 0 so that */
/* we make sure watchdog device is closed when receiving SIGTERM */
signal(SIGTERM, sigterm_handler);
## -1051,6 +1057,11 ##
do_check2(check_bin(act->name, timeout, 1), act->name, rbinary, NULL);
#endif
+ if(check == TRUE)
+ {
+ do_check(test_files(), repair_bin, NULL);
+ }
+
/* finally sleep some seconds */
usleep(tint * 500000); /* this should make watchdog sleep tint seconds alltogther */
/* sleep(tint); */
.bbappend file
# changes for patching the disk IO check
FILESEXTRAPATHS_prepend := "${THISDIR}/${PN}-${PV}:"
FILESEXTRAPATHS_append := "${THISDIR}/files"
SRC_URI += " \
file://watchdog.patch \
"
Above "SRC_URI" & "SRC_URI_machineoverride" tried one-by-one and did not work
SRC_URI_machineoverride += "file://watchdog.patch"
The problem may be that you have file://watchdog.patch twice in your SRC_URI, and bitbake attempts to apply it twice. Thus the second fails.
Try removing the SRC_URI_machineoverride line.
your current patch file is mismatching with the source found in https://sourceforge.net/projects/watchdog/files/watchdog/5.13/
I have added changes manually to file from https://sourceforge.net/projects/watchdog/files/watchdog/5.13/ and patch is as below,
--- a/src/watchdog.c 2020-07-02 13:57:45.771971643 +0200
+++ b/src/watchdog.c 2020-07-02 13:57:55.979985941 +0200
## -567,6 +567,7 ##
pid_t child_pid;
int oom_adjusted = 0;
struct stat s;
+ int check = FALSE
#if USE_SYSLOG
char *opts = "d:i:n:Ffsvbql:p:t:c:r:m:a:";
## -1053,6 +1054,10 ##
do_check2(check_bin(act->name, timeout, 1), act->name, rbinary, NULL);
#endif
+ if(check == TRUE)
+ {
+ do_check(test_files(), repair_bin, NULL);
+ }
/* finally sleep some seconds */
usleep(tint * 500000); /* this should make watchdog sleep tint seconds alltogther */
/* sleep(tint); */

Rexx, Parse a file for a single line

I'm trying to find a way to parse a file in Rexx. Each line has two words and an IP address.
Example
Location Name 10.0.0.1
I have looked over a lot of documentation and i can get it to print all the lines in the file but i cannot figure out how to search an entire file and print a specific line by using a match operator.
For Regina Rexx this is a program should be close to what you want:
Call A000_init
Call R000_ReadFile
do while MoreData
parse var line pt1 pt2
if (pt1 == whatever) then do
/* Do some thing */
end
Call R000_ReadFile
end
A000_init:
Yes = 1
No = 0
MoreData = yes
filename = .....
return
R000_ReadFile:
if lines(filename,'N') then do
Line= LineIn(filename)
end; else do
line = ''
MoreData = no
end
Return
This is a pretty easy task for a Rexx program.
ExpectedLocation = 'Living Room' /* What location are we searching for? */
Signal on NotReady /* Jump to "NotReady:" at end-of-file. */
Do Forever /* ... or at least until EOF or Exit! */
Parse LineIn Word1 Word2 IPAddress . /* Pull apart the three tokens on the line */
Location = Word1 Word2 /* Put the two words of the location back together. */
If Location = ExpectedLocation then Do /* Did we find it? */
Say "Found it :-)" /* Yay! */
Exit /* We're done, stop the program. */
End
End
NotReady: /* We come here at end-of-file. */
Say "Didn't find it :-(" /* Darn! */
Regina is Open Source, the project is on SourceForge at http://regina-rexx.sourceforge.net, and the documentation for the version you're using can be downloaded from http://sourceforge.net/projects/regina-rexx/files/regina-documentation/3.4/
Input file : XXXXXX.XXXX.XXXX
***************************** Top of Data
Location1 Name1 11.11.11.11
Location2 Name2 22.22.22.22
Location3 Name3 33.33.33.33
**************************** Bottom of Data
Code:
/* REXX */
/* Author : Ebin Paulose */
/*=============================================================================*/
YourWord = Location2 /*Word which we need to find, here i m giving "Location2"*/
Your_PS = 'XXXXXX.XXXX.XXXX' /*File name where we need to search */
"ALLOC DA('"Your_PS"') F(FILEDD) SHR REUSE" /* Allocate the file */
DO FOREVER
"EXECIO 1 DISKR FILEDD"
IF RC>0 THEN LEAVE
PULL Record /* pull one record from file */
PARSE VAR Record Location " " Name " " IPAddress
/* parse record into 3 parts Location, Name and IPAddress */
IF Location = YourWord THEN do /* check for matching */
SAY 'Found : ' Location
Found = 'Y'
END
END
IF Found ¬= 'Y' THEN DO
SAY 'Sorry Search item Not found'
END
"EXECIO 0 DISKR FILEDD (FINIS"
"FREE F(FILEDD)"
Output 1 (item found) :
Found : LOCATION2
Output 2 (item not found) :
Sorry Search item Not found
filename = 'MY.DATA.SET'
if sysdsn(''''filename'''') <> 'OK' then exit
address 'TSO'
"ALLOCATE FILE(INDD) DATASET('"filename"') SHR REUSE"
"EXECIO DISKR * INDD(STEM file. FINIS)"
"CLOSE FILE(INDD)"
target = 'My Location'
found = 1==0 /* false */
do i = 1 to file.0
card = file.i
parse var card loc1 loc2 ip_address .
found = loc1' 'loc2 == target
if found then leave
end i
if found then
say 'IP address of 'target' is 'ip_address
else
say 'No IP address found for 'target
exit

"error: variable length array of non-POD element type" when compiling re::engine::RE2

I'm trying to install the re::engine::RE2 module with cpan.
When building, the compiler emits the error message:
re2_xs.cc:254:25: error: variable length array of non-POD element type
're2::StringPiece'
re2::StringPiece res[re->nparens + 1];
^
What does that mean? How can I fix that? I'm using a 2011 Macbook Air and Perl 5.12.
re::engine::RE2 was never successfully tested on darwin/Perl 5.12.
Googling for the error message suggests that this is a flaw in old clang that ships with Xcode, and not present in newer versions.
Upgrade your software, you're out of support, perhaps use Macports to get a fresh gcc.
True, re::engine::RE2 never compiled on OS X. But you can get it to compile. Locate the cpan directory. For me it is ~/.cpan/build/re-engine-RE2-0.13-BY20k3/. Then within there, you change two C++ source files.
$ diff -p re2_xs.cc.old re2_xs.cc
*** re2_xs.cc.old 2015-04-20 20:20:15.000000000 +0200
--- re2_xs.cc 2015-04-20 20:22:24.000000000 +0200
*************** RE2_exec(pTHX_ REGEXP * const rx, char *
*** 229,236 ****
RE2 * ri = (RE2*) SvANY(rx)->pprivate;
regexp * re = SvANY(rx);
- re2::StringPiece res[re->nparens + 1];
-
#ifdef RE2_DEBUG
Perl_warner(aTHX_ packWARN(WARN_MISC), "RE2: Matching '%s' (%p, %p) against '%s'", stringarg, strbeg, stringarg, RX_WRAPPED(rx));
#endif
--- 229,234 ----
*************** RE2_exec(pTHX_ REGEXP * const rx, char *
*** 241,246 ****
--- 239,246 ----
return 0;
}
+ re2::StringPiece *res = new re2::StringPiece[re->nparens + 1];
+
bool ok = ri->Match(
re2::StringPiece(strbeg, strend - strbeg),
stringarg - strbeg,
*************** RE2_exec(pTHX_ REGEXP * const rx, char *
*** 250,255 ****
--- 250,256 ----
/* Matching failed */
if (!ok) {
+ delete [] res;
return 0;
}
*************** RE2_exec(pTHX_ REGEXP * const rx, char *
*** 266,271 ****
--- 267,274 ----
}
}
+ delete [] res;
+
return 1;
}
Attempting to compile after that will get you to a tr1 issue.
/usr/bin/clang -o obj/util/arena.o -xc++ -O3 -DHAVE_PTHREAD -pthread -Wno-sign-compare -c -I. -DNDEBUG util/arena.cc
In file included from util/arena.cc:5:
./util/util.h:45:10: fatal error: 'tr1/unordered_set' file not found
I'm sure there are simpler -D fixes for the Makefile but I changed the code.
# diff -p re2/util/util.h.old re2/util/util.h
*** re2/util/util.h.old 2015-04-20 20:29:01.000000000 +0200
--- re2/util/util.h 2015-04-20 20:29:26.000000000 +0200
*************** using std::make_pair;
*** 42,49 ****
#if defined(__GNUC__) && !defined(USE_CXX0X)
! #include <tr1/unordered_set>
! using std::tr1::unordered_set;
#else
--- 42,51 ----
#if defined(__GNUC__) && !defined(USE_CXX0X)
! //#include <tr1/unordered_set>
! //using std::tr1::unordered_set;
! #include <unordered_set>
! using std::unordered_set;
#else
Now just
make && make install
and you now have re::engine::RE2 available for OS X.
The answer by Douglas Scofield is correct, but one additional change is needed. In re2_xs.cc you also must change line 244 as follows:
bool ok = ri->Match(
re2::StringPiece(strbeg, strend - strbeg),
stringarg - strbeg,
strend - strbeg,
RE2::UNANCHORED,
! res, re->nparens + 1);
Douglas Scofield's answer changes the array res to pointer *res to avoid the compiler error of variable length array. However, after this change the line
sizeof res / sizeof *res
does not work because you would be taking the size of the pointer and dividing by the size of the array. Therefore, you have to make the change above to
re->nparens + 1
to get the size of the array pointed to by *res, which is what was desired in the original code.
If you don't make the change then capture groups will not work in re2. You will get errors that your capture groups like $1, $2, etc. are uninitialized.
So great answer above, just needs one addendum.

Why system doesn't return main's value?

[root# test]$ cat return10.c
#include <stdio.h>
int main(int argc, char *argv[]){
return 10;
}
[root# test]$ perl -e 'print system("/path_to_return10")'
2560
I was expecting 10 but got 2560,why?
See $? in perldoc perlvar.
You got 10 * 256 (return value = 10) + 0 * 128 (there was no core dump) + 0 (process wasn't killed by signal).
as specified in the documentation for the system call in perl (http://perldoc.perl.org/functions/system.html):
The return value is the exit status of the program as returned by the
wait call. To get the actual exit value, shift right by eight (see
below).
indeed: 2560 >> 8 = 10