在WINDOWS環境的CODE::BLOCKS裏面寫好的測試程序,想拿到Linux裏面試驗一把。報錯: undefined reference to `std::ios_base::Init::Init()
1. 確認是否安裝 gcc-c++的支持庫
[root@ht168 ~]# rpm -qa| grep gcc
gcc-4.8.5-11.el7.x86_64
gcc-c++-4.8.5-11.el7.x86_64
libgcc-4.8.5-11.el7.x86_64
libgcc-4.8.5-11.el7.i686
2. 錯誤信息如下
[ecsmid@ht168 zhouc]$ gcc -m64 -o zhou while_test.cpp
/tmp/ccIp6o1B.o: In function `main':
while_test.cpp:(.text+0x2d): undefined reference to `std::cout'
while_test.cpp:(.text+0x32): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
while_test.cpp:(.text+0x3e): undefined reference to `std::cin'
while_test.cpp:(.text+0x43): undefined reference to `std::istream::operator>>(int&)'
while_test.cpp:(.text+0x58): undefined reference to `std::basic_ios<char, std::char_traits<char> >::operator void*() const'
while_test.cpp:(.text+0x6c): undefined reference to `std::cout'
while_test.cpp:(.text+0x71): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
while_test.cpp:(.text+0x7e): undefined reference to `std::ostream::operator<<(int)'
/tmp/ccIp6o1B.o: In function `__static_initialization_and_destruction_0(int, int)':
while_test.cpp:(.text+0xaa): undefined reference to `std::ios_base::Init::Init()'
while_test.cpp:(.text+0xb9): undefined reference to `std::ios_base::Init::~Init()'
3. 解決:
Linux下面採用gcc 編譯 c++的代碼第一需要安裝gcc-c++支持庫,其次編譯的時候需要添加參數 -lstdc++。正確的編譯方式如下
[ecsmid@ht168 zhouc]$ gcc -m64 -lstdc++ -Wall -o zhou while_test.cpp
[ecsmid@ht168 zhouc]$