8

I am trying to write a simple C++ program on an AIX Box. The program is given below:

# include <iostream>
# include <fstream>
using namespace std ;


int main()
{
    ofstream of ;
    of.open("license.txt") ;
    of<<"hello"<<endl ;
    of.close() ;
}

My LDFLAGS has is set as following:

-maix64 -L/disk3/TOOLS/GCCTools/gcc-4.5.1/lib/ppc64 \
-L/disk3/TOOLS/GCCTools/gcc-4.5.1/lib/gcc/powerpc-ibm-aix6.1.0.0/4.5.1/ppc64 \
-L/disk3/TOOLS/GCCTools/gcc-4.5.1/lib/gcc/powerpc-ibm-aix6.1.0.0/4.5.1 \
-L/disk3/TOOLS/OPENSSL/lib

CFLAGS is:

-O2 -maix64 -I/disk3/TOOLS/OPENSSL/include -D_ALL_SOURCE -D_XOPEN_SOURCE \
-D_XOPEN_SOURCE_EXTENDED -DSS_64BIT_SERVER -D_POSIX_SOURCE -D__64BIT__ \
-I/disk3/TOOLS/OPENSSL/include -I/usr/include \
-I/disk3/TOOLS/GCCTools/gcc-4.5.1/lib/gcc/powerpc-ibm-aix6.1.0.0/4.5.1/include

The program compiles fine. But when I try to run the same, the program comes out with a segmentation fault. I ran the same with gdb and found the following issue when I use ofstream:

Program received signal SIGSEGV, Segmentation fault.
0x09000000036107c4 in std::locale::operator=(std::locale const&) (this=
findvar.c:706: internal-error: value_from_register: Value not stored anywhere!

Any idea on why this is happening? Any help is appreciated :)

Note: fstream in itself works...

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Ricketyship
  • 644
  • 2
  • 7
  • 22

3 Answers3

1

I meet the same error. The key points to repro the error are: 1 use std::stream(such as std::ofstream) in share libary; 2 use pthread function (such as pthread_self) in share library; 3 use "-O2" to optimize code. Then it shows "Segmentation fault (core dumped)".

AIX provides 2 versions(64bit) of libstdc++.a. (see http://www.perzl.org/aix/index.php?n=Main.GCCBinariesVersionNeutral) 64-bit compilation, non-thread-safe (<prefix>/ppc64) 64-bit compilation, thread-safe (<prefix>/pthread/ppc64)

My solution is: change LIBPATH to use "<prefix>/pthread/ppc64" version. such as set LIBPATH as "/opt/freeware/lib/gcc/powerpc-ibm-aix6.1.0.0/4.6.1/pthread/ppc64/"

It works well in my machine.

Tushar Gupta - curioustushar
  • 58,085
  • 24
  • 103
  • 107
Dong Wang
  • 11
  • 1
  • @TusharGupta Using pthread option works. Changing LIBPATH also works. It's safer to user -lpthread as this makes sure that any other links which might have been missed out will be included. – Ricketyship Jun 11 '13 at 09:20
  • Note: the two versions of libstdc++ (pthread'ed and pthread-less) are incompatible. Usually, you cannot tell other parts of the complete program will be threaded or not, so the safe way is always using the threaded version. (A similar problem is large-file-support (in 32-bit mode): you cannot mix code compiled with different compiler settings.) – Lorinczy Zsigmond Sep 24 '19 at 12:10
0

I have analyzed the issue and found a work around.

Here is what i did:

I did an ldd on my testprog executable:

ldd test
test needs:
         /usr/lib/threads/libc.a(shr_64.o)
         /disk3/TOOLS/GCCTools/gcc-4.5.1/lib/gcc/powerpc-ibm-aix6.1.0.0/4.5.1/pthread/ppc64/libstdc++.a(libstdc++.so.6)
         /disk3/TOOLS/GCCTools/gcc-4.5.1/lib/gcc/powerpc-ibm-aix6.1.0.0/4.5.1/pthread/ppc64/libgcc_s.a(shr.o)
         /unix
         /lib/libcrypt.a(shr_64.o)
         /lib/libpthreads.a(shr_xpg5_64.o)

I found that pthread's libstdc was being used. This was due to my LIBPATH having this path before /usr

Next, I reset my LIBPATH to exclude all those paths which had pthread's gcc being used, making sure that the other gcc libraries used where available in LIBPATH

Finally I compiled the test program with this new LIBPATH

Note: LD_LIBRARY_PATH is used by linux and LIBPATH is used by AIX.

Cheers!

Ricketyship
  • 644
  • 2
  • 7
  • 22
  • @Christian.K : yes It did. I already had commented on your answer :) If you ask me, both the answers are right. either you include -pthread in your CFLAGS or, make the program use your default libraries than pthread libraries – Ricketyship Jan 31 '12 at 11:12
0

It's been a while, but out of my head: don't you need to add -pthread to the compile / link options?

Christian.K
  • 47,778
  • 10
  • 99
  • 143