Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hanging COUT!!! Help! C/C++/Unix/Informix/MQM/Solaris

Status
Not open for further replies.

bamboozled

Programmer
May 28, 2002
6
CH
Hi,
Could someone please help me.

My program hangs in a simple COUT statement!! What seems to be the problem??
I've tried different combinations of compile/link/runtime options, nothing seems to work. This is frustrating!

There is really nothing complicated here.
I've simplified the scenario and created three simple programs (codes below).
These are my objects:
1. tstmainc.ec - Main Program in C
2. tstsubc.c - Sub Program in C (simple printf)
3. tstsubcp.ec - Sub Program in C++ (SQL commands and Cout).

We are using:
- Informix 731
- uname -a = > SunOS gfxdev 5.6 Generic_105181-23 sun4u sparc SUNW,Ultra-2
- cannot remove mqm libraries (real program uses them)

Any help will be greatly appreciated!

Thanks!

==============================
tstmk.mk - Make File
==============================
INFORMIXINCL= $(INFORMIXDIR)/incl/esql
INFORMIXLB=$(INFORMIXDIR)/lib/esql
LIBRARIES= -L/usr/mqm/lib -lmqm -lmqmcs -lmqmzse -lnsl -lsocket -ldl -lm
LIBDIRS=-L$(INFORMIXDIR)/lib/esql -L$(INFORMIXDIR)/lib

INCLUDES= -I./-I/app/mqm/inc-I/usr/include-I/usr/ucbinclude-I$(INFORMIXINCL)-I$(INFORMIXDIR)/incl/tools

LDFLAGS= -Dsparc -DPRINT -g -DSET_UID -mt $(INFORMIXDIR)/lib/esql/checkapi.o $(INFORMIXDIR)/lib/esql/libixsql.a $(INFORMIXDIR)/lib/libixasf.a $(INFORMIXDIR)/lib/esql/libixos.a $(INFORMIXDIR)/lib/esql/libixgen.a $(INFORMIXDIR)/lib/esql/libixgls.a $(INFORMIXDIR)/lib/esql/libixglx.a -lnsl -lsocket -lm -laio -ldl -lelf
CCFLAGS= -c -Dsparc -DPRINT -g -DSET_UID -I. -I/usr/include -I$(INFORMIXDIR)/incl/esq
l -I$(INFORMIXDIR)/incl/tools -mt

CPFLAGS=-g -mt

CC=/export/vol/SUNWspro5.0/bin/cc
CP=/export/vol/SUNWspro5.0/bin/CC

OBJS=tstmainc.o tstsubcp.o tstsubc.o

.SUFFIXES: .ec

all: tstappc
@echo done

tstappc: $(OBJS)
$(CP) -o $@ $(OBJS) $(LDFLAGS) $(LIBDIRS) $(LIBRARIES)

tstmainc.o: tstmainc.c
$(CC) $(CCFLAGS) tstmainc.c $(INCLUDES)

tstsubcp.o: tstsubcp.c
$(CP) $(CPFLAGS) -c tstsubcp.c $(INCLUDES)

tstsubc.o: tstsubc.c
$(CC) $(CCFLAGS) -c tstsubc.c $(INCLUDES)

clean:
rm $(OBJS) core

install:
$(CP) -o tstmainc -O -L$(INFORMIXLB) $(OBJS) $(LIBRARIES) $(LIBDIRS)
## strip tstmainc

.ec.c:
esql -g -e `pwd`/$*.ec

.c.o:
$(CP) -c $(CPFLAGS) $(INCLUDES) `pwd`/$*.c


==============================
tstmainc.ec - Main Program in C
==============================
#include <strings.h>
#include <stdio.h>
#include <stdlib.h>
$include sqlca;
#include <sys/types.h>
#include <sys/file.h>
#include <sys/utsname.h>
#include <sys/stat.h>
#include <dirent.h>
#include &quot;tstapp.h&quot;

int main() {

int mt530cnt;
printf (&quot;PRINTF : Main C Program Start\n&quot;);

tstsubc();

EXEC SQL WHENEVER ERROR CALL sqlerror;
EXEC SQL DATABASE swift_in;

mt530cnt = sql_select(&quot;SELECT count(*) FROM mt530 WHERE swift_msg_type = 531&quot;);

printf (&quot;PRINTF : There are %d 531s in mt530.\n&quot;,mt530cnt);
printf (&quot;PRINTF : Main C Program End\n&quot;);

return(0);
}

==============================
tstsubc.c - Sub Program in C
==============================
#include <stdio.h>
#include &quot;tstapp.h&quot;

void tstsubc() {
printf (&quot;PRINTF : --Sub C Program - tstsubc\n&quot;);
}

==============================
tstsubcp.ec - Sub Program in C++
==============================
#include <stdlib.h>
#include <iostream.h>

extern &quot;C&quot;
{
$include sqlca;
}
#include <sys/types.h>
#include <sys/file.h>
#include <sys/utsname.h>
#include <sys/stat.h>

#include &quot;tstapp.h&quot;

void sqlerror(void)
{
cerr << &quot;SQL = &quot; << sqlca.sqlcode;
if (sqlca.sqlerrd[1] != 0)
cerr << &quot;, ISAM = &quot; << sqlca.sqlerrd[1];
cerr << endl;
exit(1);
}

int sql_select(char *txt)
{
printf (&quot;PRINTF : --Sub C++ Program Start - sql_select\n&quot;);

/* COUT HANGS HERE!!! */
cout << &quot;COUT : --Sub C++ Program Start - sql_select&quot; << endl;

EXEC SQL BEGIN DECLARE SECTION;
char *stmt = txt;
int retval;
EXEC SQL END DECLARE SECTION;

EXEC SQL WHENEVER ERROR CALL sqlerror;

EXEC SQL PREPARE p_select FROM :stmt;
EXEC SQL DECLARE c_select CURSOR FOR p_select;
EXEC SQL OPEN c_select;
EXEC SQL FETCH c_select INTO :retval;
EXEC SQL CLOSE c_select;
EXEC SQL FREE c_select;

#if ESQLC_VERSION >= 600
EXEC SQL FREE p_select;
#endif

printf (&quot;PRINTF : --Sub C++ Program End - sql_select\n&quot;);

/* COUT will hang here!!! */
cout << &quot;COUT : --Sub C++ Program End - sql_select&quot; << endl;

return(retval);
}

==============================
tstapp.h
==============================
#ifdef __cplusplus
extern &quot;C&quot; {
#endif
void tstsubc();
void sqlerror(void);
int sql_select(char *);
#ifdef __cplusplus
}
#endif

 
Hello, I havn't read all the code, but I can tell you why your program hangs:

Its definetely a fault in your make file, double check it!
or delete all .o files and recompile.

i had the same thing,also wondering why a simple cout did not work anymore,and I deleted all the .o files and recompiled and it worked!

good luck .
Greetz,

The Muppeteer.

themuppeteer@hotmail.com

Don't eat yellow snow...
 
I have checked it a hundred times, trying different combinations and options, and deleted all *.o,esql generated *.c and the executable and recompiled everything.

Any suggestion what could be wrong in the make file?

Thanks!

 
Yeah,
I had it when I changed a function in a class,compiled
the program again, but because I forgot a file dependency , a file did not recompile,therefore it crashed from the moment I made an instance of that class...but now I'm talking about it,I also remember that if I put cout on the first line of that program,that cout worked...
Does printf work in the sub program?
Could it be that C and C++ can't work together like that ?
And that cout is the first C++ function you call ? (and therefore crashes)
I've never worked with seperated programs that way before.
Sorry I was so sure of the makefile error...I'm not so sure anymore.
Can you compile your C program like it was a C++ program ( with the c++ compiler)? perhaps when it is the same compiler,it will be more compatible...



Greetz,

The Muppeteer.

themuppeteer@hotmail.com

Don't eat yellow snow...
 
The printf works everywhere.

Given the same scenario, except that the main program is in C++, cout as its first command does not work either.

I'm not even sure if this C/C++ combination should work but I've tested it using a lower version (Informix5) and it is working properly. I'm using Informix 7.31 now, when it's failing.

If I remember right, I've tried to compile everything in C++ but it produced the same result. Let me just try that again. Though I may have a bit of a problem here when I apply this in the real project because of C++ strict prototyping syntax checking, and all the old C codes are not properly prototyped.

Thanks for your continous advice!




 
If everything would fail,there is always a tricky way around...
You could compile your your c++ program to an executable,
that shows the result on the screen, but you call it from you main c program with &quot;popen&quot; (check the internet),in that way, you will get the output in a FILE var in your program (FILE mysql=popen(&quot;...&quot;,&quot;...&quot;);) . Then you can parse that file var and get you data.

Hey, have you tried to write a small main in your C++ program to see if it works seperately? (just in case)
Can't you write your C++ program as C ? I mean do you need C++ to do this ?


Good luck. Greetz,

The Muppeteer.

themuppeteer@hotmail.com

Don't eat yellow snow...
 
I tried putting the main in the C++ and it works fine.

Yes, unfortunately the C++ programs are too complicated to convert to C, so I need to do this is C++.

I'll try to read about the popen stuff.

With the production coming up very very soon, I need all the luck I can get!

Thanks for all your advices and suggestions.


 
If it works fine with the main,then it has to be that the 2 (C and C++) don't work together.
I think popen would be the best for you,
I'll give you an example:

void main()
{
FILE *result=popen(&quot;ls&quot;,&quot;r&quot;);// execute ls and it is a
//read pipe
while( (t=fgetc(result))!=EOF)cout<<t;
cout<<endl;
}

I did had to look up how I had to interrogate a FILE var,because I didn't know that (cause I'm a C++ programmer, not C),but I found it in a book.

This morning I read on a site that the same exists for C++:

Hope it works...

Greetz,

The Muppeteer.

themuppeteer@hotmail.com

Don't eat yellow snow...
 
two things i would try is putting a flush into the cout

cout<<&quot;blah&quot;<<flush<<endl;

or cerr

cerr<<&quot;blah&quot;<<endl;
cerr<<&quot;blah&quot;<<flush<<endl;


Matt
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top