标签: undefined-reference

什么是未定义的引用/未解析的外部符号错误,我该如何解决?

什么是未定义的参考/未解决的外部符号错误?什么是常见原因以及如何修复/预防它们?

随意编辑/添加您自己的.

c++ c++-faq linker-errors unresolved-external undefined-reference

1418
推荐指数
32
解决办法
52万
查看次数

奇怪的链接错误:命令行中缺少DSO

当我编译openvswitch-1.5.0时,我遇到了以下编译错误:

 gcc -Wstrict-prototypes -Wall -Wno-sign-compare -Wpointer-arith
     -Wdeclaration-after-statement -Wformat-security -Wswitch-enum -Wunused-parameter -Wstrict-aliasing -Wbad-function-cast -Wcast-align -Wstrict-prototypes -Wold-style-definition -Wmissing-prototypes -Wmissing-field-initializers -Wno-override-init  -g -O2 -export-dynamic ***-lpthread***  -o utilities/ovs-dpctl utilities/ovs-dpctl.o lib/libopenvswitch.a
 /home/jyyoo/src/dpdk/build/lib/librte_eal.a
 /home/jyyoo/src/dpdk/build/lib/libethdev.a
 /home/jyyoo/src/dpdk/build/lib/librte_cmdline.a
 /home/jyyoo/src/dpdk/build/lib/librte_hash.a
 /home/jyyoo/src/dpdk/build/lib/librte_lpm.a
 /home/jyyoo/src/dpdk/build/lib/librte_mbuf.a
 /home/jyyoo/src/dpdk/build/lib/librte_ring.a
 /home/jyyoo/src/dpdk/build/lib/librte_mempool.a
 /home/jyyoo/src/dpdk/build/lib/librte_malloc.a -lrt -lm 
     /usr/bin/ld: /home/jyyoo/src/dpdk/build/lib/librte_eal.a(eal.o): undefined reference
     to symbol 'pthread_create@@GLIBC_2.2.5'
     /lib/x86_64-linux-gnu/libpthread.so.0: error adding symbols: DSO missing from 
     command line
Run Code Online (Sandbox Code Playgroud)

如果我试着看到它的符号libpthread,它看起来很好.

$ readelf -s /lib/x86_64-linux-gnu/libpthread.so.0 | grep pthread_create
   199: 0000000000008220  2814 FUNC    GLOBAL DEFAULT   13 pthread_create@@GLIBC_2.2.5
   173: 0000000000008220  2814 FUNC    LOCAL  DEFAULT   13 __pthread_create_2_1
   462: …
Run Code Online (Sandbox Code Playgroud)

linker gcc compiler-errors undefined-reference

188
推荐指数
7
解决办法
40万
查看次数

未定义的引用`pow'和`floor'

我正在尝试在C中制作一个简单的斐波那契计算器,但在编译时gcc告诉我,我错过了战俘和地板功能.怎么了?

码:

#include <stdio.h>
#include <math.h>

int fibo(int n);

int main() {
        printf("Fib(4) = %d", fibo(4));
        return 0;
}

int fibo(int n) {
        double phi = 1.61803399;

        return (int)(floor((float)(pow(phi, n) / sqrt(5)) + .5f));
}
Run Code Online (Sandbox Code Playgroud)

输出:

gab@testvm:~/work/c/fibo$ gcc fib.c -o fibo
/tmp/ccNSjm4q.o: In function `fibo':
fib.c:(.text+0x4a): undefined reference to `pow'
fib.c:(.text+0x68): undefined reference to `floor'
collect2: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)

c gcc undefined-reference

117
推荐指数
5
解决办法
18万
查看次数

未定义的引用`sin`

我有以下代码(根据这个问题的基础知识):

#include<stdio.h>
#include<math.h>

double f1(double x)
{
    double res = sin(x);
    return 0;
}

/* The main function */
int main(void)
{
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

gcc test.c我编译它时,我得到以下错误,我无法解决原因:

/tmp/ccOF5bis.o: In function `f1':
test2.c:(.text+0x13): undefined reference to `sin'
collect2: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)

但是,我编写了各种sinmain函数内部调用的测试程序,并且这些程序完美地工作.我必须在这里做一些明显错误的事 - 但它是什么?

c math linker-errors undefined-reference

90
推荐指数
4
解决办法
13万
查看次数

未定义的模板函数引用

我有三个文件.main.cpp的内容是

#include<iostream>
#include<QString>

#include "util.h"

int main()
{
    using Util::convert2QString;

    using namespace std;
    int n =22;
    QString tmp = convert2QString<int>(n);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

util.h

namespace Util
{
    template<class T>
    QString convert2QString(T type , int digits=0);
}
Run Code Online (Sandbox Code Playgroud)

util.cpp

namespace Util
{
    template<class T>
        QString convert2QString(T type, int digits=0)
        {
            using std::string;

            string temp = (boost::format("%1%") % type).str();

            return QString::fromStdString(temp);
        }
}
Run Code Online (Sandbox Code Playgroud)

当我尝试使用以下命令编译这些文件时,我得到未定义的引用错误

vickey@tb:~/work/trash/template$ g++ main.cpp  util.cpp -lQtGui -lQtCore  -I. -I/usr/local/Trolltech/Qt-4.8.0/include/QtCore -I/usr/local/Trolltech/Qt-4.8.0/include/QtGui -I/usr/local/Trolltech/Qt-4.8.0/include
/tmp/cca9oU6Q.o: In function `main':
main.cpp:(.text+0x22): undefined reference to `QString …
Run Code Online (Sandbox Code Playgroud)

c++ qt boost undefined-reference

74
推荐指数
2
解决办法
6万
查看次数

未定义的静态成员引用

我正在使用交叉编译器.我的代码是:

class WindowsTimer{
public:
  WindowsTimer(){
    _frequency.QuadPart = 0ull;
  } 
private:
  static LARGE_INTEGER _frequency;
};
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

对WindowsTimer :: _ frequency'的未定义引用

我也尝试将其改为

LARGE_INTEGER _frequency.QuadPart = 0ull;
Run Code Online (Sandbox Code Playgroud)

要么

static LARGE_INTEGER _frequency.QuadPart = 0ull;
Run Code Online (Sandbox Code Playgroud)

但我仍然得到错误.

有谁知道为什么?

c++ cross-compiling undefined-reference

70
推荐指数
4
解决办法
6万
查看次数

对sqrt(或其他数学函数)的未定义引用

我有这个简单的代码:

max = (int) sqrt (number);
Run Code Online (Sandbox Code Playgroud)

在标题中我有:

#include <math.h>
Run Code Online (Sandbox Code Playgroud)

但应用程序仍然表示未定义的引用sqrt.你觉得这里有什么问题吗?看起来一切都应该没问题.

c linker-errors undefined-reference

57
推荐指数
2
解决办法
9万
查看次数

C错误:未定义的函数引用,但它已定义

只是一个简单的程序,但我一直得到这个编译错误.我正在使用MinGW作为编译器.

这是头文件,point.h:

//type for a Cartesian point
typedef struct {
  double x;
  double y;
} Point;

Point create(double x, double y);
Point midpoint(Point p, Point q);
Run Code Online (Sandbox Code Playgroud)

这是重点.:

//This is the implementation of the point type
#include "point.h"

int main() {
  return 0;
}
Point create(double x, double y) {
  Point p;
  p.x = x;
  p.y = y;
  return p;
}

Point midpoint(Point p, Point q) {
  Point mid;
  mid.x = (p.x + q.x) / 2;
  mid.y …
Run Code Online (Sandbox Code Playgroud)

c function linker-errors undefined-reference

55
推荐指数
3
解决办法
30万
查看次数

未定义的符号"vtable for ..."和"typeinfo for ..."?

几乎是最后一步,但仍然有一些奇怪的错误......

bash-3.2$ make
g++ -Wall -c -g Myworld.cc
g++ -Wall -g solvePlanningProblem.o Position.o AStarNode.o PRM.o PRMNode.o World.o SingleCircleWorld.o Myworld.o RECTANGLE.o CIRCLE.o -o solvePlanningProblem
Undefined symbols:
  "vtable for Obstacle", referenced from:
      Obstacle::Obstacle()in Myworld.o
  "typeinfo for Obstacle", referenced from:
      typeinfo for RECTANGLEin RECTANGLE.o
      typeinfo for CIRCLEin CIRCLE.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
make: *** [solvePlanningProblem] Error 1
Run Code Online (Sandbox Code Playgroud)

vtable和typeinfo的含义是什么?

c++ linker-errors pure-virtual vtable undefined-reference

46
推荐指数
5
解决办法
4万
查看次数

DSO在命令行中丢失

我正在尝试编译这样的C++程序:

$ g++ -o Sniffer_Train main.cpp Sniffer_train.cpp Sniffer_train.h -lmysqlclient -lpcap
Run Code Online (Sandbox Code Playgroud)

但是我收到以下错误:

/usr/bin/ld: /tmp/cct6xeXD.o: undefined reference to symbol
'pthread_join@@GLIBC_2.4' //lib/arm-linux-gnueabihf/libpthread.so.0:
error adding symbols: DSO missing from command line

collect2: error: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)

我不知道这个错误意味着什么.任何帮助将不胜感激.

c++ linux g++ undefined-reference

43
推荐指数
1
解决办法
10万
查看次数