繁体   English   中英

g ++编译时未定义参考错误

[英]g++ undefined reference error on compiling

我试图在Linux上编译我的程序。 它在Windows上运行良好(在Visual Studio 2015下编译)。 但是,当我尝试在Linux上执行相同操作时,出现此错误:

g++ -std=c++0x -std=c++0x    Prueba1.cpp   -o Prueba1
/tmp/cc9HTgYi.o: In function `Escuchador::onFrame(Leap::Controller const&)':
Prueba1.cpp:(.text+0x51): undefined reference to `Leap::Controller::frame(int) const'
Prueba1.cpp:(.text+0x6c): undefined reference to `Leap::Frame::fingers() const'
Prueba1.cpp:(.text+0x7d): undefined reference to `Leap::FingerList::count() const'
Prueba1.cpp:(.text+0x97): undefined reference to `Leap::Frame::hands() const'
Prueba1.cpp:(.text+0xa8): undefined reference to `Leap::HandList::count() const'
Prueba1.cpp:(.text+0xbc): undefined reference to `Leap::Frame::timestamp() const'
Prueba1.cpp:(.text+0xce): undefined reference to `Leap::Frame::id() const'
Prueba1.cpp:(.text+0x1ac): undefined reference to `Leap::Frame::hands() const'
Prueba1.cpp:(.text+0x1c7): undefined reference to `Leap::HandList::begin() const'
Prueba1.cpp:(.text+0x203): undefined reference to `Leap::Hand::isLeft() const'
Prueba1.cpp:(.text+0x245): undefined reference to `Leap::Hand::id() const'
Prueba1.cpp:(.text+0x2fc): undefined reference to `Leap::Hand::palmNormal() const'
Prueba1.cpp:(.text+0x317): undefined reference to `Leap::Hand::direction() const'
Prueba1.cpp:(.text+0x332): undefined reference to `Leap::Hand::fingers() const'
Prueba1.cpp:(.text+0x34d): undefined reference to `Leap::FingerList::begin() const'
Prueba1.cpp:(.text+0x37e): undefined reference to `Leap::Pointable::id() const'
Prueba1.cpp:(.text+0x38e): undefined reference to `Leap::Finger::type() const'
Prueba1.cpp:(.text+0x47b): undefined reference to `Leap::Finger::bone(Leap::Bone::Type) const'
Prueba1.cpp:(.text+0x510): undefined reference to `Leap::Frame::hands() const'
Prueba1.cpp:(.text+0x533): undefined reference to `Leap::HandList::operator[](int) const'
Prueba1.cpp:(.text+0x54e): undefined reference to `Leap::Hand::fingers() const'
Prueba1.cpp:(.text+0x582): undefined reference to `Leap::FingerList::begin() const'
Prueba1.cpp:(.text+0x595): undefined reference to `Leap::Bone::Bone()'
Prueba1.cpp:(.text+0x5ea): undefined reference to `Leap::Finger::bone(Leap::Bone::Type) const'
Prueba1.cpp:(.text+0x63e): undefined reference to `Leap::Finger::type() const'
Prueba1.cpp:(.text+0x67f): undefined reference to `Leap::operator<<(std::ostream&, Leap::Bone const&)'
Prueba1.cpp:(.text+0x6ee): undefined reference to `Leap::FingerList::end() const'
Prueba1.cpp:(.text+0x726): undefined reference to `Leap::Hand::fingers() const'
Prueba1.cpp:(.text+0x74d): undefined reference to `Leap::FingerList::begin() const'
Prueba1.cpp:(.text+0x768): undefined reference to `Leap::FingerList::end() const'
Prueba1.cpp:(.text+0x7ab): undefined reference to `Leap::Finger::bone(Leap::Bone::Type) const'
Prueba1.cpp:(.text+0x7ce): undefined reference to `Leap::Finger::bone(Leap::Bone::Type) const'
Prueba1.cpp:(.text+0x7f1): undefined reference to `Leap::Finger::bone(Leap::Bone::Type) const'
Prueba1.cpp:(.text+0x814): undefined reference to `Leap::Finger::bone(Leap::Bone::Type) const'
Prueba1.cpp:(.text+0x825): undefined reference to `Leap::Finger::type() const'
Prueba1.cpp:(.text+0x848): undefined reference to `Leap::Bone::direction() const'
Prueba1.cpp:(.text+0x863): undefined reference to `Leap::Bone::direction() const'
Prueba1.cpp:(.text+0x89e): undefined reference to `Leap::Bone::direction() const'
Prueba1.cpp:(.text+0x8b9): undefined reference to `Leap::Bone::direction() const'
Prueba1.cpp:(.text+0x8f4): undefined reference to `Leap::Bone::direction() const'
/tmp/cc9HTgYi.o:Prueba1.cpp:(.text+0x90c): more undefined references to `Leap::Bone::direction() const' follow
/tmp/cc9HTgYi.o: In function `Escuchador::onFrame(Leap::Controller const&)':
Prueba1.cpp:(.text+0xa15): undefined reference to `Leap::Finger::type() const'
Prueba1.cpp:(.text+0xa37): undefined reference to `Leap::Bone::direction() const'
Prueba1.cpp:(.text+0xa4f): undefined reference to `Leap::Bone::direction() const'
Prueba1.cpp:(.text+0xa87): undefined reference to `Leap::Bone::direction() const'
Prueba1.cpp:(.text+0xa9f): undefined reference to `Leap::Bone::direction() const'
Prueba1.cpp:(.text+0xad7): undefined reference to `Leap::Bone::direction() const'
/tmp/cc9HTgYi.o:Prueba1.cpp:(.text+0xaef): more undefined references to `Leap::Bone::direction() const' follow
/tmp/cc9HTgYi.o: In function `Escuchador::onFrame(Leap::Controller const&)':
Prueba1.cpp:(.text+0xbf8): undefined reference to `Leap::Finger::type() const'
Prueba1.cpp:(.text+0xc1b): undefined reference to `Leap::Bone::direction() const'
Prueba1.cpp:(.text+0xc33): undefined reference to `Leap::Bone::direction() const'
Prueba1.cpp:(.text+0xc6b): undefined reference to `Leap::Bone::direction() const'
Prueba1.cpp:(.text+0xc83): undefined reference to `Leap::Bone::direction() const'
Prueba1.cpp:(.text+0xcbb): undefined reference to `Leap::Bone::direction() const'
/tmp/cc9HTgYi.o:Prueba1.cpp:(.text+0xcd3): more undefined references to `Leap::Bone::direction() const' follow
/tmp/cc9HTgYi.o: In function `Escuchador::onFrame(Leap::Controller const&)':
Prueba1.cpp:(.text+0xeb4): undefined reference to `Leap::FingerList::end() const'
Prueba1.cpp:(.text+0xf27): undefined reference to `Leap::HandList::end() const'
/tmp/cc9HTgYi.o: In function `main':
Prueba1.cpp:(.text+0x11a3): undefined reference to `Leap::Controller::Controller()'
Prueba1.cpp:(.text+0x11b7): undefined reference to `Leap::Controller::addListener(Leap::Listener&)'
Prueba1.cpp:(.text+0x11fb): undefined reference to `Leap::Controller::removeListener(Leap::Listener&)'
Prueba1.cpp:(.text+0x120c): undefined reference to `Leap::Controller::~Controller()'
Prueba1.cpp:(.text+0x122a): undefined reference to `Leap::Controller::~Controller()'
/tmp/cc9HTgYi.o: In function `Leap::Pointable::~Pointable()':
Prueba1.cpp:(.text._ZN4Leap9PointableD2Ev[_ZN4Leap9PointableD5Ev]+0x16): undefined reference to `Leap::Interface::~Interface()'
/tmp/cc9HTgYi.o: In function `Leap::Bone::operator=(Leap::Bone&&)':
Prueba1.cpp:(.text._ZN4Leap4BoneaSEOS0_[_ZN4Leap4BoneaSEOS0_]+0x14): undefined reference to `Leap::Interface::operator=(Leap::Interface const&)'
/tmp/cc9HTgYi.o: In function `Leap::ConstListIterator<Leap::HandList, Leap::Hand>::operator*() const':
Prueba1.cpp:(.text._ZNK4Leap17ConstListIteratorINS_8HandListENS_4HandEEdeEv[_ZNK4Leap17ConstListIteratorINS_8HandListENS_4HandEEdeEv]+0x20): undefined reference to `Leap::HandList::operator[](int) const'
/tmp/cc9HTgYi.o: In function `Leap::ConstListIterator<Leap::FingerList, Leap::Finger>::operator*() const':
Prueba1.cpp:(.text._ZNK4Leap17ConstListIteratorINS_10FingerListENS_6FingerEEdeEv[_ZNK4Leap17ConstListIteratorINS_10FingerListENS_6FingerEEdeEv]+0x20): undefined reference to `Leap::FingerList::operator[](int) const'
/tmp/cc9HTgYi.o: In function `Leap::Frame::~Frame()':
Prueba1.cpp:(.text._ZN4Leap5FrameD2Ev[_ZN4Leap5FrameD5Ev]+0x16): undefined reference to `Leap::Interface::~Interface()'
/tmp/cc9HTgYi.o: In function `Leap::HandList::~HandList()':
Prueba1.cpp:(.text._ZN4Leap8HandListD2Ev[_ZN4Leap8HandListD5Ev]+0x16): undefined reference to `Leap::Interface::~Interface()'
/tmp/cc9HTgYi.o: In function `Leap::FingerList::~FingerList()':
Prueba1.cpp:(.text._ZN4Leap10FingerListD2Ev[_ZN4Leap10FingerListD5Ev]+0x16): undefined reference to `Leap::Interface::~Interface()'
/tmp/cc9HTgYi.o: In function `Leap::Hand::~Hand()':
Prueba1.cpp:(.text._ZN4Leap4HandD2Ev[_ZN4Leap4HandD5Ev]+0x16): undefined reference to `Leap::Interface::~Interface()'
/tmp/cc9HTgYi.o: In function `Leap::Bone::~Bone()':
Prueba1.cpp:(.text._ZN4Leap4BoneD2Ev[_ZN4Leap4BoneD5Ev]+0x16): undefined reference to `Leap::Interface::~Interface()'
/tmp/cc9HTgYi.o:(.rodata._ZTIN4Leap5FrameE[_ZTIN4Leap5FrameE]+0x8): undefined reference to `typeinfo for Leap::Interface'
/tmp/cc9HTgYi.o:(.rodata._ZTIN4Leap8HandListE[_ZTIN4Leap8HandListE]+0x8): undefined reference to `typeinfo for Leap::Interface'
/tmp/cc9HTgYi.o:(.rodata._ZTIN4Leap10FingerListE[_ZTIN4Leap10FingerListE]+0x8): undefined reference to `typeinfo for Leap::Interface'
/tmp/cc9HTgYi.o:(.rodata._ZTIN4Leap4HandE[_ZTIN4Leap4HandE]+0x8): undefined reference to `typeinfo for Leap::Interface'
/tmp/cc9HTgYi.o:(.rodata._ZTIN4Leap4BoneE[_ZTIN4Leap4BoneE]+0x8): undefined reference to `typeinfo for Leap::Interface'
/tmp/cc9HTgYi.o:(.rodata._ZTIN4Leap9PointableE[_ZTIN4Leap9PointableE]+0x8): more undefined references to `typeinfo for Leap::Interface' follow
collect2: error: ld returned 1 exit status
make: *** [Prueba1] Error 1

我的#include是:

#include <iostream>
#include <cstring>
#include "Leap.h"

看来问题出在Leap.h标头中。 在SDK中,有一个Sample.cpp文件,我可以对其进行编译并很好地运行。 我使用相同的makefile和相同的文件系统; 我正在使用与Sample.cpp相同的#include,因此我将.cpp文件移动到Sample.cpp所在的文件夹中并尝试编译,但出现错误。 我不知道如何解决,有什么想法吗? 这是makefile:

OS := $(shell uname)
ARCH := $(shell uname -m)
CXXFLAGS := -std=c++0x -std=c++0x

ifeq ($(OS), Linux)
  ifeq ($(ARCH), x86_64)
LEAP_LIBRARY := ../lib/x64/libLeap.so -Wl,-rpath,../lib/x64
  else
LEAP_LIBRARY := ../lib/x86/libLeap.so -Wl,-rpath,../lib/x86
  endif
else
  # OS X
LEAP_LIBRARY := ../lib/libLeap.dylib
endif

Sample: Sample.cpp
$(CXX) -Wall  -g -I../include Sample.cpp -o Sample $(LEAP_LIBRARY)

ifeq ($(OS), Darwin)
install_name_tool -change @loader_path/libLeap.dylib ../lib/libLeap.dylib Sample
endif

clean:
rm -rf Sample Sample.dSYM

我只添加了CXXFLAGS来解决C + 98编译器错误。 我也可以用这种方式编译Sample.cpp,但是当我尝试对文件执行相同操作时,我得到了错误。 谢谢您的帮助!

看来问题出在Leap.h标头中。

不,标题没有问题。 您只是没有链接到提供这些功能的定义的库。

查看makefile如何构建示例程序:它链接到libleap.so

您需要这样做:

g++ -std=c++0x -std=c++0x    Prueba1.cpp   -o Prueba1 ../lib/x64/libleap.so -Wl,-rpath,../lib/x64

(如果您使用的是32位系统,请用x86替换x64)。

首先,必读: 什么是未定义的引用/未解决的外部符号错误,我该如何解决? 请至少阅读与外部库组件有关的答案,然后返回此问题。


好的,因此,在您情况下,您正在运行链接命令而未指定外部库。 怎么会? 为什么您可以编译该软件包随附的示例?

看一下makefile。 它说

Sample: Sample.cpp
    $(CXX) -Wall  -g -I../include Sample.cpp -o Sample $(LEAP_LIBRARY)

规则告诉make三件事情:构建什么 (冒号前的第一行),该怎么 (冒号后)建造它,以及如何构建它(第二行)。 因此,当您运行类似

make Sample

make有一个很好的主意该怎么做。

现在,如果你说

make Prueba1

情况有所不同。 makefile中没有涉及Prueba1特殊规则,因此make获取其(大量)内置规则集,并寻找一个允许其为您构建Prueba1 确实存在这样的内置规则,但是它对libLeap.so并不了解。 这就是为什么您在构建日志中看不到它的原因。

当然,您可以在makefile中添加以下内容:

Prueba1: Prueba1.cpp
    $(CXX) -Wall  -g -I../include Prueba1.cpp -o Prueba1 $(LEAP_LIBRARY)

但是我敢猜测,到Prueba99时,它会变老一些。 我们可以做得更好吗?

是的,我们可以,但是让我们先回答另一个问题。 您的编译命令如何知道-std=c++0x

这是因为您将此标志添加到了内置宏 CXXFLAGS ,并且内置规则使用了此宏。 顺便说一句,您不需要指定-std=c++0x两次。

有没有一种方法可以像对待编译器标志一样对待这些库? 绝对! 有两个内置宏LDLIBSLDFLAGS 如果以这种方式定义它们:

LDLIBS := ../lib/x64/libLeap.so
LDFLAGS := -Wl,-rpath,../lib/x64

或(或者)

LDLIBS := -lLeap
LDFLAGS := -L ../lib/x64 -Wl,-rpath,../lib/x64

您的makefile应该能够针对libLeap构建任何C ++程序(如果它是作为单个C ++源文件编写的)。

-I../include添加到CPPFLAGS可能是明智的,因为Sample规则会使用它。 没有它,您的编译似乎可以工作,但是如果您更改了工作目录,则编译可能会失败。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM