什么是未定义的参考/未解决的外部符号错误?什么是常见原因以及如何修复/预防它们?
随意编辑/添加您自己的.
c++ c++-faq linker-errors unresolved-external undefined-reference
我是C的新手,我有这个代码:
#include <stdio.h>
#include <math.h>
int main(void)
{
double x = 0.5;
double result = sqrt(x);
printf("The square root of %lf is %lf\n", x, result);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但是当我编译它时:
gcc test.c -o test
Run Code Online (Sandbox Code Playgroud)
我收到这样的错误:
/tmp/cc58XvyX.o: In function `main':
test.c:(.text+0x2f): undefined reference to `sqrt'
collect2: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)
为什么会这样?是sqrt()不是在math.h头文件中?我cosh和其他三角函数有同样的错误.为什么?
当我尝试运行我的Xcode项目时,它失败并显示一条错误,指出我有重复的符号.我在网上查找了这些重复但没有运气的地方:

任何想法如何解决这一问题?
我有以下代码(根据这个问题的基础知识):
#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)
但是,我编写了各种sin从main函数内部调用的测试程序,并且这些程序完美地工作.我必须在这里做一些明显错误的事 - 但它是什么?
我要构建静态库.我想在我的iPhone和ipad应用程序中使用.当我尝试运行模拟器时,我得到链接错误.我是iOS开发的新手.善意的帮助;
ld:警告:忽略文件/Users/valuelabs/Desktop/DruvaProject/libraries/libnetUtils.a,文件是为存档而构建的,而不是被链接的体系结构(i386)体系结构i386的未定义符号:
"_ OBJC_CLASS _ $ _ netUtils",引用自:ViewController.o中的objc-class-ref ld:找不到架构i386 clang的符号:错误:链接器命令失败,退出代码为1(使用-v查看调用)
我尝试在架构中添加i386.但没有运气
我从apple的文档中复制了一些代码- 我得到了这两个错误:
Undefined symbols for architecture i386:
"_kUTTypeImage", referenced from:
-[ImagePicker imagePickerController:didFinishPickingMediaWithInfo:] in ImagePicker.o
ld: symbol(s) not found for architecture i386
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
编辑:代码:
- (IBAction) showSavedMediaBrowser {
[self startMediaBrowserFromViewController: self
usingDelegate: (id)self];
}
- (BOOL) startMediaBrowserFromViewController: (UIViewController*) controller
usingDelegate: (id <UIImagePickerControllerDelegate,
UINavigationControllerDelegate>) delegate {
if (([UIImagePickerController isSourceTypeAvailable:
UIImagePickerControllerSourceTypeSavedPhotosAlbum] == NO)
|| (delegate == nil)
|| (controller == nil))
return NO;
UIImagePickerController *mediaUI = [[UIImagePickerController …Run Code Online (Sandbox Code Playgroud) 我只是想摆脱像这样弹出的警告:
ld: warning: directory not found for option '-F/Users/m/Desktop/FacebookSDK'
ld: warning: directory not found for option '-F/Users/m/Desktop/FacebookSDK/FBAudienceNetwork'
ld: warning: directory not found for option '-F/Users/gavin/Downloads/shaffiulla11-bat-b86d81d8adc6'
ld: warning: directory not found for option '-F/Users/gavin/Desktop/shaffiulla11-bat-b86d81d8adc6'
ld: warning: ignoring file /Users/Gavin/Desktop/FacebookSDK/Bolts.framework/Bolts, missing required architecture x86_64 in file /Users/Gavin/Desktop/FacebookSDK/Bolts.framework/Bolts (2 slices)
Run Code Online (Sandbox Code Playgroud)
显然我现在正在使用不同的目录,因为我的驱动器由于逻辑板问题而进行了多次重新格式化,并且我想知道最方便的方法来摆脱这些错误.感谢您的时间和帮助.
我在Windows 7 Ultimate 32位上使用Qt Creator 2.0.1和Qt 4.7.0(32位).
请考虑以下代码,这是产生错误的最小代码:
class T : public QObject, public QGraphicsItem
{
Q_OBJECT
public:
T() {}
QRectF boundingRect() const {return QRectF();}
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
QWidget *widget) {}
};
int main()
{
T t;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
上面的代码片段导致以下链接器错误:
在函数"T"中:
未定义引用`vtable for T'
未定义引用`vtable for T'
在函数`~T'中:
未定义引用`vtable for T'
未定义引用`vtable for T'
如果我注释掉包含的行Q_OBJECT,它编译得很好.我需要信号和插槽,QGraphicsItem所以我需要Q_OBJECT.
代码有什么问题?谢谢.
这是我的标题:
#ifndef BARELYSOCKET_H
#define BARELYSOCKET_H
#include <QObject>
//! The First Draw of the BarelySocket!
class BarelySocket: public QObject
{
Q_OBJECT
public:
BarelySocket();
public slots:
void sendMessage(Message aMessage);
signals:
void reciveMessage(Message aMessage);
private:
// QVector<Message> reciveMessages;
};
#endif // BARELYSOCKET_H
Run Code Online (Sandbox Code Playgroud)
这是我的班级:
#include <QTGui>
#include <QObject>
#include "type.h"
#include "client.h"
#include "server.h"
#include "barelysocket.h"
BarelySocket::BarelySocket()
{
//this->reciveMessages.clear();
qDebug("BarelySocket::BarelySocket()");
}
void BarelySocket::sendMessage(Message aMessage)
{
}
void BarelySocket::reciveMessage(Message aMessage)
{
}
Run Code Online (Sandbox Code Playgroud)
我收到链接器错误:
undefined reference to 'vtable for BarelySocket'
Run Code Online (Sandbox Code Playgroud)
Message是一个复杂的struct …我正在尝试使用libtommath库.我在Ubuntu linux上使用NetBeans IDE作为我的项目.我已经下载并构建了库,我已经完成了'make install'以将生成的.a文件放入/ usr/lib /并将.h文件放入/ usr/include
它似乎是正确地找到文件(因为我不再得到这些错误,我在安装到/ usr目录之前做了这些错误).
但是,当我创建一个简单的main来调用mp_init(在库中)时,当我尝试创建项目时出现以下错误:
mkdir -p build/Debug/GNU-Linux-x86
rm -f build/Debug/GNU-Linux-x86/main.o.d
gcc -c -g -MMD -MP -MF build/Debug/GNU-Linux-x86/main.o.d -o build/Debug/GNU-Linux-x86/main.o main.c
mkdir -p dist/Debug/GNU-Linux-x86
gcc -o dist/Debug/GNU-Linux-x86/cproj1 build/Debug/GNU-Linux-x86/main.o
build/Debug/GNU-Linux-x86/main.o: In function 'main':
/home/[[myusername]]/NetBeansProjects/CProj1/main.c:18: undefined reference to `mp_init'
collect2: ld returned 1 exit status
make[2]: *** [dist/Debug/GNU-Linux-x86/cproj1] Error 1
Run Code Online (Sandbox Code Playgroud)
因此,看起来链接器无法在库中找到该函数,但它就在那里,所以我只是不知道是什么原因造成的.
如果我直接输入gcc命令并跳过makefile,我会得到同样的错误,我也确保静态库也用gcc编译.
编辑添加:
如果我直接编译并使用-l或-L添加库,我会得到同样的错误:
$ gcc -l /usr/lib/libtommath.a main.c
/usr/bin/ld: cannot find -l/usr/lib/libtommath.a
collect2: ld returned 1 exit status
$ gcc -llibtommath.a main.c
/usr/bin/ld: cannot find -llibtommath.a …Run Code Online (Sandbox Code Playgroud)