Thread: connecting using libpq breaks printf

connecting using libpq breaks printf

From
Joey Morris
Date:
This is my first attempt at using libpq, and I'm running across a strange
problem. Here is my bare-bones program:

#include <stdio.h>
#include "libpq-fe.h"

int main(int argc, char **argv) {
  PGconn *conn;
  fprintf(stderr, "connecting\n");
  conn = PQconnectdb("dbname=postgres");
  PQfinish(conn);
  return 0;
}

I expected this program to print "connecting", but in fact I get no output
whatsoever. If I comment out the PQconnectdb and PQfinish lines, I see
"connecting" as expected. What could be going on here?

A few notes:
 - I'm running PostgreSQL 8.3.6 on Windows XP. I used the one-click installer
   to install.
 - I'm compiling the program with MinGW. I get no compiler warnings or errors.
 - I can connect to the database just fine using both pgAdmin and the
   command-line client. The database is running on localhost.
 - I've tried adding code to see if PQstatus(conn) == CONNECTION_OK, but this
   hasn't been useful. Since fprintf() isn't working, I can't display a message
   showing the result of the comparison.
 - I've tried various combinations of connection options in case there was an
   issue with the hostname, database name, username, or password. I always get
   the same result: no output.
 - I've tried printing to stdout and to a file, but neither helped.

Thanks for any help you can provide.
Joey

Re: connecting using libpq breaks printf

From
Lennin Caro
Date:
> This is my first attempt at using libpq, and I'm running
> across a strange
> problem. Here is my bare-bones program:
>
> #include <stdio.h>
> #include "libpq-fe.h"
>
> int main(int argc, char **argv) {
>   PGconn *conn;
>   fprintf(stderr, "connecting\n");
>   conn = PQconnectdb("dbname=postgres");
>   PQfinish(conn);
>   return 0;
> }
>
> I expected this program to print "connecting",
> but in fact I get no output
> whatsoever. If I comment out the PQconnectdb and PQfinish
> lines, I see
> "connecting" as expected. What could be going on
> here?
>
> A few notes:
>  - I'm running PostgreSQL 8.3.6 on Windows XP. I used
> the one-click installer
>    to install.
>  - I'm compiling the program with MinGW. I get no
> compiler warnings or errors.
>  - I can connect to the database just fine using both
> pgAdmin and the
>    command-line client. The database is running on
> localhost.
>  - I've tried adding code to see if PQstatus(conn) ==
> CONNECTION_OK, but this
>    hasn't been useful. Since fprintf() isn't
> working, I can't display a message
>    showing the result of the comparison.
>  - I've tried various combinations of connection
> options in case there was an
>    issue with the hostname, database name, username, or
> password. I always get
>    the same result: no output.
>  - I've tried printing to stdout and to a file, but
> neither helped.
>
> Thanks for any help you can provide.
> Joey


try
fprintf(stdout,"Connection \n");
printf("Connection \n");




Re: connecting using libpq breaks printf

From
Aurimas Černius
Date:
Hi,


> This is my first attempt at using libpq, and I'm running across a strange
> problem. Here is my bare-bones program:
>
> #include<stdio.h>
> #include "libpq-fe.h"
>
> int main(int argc, char **argv) {
>    PGconn *conn;
>    fprintf(stderr, "connecting\n");
>    conn = PQconnectdb("dbname=postgres");
>    PQfinish(conn);
>    return 0;
> }
>
> I expected this program to print "connecting", but in fact I get no output
> whatsoever. If I comment out the PQconnectdb and PQfinish lines, I see
> "connecting" as expected. What could be going on here?

Did you use *exactly* the same command line to compile both versions?
What is that command line(s)?


> A few notes:
>   - I'm running PostgreSQL 8.3.6 on Windows XP. I used the one-click installer
>     to install.
>   - I'm compiling the program with MinGW. I get no compiler warnings or errors.
>   - I can connect to the database just fine using both pgAdmin and the
>     command-line client. The database is running on localhost.
>   - I've tried adding code to see if PQstatus(conn) == CONNECTION_OK, but this
>     hasn't been useful. Since fprintf() isn't working, I can't display a message
>     showing the result of the comparison.
>   - I've tried various combinations of connection options in case there was an
>     issue with the hostname, database name, username, or password. I always get
>     the same result: no output.
>   - I've tried printing to stdout and to a file, but neither helped.
>
> Thanks for any help you can provide.
> Joey
>


--
Aurimas

Re: connecting using libpq breaks printf

From
Aurimas Černius
Date:
Hi,

Looks like you accidently wrote to me personnally, not to mailing list.

> On Wed, Feb 18, 2009 at 10:01 AM, Aurimas Černius<aurisc4@gmail.com>  wrote:
>> Did you use *exactly* the same command line to compile both versions?
>> What is that command line(s)?
>
> Yes, I used the same compilation command line for both versions:
>
> gcc -o pq_test -I"C:\Program Files\PostgreSQL\8.3\include"
> -L"C:\Program Files\PostgreSQL\8.3\lib" -lpq pq_test.c


Libraries should be places after the source file, that is (not sure
about -L, but for -l surely):

gcc -o pq_test -I"C:\Program Files\PostgreSQL\8.3\include" pq_test.c
-L"C:\Program Files\PostgreSQL\8.3\lib" -lpq

I don't if that's the problem, but it's not hard to try :)

--
Aurimas

Re: connecting using libpq breaks printf

From
Joey Morris
Date:
Lennin Caro <lennin.caro@yahoo.com> wrote on Wed, Feb 18, 2009 at 06:53:32AM -0800:
> try
> fprintf(stdout,"Connection \n");
> printf("Connection \n");

Thanks, but both produce the same behavior as fprintf(stderr, "Connection \n"):
It prints when the PQconnectdb and PQfinish lines are commented out but not
when they are present.

Re: connecting using libpq breaks printf

From
Joey Morris
Date:
Aurimas Černius <aurisc4@gmail.com> wrote on Wed, Feb 18, 2009 at 06:08:07PM +0200:
> Libraries should be places after the source file, that is (not sure
> about -L, but for -l surely):
>
> gcc -o pq_test -I"C:\Program Files\PostgreSQL\8.3\include" pq_test.c
> -L"C:\Program Files\PostgreSQL\8.3\lib" -lpq
>
> I don't if that's the problem, but it's not hard to try :)

Thanks for the suggestion, but unfortunately it didn't fix this problem.

Re: connecting using libpq breaks printf

From
Aurimas Černius
Date:
Hi,


> Aurimas Černius<aurisc4@gmail.com>  wrote on Wed, Feb 18, 2009 at 06:08:07PM +0200:
>> Libraries should be places after the source file, that is (not sure
>> about -L, but for -l surely):
>>
>> gcc -o pq_test -I"C:\Program Files\PostgreSQL\8.3\include" pq_test.c
>> -L"C:\Program Files\PostgreSQL\8.3\lib" -lpq
>>
>> I don't if that's the problem, but it's not hard to try :)
>
> Thanks for the suggestion, but unfortunately it didn't fix this problem.


How do you run the program, in console or by double clicking on
executable. Try the second. Does the console window apear on the screen?

--
Aurimas

Re: connecting using libpq breaks printf

From
David Wilson
Date:
On Wed, Feb 18, 2009 at 9:47 AM, Joey Morris <rjmorris12@gmail.com> wrote:

> I expected this program to print "connecting", but in fact I get no output
> whatsoever. If I comment out the PQconnectdb and PQfinish lines, I see
> "connecting" as expected. What could be going on here?

Try adding "fflush(stderr);" after your fprintf() call. If the
connection is hanging and output hasn't been flushed, you wouldn't see
much.


--
- David T. Wilson
david.t.wilson@gmail.com

Re: connecting using libpq breaks printf

From
Joey Morris
Date:
David Wilson <david.t.wilson@gmail.com> wrote on Wed, Feb 18, 2009 at 11:36:44AM -0500:
> Try adding "fflush(stderr);" after your fprintf() call. If the
> connection is hanging and output hasn't been flushed, you wouldn't see
> much.

No, this doesn't help, either. Thanks for the suggestion.

Re: connecting using libpq breaks printf

From
Sam Mason
Date:
On Wed, Feb 18, 2009 at 06:08:07PM +0200, Aurimas Cernius wrote:
> Joey Morris wrote:
> > gcc -o pq_test -I"C:\Program Files\PostgreSQL\8.3\include" -L"C:\Program Files\PostgreSQL\8.3\lib" -lpq pq_test.c

gcc won't give many warnings unless you give it a -Wall flag; it may be
doing strange things and not telling you, doubt it though the code looks
nice and simple and compiles and runs here OK.  I'm running on a Linux
box though, but that shouldn't matter for this code.

Have you got a debugger available, it could help to single step through
and see what's going on.

Just thought, it's not because it can't find the DLL is it?

--
  Sam  http://samason.me.uk/

Re: connecting using libpq breaks printf

From
Mikko Partio
Date:


On Wed, Feb 18, 2009 at 4:47 PM, Joey Morris <rjmorris12@gmail.com> wrote:
This is my first attempt at using libpq, and I'm running across a strange
problem. Here is my bare-bones program:

#include <stdio.h>
#include "libpq-fe.h"

int main(int argc, char **argv) {
 PGconn *conn;
 fprintf(stderr, "connecting\n");
 conn = PQconnectdb("dbname=postgres");
 PQfinish(conn);
 return 0;
}


Works fine with linux + gcc. Must be something in your environment.

Regards

Mikko

Re: connecting using libpq breaks printf

From
Joey Morris
Date:
> How do you run the program, in console or by double clicking on
> executable. Try the second. Does the console window apear on the screen?

Aha! This tip has led to the solution. I had been running the program from
an emacs shell buffer. If I run it from the Windows console or by
double-clicking the executable, a dialog box pops up with this error
message:

"The application has failed to start because LIBPQ.dll was not found.
Re-installing the application may fix this problem."

At run-time, the system apparently cannot find libpq.dll and is aborting
immediately. The reason is that the directory containing libpq.dll was not
in my DLL search path. I added C:\Program Files\PostgreSQL\8.3\bin to my
PATH environment variable, and now the program works as expected. Note that
adding C:\Program Files\PostgreSQL\8.3\lib to the PATH doesn't completely
solve the problem, since bin contains some necessary DLLs that aren't in lib
(such as ssleay32.dll).

Alternatively, I could have copied the DLLs to C:\Windows\system32 or to the
directory containing the executable, but I like the PATH solution better.

Thanks, everyone, for your help!

Re: connecting using libpq breaks printf

From
Joey Morris
Date:
Aurimas Černius <aurisc4@gmail.com> wrote on Wed, Feb 18, 2009 at 05:01:14PM +0200:
> Did you use *exactly* the same command line to compile both versions?
> What is that command line(s)?

Yes, I used the same compilation command line for both versions:

gcc -o pq_test -I"C:\Program Files\PostgreSQL\8.3\include" -L"C:\Program Files\PostgreSQL\8.3\lib" -lpq pq_test.c