Sun Studio 9 C++ compiler 5.6 and use of -REENTRANT
Hi,
I have upgraded my C++ compiler to Sun Studio 9 Sun C++ 5.6 from my earlier Sun Forte Developer 7 Compiler.
After initial errors while compiling my string class library, I got it compiled using -library=iostream I am still having problms using my C++ program.
My program getfile.c in C++ compiles OK but when I executes it is coming with following error
"getfile: syntax error at line 2 : `newline or ;' unexpected"
My getfile.c has first few lines as follows:
++++++++++++++++++++++++++++++++++++++++++++++
#include <stdlib.h>
#include <unistd.h>
#include <ctype.h>
#include <iostream.h>
#include <cmqc.h>
#define _REENTRANT
#include <string.h>
#include <wait.h>
#include <sys/wait.h>
#include <imqi.hpp> //MQSeries MQI
#include <cmqc.h>
#include "apperror.h"
#include "appswitch.h"
#include "array.h"
#include "errlog.h"
#include "general.h"
#include "datetime.h"
#include "infolog.h"
#include "cmms_files.h"
+++++++++++++++++++++++++++++++++++++++++++++++++++++
I can't find error in line 2 which makes me think that I am still having some issues with my new compiler and my old code.
I am using compiler switches as follows.
+++++++++++++++++++++++++++++++++++++++++++++++++++++++
/opt/SUNWspro/bin/CC -g -xs -xar -library=iostream -YP, /ford/thishost/u/rbhave/mqrouter/lib:/usr/lib:/opt/SUNWspro/prod/lib -I/ford/thishost/u/rbhave/mqrouter/include -xildoff -c getfile.c
/opt/SUNWspro/bin/CC -g -xs -xar -library=iostream -YP,/ford/thishost/u/rbhave/mqrouter/lib:/usr/lib:/opt/SUNWspro/prod/lib -I/ford/thishost/u/rbhave/mqrouter/include -xildoff getfile.o -o getfile -Lford/thishost/u/rbhave/mqrouter/lib -L/opt/mqm/lib -lXm -lXt -lX11 -lsocket -lnsl -lstring -lipc -lutils -lstring -limqs23as -limqb23as -lmqm -lmqmalda -lmqmaldb -lmqmax -lmqmcb -lmqmcs -lmqmzse
+++++++++++++++++++++++++++++++++++++++++++++++++++++++
Also I could not find what exactly #define _REENTRANT does in my code?
Any help will be much appreciated.
Thanks
Ravi
[2232 byte] By [
Ravi_Bhave] at [2007-11-26 11:05:04]

# 1
Check getfile.c for invisible characters in the file that might be confusing the compiler. Run
od -c getfile.c | more
and look at what is around the line containing #include <unistd.h>
If you see non-printable characters other than space, \t or \n, remove those characters from the file.
Defining _REENTRANT in a source file is a very bad idea. This macro should be defined for multi-threaded code, and not otherwise. When used in multithreaded code, it must be defined in every compilation and in a way that applies it to every system header.
Your example breaks these rules, leading to inconsistent results in different .o files, and is likely to cause unpredictable progam behavior.
You never need to define _REENTRANT yourself. If the program is mult-threaded, use the option -mt on every command line in the entire program. The -mt option causes _REENTRANT to be defined automatically. Link the program with -lthread or -lpthread.
If the program is not multithreaded, you should not define _REENTRANT anywhere.
BTW, it sounds odd to be upgrading to Sun Studio 9 -- an End-Of-Life product. You should get Sun Studio 11 instead, which is free, and supports the same range of OS versions as Studio 9. Get it here:
http://developers.sun.com/sunstudio/
# 2
Many thanks for your help.
As per your recommendation I have removed _REENTRANT and compiled my program with errors as follows
++++++++++++++++++++++++++++++++++++++++++++++++++++
/opt/SUNWspro/bin/CC -xar -library=iostream -YP,/ford/thishost/u/rbhave/mqrouter/lib:/usr/lib:/opt/SUNWspro/prod/lib -I/ford/thishost/u/rbhave/mqrouter/include -xildoff -c getfile.c
"getfile.c", line 526: Error: The function "strtok_r" must have a prototype.
"getfile.c", line 529: Error: The function "strtok_r" must have a prototype.
"getfile.c", line 532: Error: The function "strtok_r" must have a prototype.
"getfile.c", line 535: Error: The function "strtok_r" must have a prototype.
"getfile.c", line 538: Error: The function "strtok_r" must have a prototype.
"getfile.c", line 541: Error: The function "strtok_r" must have a prototype.
"getfile.c", line 544: Error: The function "strtok_r" must have a prototype.
"getfile.c", line 547: Error: The function "strtok_r" must have a prototype.
"getfile.c", line 550: Error: The function "strtok_r" must have a prototype.
"getfile.c", line 553: Error: The function "strtok_r" must have a prototype.
"getfile.c", line 556: Error: The function "strtok_r" must have a prototype.
"getfile.c", line 559: Error: The function "strtok_r" must have a prototype.
"getfile.c", line 720: Error: The function "strtok_r" must have a prototype.
"getfile.c", line 721: Error: The function "strtok_r" must have a prototype.
14 Error(s) detected.
*** Error code 14
make: Fatal error: Command failed for target `getfile.o'
++++++++++++++++++++++++++++++++++++++++++++++++++++++++
When I compile with -mt option I can compile my program with no errors but when I execute it, I get segmentation fault core dump.
I tried to put some print/cout statements in my program from start to see where it is giving me the error but I do not get any idea where it is failing as it does not print my cout/print statements from line 1 also.
Which makes me think that I am not on right track still.
I compile my string and other library class using following options.
++++++++++++++++++++++++++++++++++++++++++++++++++++++++
CC -xar -library=iostream -xildoff -i -features=no%strictdestrorder -I. -I/ford/thishost/u/rbhave/mqrouter/include -c strstats.C
CC -xar -library=iostream -xildoff -i -features=no%strictdestrorder -I. -I/ford/thishost/u/rbhave/mqrouter/include -c strio.C
CC -xar -library=iostream -xildoff -i -features=no%strictdestrorder -I. -I/ford/thishost/u/rbhave/mqrouter/include -c strfrnds.C
CC -xar -library=iostream -xildoff -i -features=no%strictdestrorder -I. -I/ford/thishost/u/rbhave/mqrouter/include -c strctor.C
CC -xar -library=iostream -xildoff -i -features=no%strictdestrorder -I. -I/ford/thishost/u/rbhave/mqrouter/include -c strops.C
CC -xar -library=iostream -xildoff -i -features=no%strictdestrorder -I. -I/ford/thishost/u/rbhave/mqrouter/include -c strmfs.C
++++++++++++++++++++++++++++++++++++++++++++++++++++++++
I can not use latest Sun compiler 11 as I am using MQ series(5.3) software from IBM which says that I can use only version Sun One Studio 7 or 8 or 9 Compiler collection.
Please help me to resolve this issue. Thanks again.
Ravi
# 4
Thanks for your help. Your suggestions helped me to get on right path. ie. Compiler switches I was using were wrong.
After using following switches my program started working fine.
++++++++++++++++++++++++++++++++++++++++++++++++++++
-xarch=v8plus -mt -I/opt/mqm/inc -L/opt/mqm/lib -R/opt/mqm/li
b -R/usr/lib/32 -limqb23as -lmqmcs -lmqmzse -lsocket -lnsl -ldl -library=iostrea
m -YP,/ford/thishost/u/rbhave/mqrouter/lib:/usr/lib:/opt/SUNWspro/prod/lib
++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Thanks again
Ravi