g++ compilation problem

Hi,

I'm compiling a bunch of files using g++. The files compile fine in Ubuntu but they won't compile in CentOS (5.8).
-
Last edited on
Sorry, the first post got submitted accidentally. This is my full post

Hi,

I'm compiling a bunch of files using g++. The files compile fine in Ubuntu but they won't compile in CentOS (5.8).

I get this error when I try to compile

g++ -c -Wall -std=c++0x -Wformat src/handler.cpp -o src/handler.o
g++ src/handler.o -lpthread -lm -o bin/xyz

1
2
3
src/handler.o: In function `ApplicationHandler::CheckInput(bool*)':
handler.cpp:(.text+0x2d93): undefined reference to `std::basic_ifstream<char, std::char_traits<char> >::basic_ifstream(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::_Ios_Openmode)'
collect2: ld returned 1 exit status


I'm using g++44 to compile.

Any ideas as to why its happening?

PS: I'm using same commands in both Ubuntu and CentOS. The only difference is in linux distribution. nothing else.
Does this still happen when you perform the compiling and linking steps together?
Yes.

These are my gcc versions from Ubuntu and CentOS

gcc version 4.4.6 20110731 (Red Hat 4.4.6-3) (GCC)
gcc version 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5)

There is no problem with the c files per se but only problem is with the linux distribution because it compiles fine in Ubuntu but not CentOS.
An undefined reference to something means that you are missing a library to link to. Quite possibly (but highly unlikely) that the gcc for CentOS is not built/installed correctly since the undefined reference should be part of libstdc++.

Can you try the following command when linking and show the results (caution: it may be very long, use the program output tags):
 
g++ -v src/handler.o -lpthread -lm -o bin/xyz
Hi,

I tried it and I got the following error after running the command as you suggested

g++ -v src/handler.o -lpthread -lm -o bin/xyz

1
2
3
4
5
6
7
8
9
10
11
12
13
Using built-in specs.
Target: x86_64-redhat-linux6E
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --disable-gnu-unique-object --with-as=/usr/libexec/binutils220/as --enable-languages=c,c++,fortran --disable-libgcj --with-mpfr=/builddir/build/BUILD/gcc-4.4.6-20110824/obj-x86_64-redhat-linux6E/mpfr-install/ --with-ppl=/builddir/build/BUILD/gcc-4.4.6-20110824/obj-x86_64-redhat-linux6E/ppl-install --with-cloog=/builddir/build/BUILD/gcc-4.4.6-20110824/obj-x86_64-redhat-linux6E/cloog-install --with-tune=generic --with-arch_32=i586 --build=x86_64-redhat-linux6E
Thread model: posix
gcc version 4.4.6 20110731 (Red Hat 4.4.6-3) (GCC) 
COMPILER_PATH=/usr/libexec/gcc/x86_64-redhat-linux6E/4.4.6/:/usr/libexec/gcc/x86_64-redhat-linux6E/4.4.6/:/usr/libexec/gcc/x86_64-redhat-linux6E/:/usr/lib/gcc/x86_64-redhat-linux6E/4.4.6/:/usr/lib/gcc/x86_64-redhat-linux6E/:/usr/libexec/gcc/x86_64-redhat-linux6E/4.4.6/:/usr/libexec/gcc/x86_64-redhat-linux6E/:/usr/lib/gcc/x86_64-redhat-linux6E/4.4.6/:/usr/lib/gcc/x86_64-redhat-linux6E/
LIBRARY_PATH=/usr/lib/gcc/x86_64-redhat-linux6E/4.4.6/:/usr/lib/gcc/x86_64-redhat-linux6E/4.4.6/:/usr/lib/gcc/x86_64-redhat-linux6E/4.4.6/../../../../lib64/:/lib/../lib64/:/usr/lib/../lib64/:/usr/lib/gcc/x86_64-redhat-linux6E/4.4.6/../../../:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS='-v' '-o' 'bin/xyz' '-shared-libgcc' '-mtune=generic'
 /usr/libexec/gcc/x86_64-redhat-linux6E/4.4.6/collect2 --eh-frame-hdr -m elf_x86_64 --hash-style=gnu -dynamic-linker /lib64/ld-linux-x86-64.so.2 -o bin/xyz /usr/lib/gcc/x86_64-redhat-linux6E/4.4.6/../../../../lib64/crt1.o /usr/lib/gcc/x86_64-redhat-linux6E/4.4.6/../../../../lib64/crti.o /usr/lib/gcc/x86_64-redhat-linux6E/4.4.6/crtbegin.o -L/usr/lib/gcc/x86_64-redhat-linux6E/4.4.6 -L/usr/lib/gcc/x86_64-redhat-linux6E/4.4.6 -L/usr/lib/gcc/x86_64-redhat-linux6E/4.4.6/../../../../lib64 -L/lib/../lib64 -L/usr/lib/../lib64 -L/usr/lib/gcc/x86_64-redhat-linux6E/4.4.6/../../..  src/handler.o -lpthread -lstdc++ -lm -lgcc_s -lgcc -lc -lgcc_s -lgcc /usr/lib/gcc/x86_64-redhat-linux6E/4.4.6/crtend.o /usr/lib/gcc/x86_64-redhat-linux6E/4.4.6/../../../../lib64/crtn.o
src/handler.o: In function `ApplicationHandler::CheckInput(bool*)':
handler.cpp:(.text+0x2d93): undefined reference to `std::basic_ifstream<char, std::char_traits<char> >::basic_ifstream(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::_Ios_Openmode)'
collect2: ld returned 1 exit status
make: *** [bin/xyz] Error 1
SOLVED

Ok the problem seems with the gcc package in CentOS. The gcc package was old while the 'c files' require latest gcc package. I had to compile latest gcc from source since later than gcc4.4 was not available with Yum install.

Thanks.
Last edited on
Topic archived. No new replies allowed.