<iostream>, related linker error (g++)

Hi All,

I need help to figure it out!Please Help !!!

I am getting following linker errors using g++ and ld (Using GNU/Linux)

***`__static_initialization_and_destruction_0'
***iostream:76: undefined reference to `std::ios_base::Init::Init()'
***iostream:76: undefined reference to `std::ios_base::Init::~Init()'

GNU/Linux using g++

.\Objects\main.o: In function `__static_initialization_and_destruction_0':

/cygdrive/c/Hitex/GnuToolPackageArm/bin/../lib/gcc/arm-hitex-elf/4.1.1/../../../../include/c++/4.1.1/iostream:76: undefined reference to `std::ios_base::Init::Init()'

/cygdrive/c/Hitex/GnuToolPackageArm/bin/../lib/gcc/arm-hitex-elf/4.1.1/../../../../include/c++/4.1.1/iostream:76: undefined reference to `std::ios_base::Init::~Init()'

It is a very simple C++ code which is not using any special C++ features.
The code compiles,links and runs fine when <iostream> is "NOT" included in the main.cpp file.
but as soon as <iostream> is included in the main.cpp file, the code compiles, but the above linker error is generated.

Any help is appreciated!!!

Thanks!
Sounds like you aren't linking with the C++ runtime library.
What command are you using to compile and link? You want to use g++ for both:

g++ -c -o myfile.o myfile.cpp // compile

g++ -o myprog myfile.o // link
Last edited on
Well, thats what I was thinking but it compiles and links fine with other <XXstream> files which make me think it does sees runtime library, the problem seems to be with <iostream> only.

Thanks for the reply!!!
The files are compiled only, using g++ but linked using ld (linker), here it is

arm-hitex-elf-g++ -mcpu=arm7tdmi -c -gdwarf-2 -xc -MD -Wall -O0 -mthumb-interwork -mapcs-frame -fsigned-char -mlittle-endian -marm
-I.\Source\ -I.\ -o .\Objects\interrupt.o .\Source\System\interrupt.cpp

arm-hitex-elf-as -mcpu=arm7tdmi -gdwarf2 -mthumb-interwork
-I.\Source\ -I.\ -o .\Objects\startup.o .\Source\System\startup.s

arm-hitex-elf-g++ -mcpu=arm7tdmi -c -gdwarf-2 -xc++ -Wall -O0 -mthumb-interwork -mapcs-frame -fsigned-char -mlittle-endian -marm
-I.\Source\ -I.\ -o .\Objects\main.o .\main.cpp

arm-hitex-elf-ld -T.\Settings\main.ld.tmp --cref -t -static -nostartfiles -Map=.\DOC\DemoC++.map -stats -lhcclib -lc -lgcc -lm -o .\Objects\DemoC++.elf

Why not let g++ do it all for you?
When you use g++ as the front end to the linker, it will put the correct libraries on the path.

But using ld on its own doesn't add the standard libraries because it make no assumptions about exactly what you want to link.

So either use g++ to do your linking or add the standard libraries by hand. probably you need to at least add -lstdc++
Last edited on
1 - When using g++ as the linker:

Since I am using the IDE, I don't know how to provide g++ the input files, need to figure this out, (research )! - (show stopper)

arm-hitex-elf-g++ -mcpu=arm7tdmi -c -gdwarf-2 -xc -MD -Wall -O0 -mthumb-interwork -mapcs-frame -fsigned-char -mlittle-endian -marm o .\Objects\interrupt.o .\Source\System\interrupt.cpp

arm-hitex-elf-as -mcpu=arm7tdmi -gdwarf2 -mthumb-interwork o .\Objects\startup.o .\Source\System\startup.s

arm-hitex-elf-g++ -mcpu=arm7tdmi -c -gdwarf-2 -xc++ -Wall -O0 -mthumb-interwork -mapcs-frame -fsigned-char -mlittle-endian -marm -I. \Objects\ -I.\ -o .\Objects\main.o .\main.cpp

arm-hitex-elf-g++ -T.\Settings\main.ld.tmp --Wcref -Wt -Wnostartfiles -WMap=.\DOC\DemoC++.map -Wstats -Wstatic -Map=file.map -main.o -o .\Objects\DemoC++.elf
arm-hitex-elf-g++: unrecognized option '-Map=file.map'
arm-hitex-elf-g++: no input files

2 - "add the standard libraries by hand", means to add commad line options for the linker e.g -lstdc++. Please remember that "#include <ios>", "#include <istream>", "#include <ostream>" links fine, only <iostream> have problem.

Any way when I add the -lstdc++ in linkers option, the results are not neat, here it is,

arm-hitex-elf-g++ -mcpu=arm7tdmi -c -gdwarf-2 -xc -MD -Wall -O0 -mthumb-interwork -mapcs-frame -fsigned-char -mlittle-endian -marm -I.\Objects\ -I.\ -o .\Objects\interrupt.o .\Source\System\interrupt.cpp

arm-hitex-elf-as -mcpu=arm7tdmi -gdwarf2 -mthumb-interwork -I.\Objects\ -I.\ -o .\Objects\startup.o .\Source\System\startup.s

arm-hitex-elf-g++ -mcpu=arm7tdmi -c -gdwarf-2 -xc++ -Wall -O0 -mthumb-interwork -mapcs-frame -fsigned-char -mlittle-endian -marm -I.\Objects\ -I.\ -o .\Objects\main.o .\main.cpp

arm-hitex-elf-ld -T.\Settings\main.ld.tmp --cref -t -nostartfiles -Map=.\DOC\DemoC++.map -stats -lhcclib -lc -lgcc -static -lm -lstdc++

-o .\Objects\DemoC++.elf
arm-hitex-elf-ld: mode armelf
.\Objects\interrupt.o
.\Objects\startup.o
.\Objects\main.o
(C:\Hitex\GnuToolPackageArm\bin\..\arm-hitex-elf\lib\interwork/libstdc++.a)eh_personality.o
(C:\Hitex\GnuToolPackageArm\bin\..\arm-hitex-elf\lib\interwork/libstdc++.a)eh_terminate.o
(C:\Hitex\GnuToolPackageArm\bin\..\arm-hitex-elf\lib\interwork/libstdc++.a)eh_term_handler.o
(C:\Hitex\GnuToolPackageArm\bin\..\arm-hitex-elf\lib\interwork/libstdc++.a)eh_throw.o
(C:\Hitex\GnuToolPackageArm\bin\..\arm-hitex-elf\lib\interwork/libstdc++.a)eh_unex_handler.o
(C:\Hitex\GnuToolPackageArm\bin\..\arm-hitex-elf\lib\interwork/libstdc++.a)vterminate.o
(C:\Hitex\GnuToolPackageArm\bin\..\arm-hitex-elf\lib\interwork/libstdc++.a)cp-demangle.o
(C:\Hitex\GnuToolPackageArm\bin\..\arm-hitex-elf\lib\interwork/libstdc++.a)eh_alloc.o

........continue for many pages.
1 - When using g++ as the linker:

Well, I was able to figure out the input files entry point for g++ in the IDE, but after grinding this error shows up (another show stopper!)

arm-hitex-elf-g++ -mcpu=arm7tdmi -c -gdwarf-2 -xc -MD -Wall -O0 -mthumb-interwork -mapcs-frame -fsigned-char -mlittle-endian -marm -I.\Objects\ -I.\ -o .\Objects\interrupt.o .\Source\System\interrupt.cpp

arm-hitex-elf-as -mcpu=arm7tdmi -gdwarf2 -mthumb-interwork -I.\Objects\ -I.\ -o .\Objects\startup.o .\Source\System\startup.s

arm-hitex-elf-g++ -mcpu=arm7tdmi -c -gdwarf-2 -xc++ -Wall -O0 -mthumb-interwork -mapcs-frame -fsigned-char -mlittle-endian -marm -I.\Objects\ -I.\ -o .\Objects\main.o .\main.cpp

arm-hitex-elf-g++ .\Objects\interrupt.o .\Objects\startup.o .\Objects\main.o . -T.\Settings\main.ld.tmp

-o .\Objects\DemoC++.elf

/cygdrive/c/Hitex/GnuToolPackageArm/bin/../lib/gcc/arm-hitex-elf/4.1.1/../../../../arm-hitex-elf/bin/ld: crt0.o: No such file: No such file or directory

collect2: ld returned 1 exit status

There is no exact file name "crt0.o" exist in my system

See the man pages for gcc. In addition to the standard library, I think it includes a console-related library (crt-something).
Are you sure your compiler is installed properly? On my system:

/> find . -name "crt*.*" -print
./usr/lib/gcc/i386-redhat-linux/4.1.1/crtbeginT.o
./usr/lib/gcc/i386-redhat-linux/4.1.1/crtfastmath.o
./usr/lib/gcc/i386-redhat-linux/4.1.1/crtendS.o
./usr/lib/gcc/i386-redhat-linux/4.1.1/crtend.o
./usr/lib/gcc/i386-redhat-linux/4.1.1/crtbeginS.o
./usr/lib/gcc/i386-redhat-linux/4.1.1/crtbegin.o
./usr/lib/gcc/i386-redhat-linux/3.4.6/crtbeginT.o
./usr/lib/gcc/i386-redhat-linux/3.4.6/crtendS.o
./usr/lib/gcc/i386-redhat-linux/3.4.6/crtend.o
./usr/lib/gcc/i386-redhat-linux/3.4.6/crtbeginS.o
./usr/lib/gcc/i386-redhat-linux/3.4.6/crtbegin.o
./usr/lib/i386-redhat-linux4E/lib/crti.o
./usr/lib/i386-redhat-linux4E/lib/crtn.o
./usr/lib/i386-redhat-linux4E/lib/crt1.o
./usr/lib/gnuefi/crt0-efi-ia32.o
./usr/lib/gcc-lib/i386-redhat-linux/2.96/crtendS.o
./usr/lib/gcc-lib/i386-redhat-linux/2.96/crtend.o
./usr/lib/gcc-lib/i386-redhat-linux/2.96/crtbeginS.o
./usr/lib/gcc-lib/i386-redhat-linux/2.96/crtbegin.o
./usr/lib/crti.o
./usr/lib/crtn.o
./usr/lib/crt1.o
./usr/lib/bcc/crt0.o
./usr/lib/bcc/i386/crt0.o
Topic archived. No new replies allowed.