fedora上的gcc链接器错误:未定义引用 [英] gcc linker errors on fedora: undefined reference
问题描述
我试图在Fedora / gcc上编译/运行我的程序(这在Windows / MSVC上正常工作)。编译是好的现在,链接似乎是一个问题。根据症状和此后未定义的引用`__gxx_personality_sj0 ,在我看来问题可能在编译器/链接器结构类型中有些不匹配。
- 我刚刚卸载了我发现的所有
yum list | egrep gcc | g ++ | c ++
使用yum remove
和 - 然后运行
yum安装gcc gcc-c ++
。
注意:
- 您在下面看到的所有输出都是之后的步骤。
- gcc输出的最后一行是:
collect2:ld返回1 exit
- 代码是编译清洁,没有警告/错误。 几乎所有未定义的引用都是
- 我只有一个源文件
Main.C
)。
namespace std
我试图提供所有我能想到的信息,如果您需要更多信息,请告诉我。 p>
[root @ myTestMachine kash]#ls -Altr
total 44K
-rw-r - r- - 。 1 root root 3.5K Sep 11 11:34 .bashrc
-rw-r - r--。 1 root root 2.6K Sep 11 13:50 MyCommon.h
-rw-r - r--。 1 root root 3.5K Sep 11 14:01 Main.cpp
-rw-r - r--。 1 root root 26K Sep 11 14:02 sort.h
-rw-r - r--。 1 root root 80 Sep 11 14:26 a.cpp
[root @ myTestMachine kash]#
[root @ myTestMachine kash]#gcc -c -std = c ++ 0x Main.cpp
[root @ myTestMachine kash]#echo $?
0
[root @ myTestMachine kash]#
[root @ myTestMachine kash]#rm Main.o
[root @ myTestMachine kash]#
[root @ myTestMachine kash]#gcc -std = c ++ 0x Main.cpp
/tmp/cckR4oL1.o:在函数`main :: {lambda(std :: basic_string< char,std :: char_traits< char>,std :: allocator< char>>)#1} :: operator()(std :: basic_string< char,std :: char_traits< char>,std :: allocator< char>>)const':
Main.cpp :(。text + 0x270):未定义引用`std :: basic_string< char,std :: char_traits< char>,std :: allocator< char> > :: c_str()const'
..... LOTS其他这种未定义的refs;请参见下面的pastebin链接完整列表.....
Main.cpp :( .text._ZSt10_ConstructISsISsEEvPT_DpOT0 _ [_ ZSt10_ConstructISsISsEEvPT_DpOT0 _] + 0x37):未定义的引用`std :: basic_string< char,std :: char_traits< char>,std :: allocator< char> > :: basic_string(std :: basic_string< char,std :: char_traits< char>,std :: allocator< char>&&&&)'
/tmp/cckR4oL1.o:(.eh_frame + 0x1cb):未定义的引用`__gxx_personality_v0'
collect2:ld返回1退出状态
[root @ myTestMachine kash]#
在此处完成gcc输出: http://pastebin.com/PvycFHQM
[root @ myTestMachine kash]#uname -a
Linux myTestMachine 3.1.0-7.fc16.i686.PAE#1 SMP Tue Nov 1 20:53:45 UTC 2011 i686 i686 i386 GNU / Linux
[root @ myTestMachine kash]#
[root @ myTestMachine kash]#yum list gcc gcc-c ++
加载的插件:langpacks,presto,refresh-packagekit
安装包
gcc.i686 4.6.3-2.fc16 @updates
gcc-c ++。i686 4.6.3-2.fc16 @updates
[root @ myTestMachine kash]#
[root @ myTestMachine kash]#rpm -qa | grep gcc
gcc-4.6.3-2.fc16.i686
libgcc-4.6.3-2.fc16.i686
arm-gp2x-linux-gcc-4.1.2-12。 fc15.i686
gcc-c ++ - 4.6.3-2.fc16.i686
arm-gp2x-linux-gcc-c ++ - 4.1.2-12.fc15.i686
[root @ myTestMachine kash]#
[root @ myTestMachine kash]#rpm -qa | grep g ++
[root @ myTestMachine kash]#
[root @ myTestMachine kash]#rpm -qa | grep c ++
libsigc ++ 20-2.2.10-1.fc16.i686
libstdc ++ - devel-4.6.3-2.fc16.i686
libstdc ++ - 4.6.3-2.fc16 .i686
gcc-c ++ - 4.6.3-2.fc16.i686
arm-gp2x-linux-gcc-c ++ - 4.1.2-12.fc15.i686
[root @ myTestMachine kash]#
[root @ myTestMachine kash]#rpm -qa | grep ld
rpm-build-libs-4.9.1.2-1.fc16.i686
libldb-1.1.0-1.fc16.i686
openldap-2.4.26-1.fc16。 1.i686
[root @ myTestMachine kash]#rpm -qa | grep -w ld
[root @ myTestMachine kash]#
[root @ myTestMachine kash]#ld -version
GNU ld版本2.21.53.0.1-6.fc16 20110716
版权所有2011 Free Software Foundation,Inc.
此程序是免费软件;您可以根据
GNU通用公共许可证版本3或(在您的选择)更高版本的条款重新分配它。
此程序绝对没有保修。
[root @ myTestMachine kash]#
[root @ myTestMachine kash]#gcc --version
gcc(GCC)4.6.3 20120306(Red Hat 4.6.3-2)
版权所有(C)2011自由软件基金会,
这是免费软件;请参阅复制条件的来源。有NO
保修;甚至不适用于适销性或特定用途的适用性。
[root @ myTestMachine kash]#
解决方案>不要这样做:
gcc -std = c ++ 0x Main.cpp
$>
$ b c> g ++ -std = c ++ 0x Main.cpp
>
gcc Main.cpp -lstdc ++
b $ b底线是:请确保您在链接阶段使用g ++或-lstdc ++ 。您可以在编译阶段使用前端。
I'm trying to compile/run my program (which is working fine on Windows/MSVC) on Fedora/gcc. Compilation is fine now, linking seems to be a problem. Based on symptoms and this post undefined reference to `__gxx_personality_sj0, it seemed to me that the problem might be some mismatch in compiler/linker arch type. Anyway, I'm no expert so
- I just uninstalled everything I found with
yum list | egrep gcc|g++|c++
usingyum remove
and- then ran
yum install gcc gcc-c++
.NOTES:
- All output you see below is AFTER steps above.
- Last line of gcc output is:
collect2: ld returned 1 exit status
- Code is compile clean, no warnings/errors.
- Almost all undefined references are to names in
namespace std
- I have only one source file
Main.C
(and 2 headers).I've tried to provide all the info I could think of, let me know if you need anymore info.
[root@myTestMachine kash]# ls -Altr total 44K -rw-r--r--. 1 root root 3.5K Sep 11 11:34 .bashrc -rw-r--r--. 1 root root 2.6K Sep 11 13:50 MyCommon.h -rw-r--r--. 1 root root 3.5K Sep 11 14:01 Main.cpp -rw-r--r--. 1 root root 26K Sep 11 14:02 sort.h -rw-r--r--. 1 root root 80 Sep 11 14:26 a.cpp [root@myTestMachine kash]# [root@myTestMachine kash]# gcc -c -std=c++0x Main.cpp [root@myTestMachine kash]# echo $? 0 [root@myTestMachine kash]# [root@myTestMachine kash]# rm Main.o [root@myTestMachine kash]# [root@myTestMachine kash]# gcc -std=c++0x Main.cpp /tmp/cckR4oL1.o: In function `main::{lambda(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)#1}::operator()(std::basic_string<char, std::char_traits<char>, std::allocator<char> >) const': Main.cpp:(.text+0x270): undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::c_str() const' ..... LOTS of other such undefined refs; see complete list on pastebin link below ..... Main.cpp:(.text._ZSt10_ConstructISsISsEEvPT_DpOT0_[_ZSt10_ConstructISsISsEEvPT_DpOT0_]+0x37): undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(std::basic_string<char, std::char_traits<char>, std::allocator<char> >&&)' /tmp/cckR4oL1.o:(.eh_frame+0x1cb): undefined reference to `__gxx_personality_v0' collect2: ld returned 1 exit status [root@myTestMachine kash]#
Complete gcc output here: http://pastebin.com/PvycFHQM
[root@myTestMachine kash]# uname -a Linux myTestMachine 3.1.0-7.fc16.i686.PAE #1 SMP Tue Nov 1 20:53:45 UTC 2011 i686 i686 i386 GNU/Linux [root@myTestMachine kash]# [root@myTestMachine kash]# yum list gcc gcc-c++ Loaded plugins: langpacks, presto, refresh-packagekit Installed Packages gcc.i686 4.6.3-2.fc16 @updates gcc-c++.i686 4.6.3-2.fc16 @updates [root@myTestMachine kash]# [root@myTestMachine kash]# rpm -qa | grep gcc gcc-4.6.3-2.fc16.i686 libgcc-4.6.3-2.fc16.i686 arm-gp2x-linux-gcc-4.1.2-12.fc15.i686 gcc-c++-4.6.3-2.fc16.i686 arm-gp2x-linux-gcc-c++-4.1.2-12.fc15.i686 [root@myTestMachine kash]# [root@myTestMachine kash]# rpm -qa | grep g++ [root@myTestMachine kash]# [root@myTestMachine kash]# rpm -qa | grep c++ libsigc++20-2.2.10-1.fc16.i686 libstdc++-devel-4.6.3-2.fc16.i686 libstdc++-4.6.3-2.fc16.i686 gcc-c++-4.6.3-2.fc16.i686 arm-gp2x-linux-gcc-c++-4.1.2-12.fc15.i686 [root@myTestMachine kash]# [root@myTestMachine kash]# rpm -qa | grep ld rpm-build-libs-4.9.1.2-1.fc16.i686 libldb-1.1.0-1.fc16.i686 openldap-2.4.26-1.fc16.1.i686 [root@myTestMachine kash]# rpm -qa | grep -w ld [root@myTestMachine kash]# [root@myTestMachine kash]# ld -version GNU ld version 2.21.53.0.1-6.fc16 20110716 Copyright 2011 Free Software Foundation, Inc. This program is free software; you may redistribute it under the terms of the GNU General Public License version 3 or (at your option) a later version. This program has absolutely no warranty. [root@myTestMachine kash]# [root@myTestMachine kash]# gcc --version gcc (GCC) 4.6.3 20120306 (Red Hat 4.6.3-2) Copyright (C) 2011 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. [root@myTestMachine kash]#
解决方案Don't do this:
gcc -std=c++0x Main.cpp
Do this instead:
g++ -std=c++0x Main.cpp
Or do this instead:
gcc Main.cpp -lstdc++
The bottom line is: make sure that you either use "g++" or "-lstdc++" at the link stage. You can use either front end during the compile stage.
这篇关于fedora上的gcc链接器错误:未定义引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文