2016-08-03 12 views
0

Ich versuche, mein Programm unter Linux zu kompilieren. Es funktioniert hervorragend unter Windows (kompiliert unter Visual Studio 2015). Aber wenn ich versuche, das gleiche auf Linux zu tun, bekomme ich diesen Fehler:g ++ undefinierter Referenzfehler beim Kompilieren

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 

Meine #include sind:

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

So scheint es, das Problem mit dem Kopf Leap.h ist. Im SDK gibt es eine Sample.cpp, die ich kompilieren kann und läuft super. Ich benutze das gleiche Makefile und das selbe Dateisystem; Ich benutze das gleiche #include als die Sample.cpp, also verschiebe ich meine .cpp-Datei in den gleichen Ordner wie Sample.cpp und versuche zu kompilieren, und ich bekomme den Fehler. Ich weiß nicht, wie ich es lösen soll, irgendwelche Ideen? Hier ist das 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 

Ich habe nur die CXXFLAGS hinzugefügt, um einen C + 98 Compilerfehler zu lösen. Ich kann die Sample.cpp auch auf diese Weise kompilieren, aber wenn ich versuche, das gleiche für meine Datei zu tun, bekomme ich die Fehler. Danke für die Hilfe!

+1

Ich habe das Gefühl, dass die Marke das Betriebssystem nicht richtig erkennt. Versuchen Sie: UNAME_S: = $ (shell uname -s) ifeq ($ (UNAME_S), Linux) --- Sie Zeug hier --- endif .... Siehe mehr: http://StackOverflow.com/ Fragen/714100/os-detecting-makefile – fritzone

+1

@fritzone Es sieht so aus, als würde OP das Makefile überhaupt nicht benutzen. –

+3

libLeap.so fehlt in Ihrer Kompilierungszeile. Dies bedeutet, dass Sie entweder das Makefile nicht verwenden oder es verwenden, ohne es für Ihr Programm zu aktualisieren. Wie geschrieben, funktioniert es nur mit einem Programm namens Sample. Sie müssen das Makefile so modifizieren, dass es mit Ihrem Programmnamen oder (einer erweiterten Mission) arbeitet, um es so zu verbessern, dass es mit jedem Programmnamen funktioniert und Standardmakros wie LDLIBS verwendet. –

Antwort

1

So it seems the problem is with the Leap.h header.

Nein, es gibt kein Problem mit der Kopfzeile. Sie verknüpfen einfach nicht mit der Bibliothek, die die Definitionen dieser Funktionen bereitstellt.

Blick darauf, wie die Make-Datei des Beispielprogramm baut: es libleap.so,

Sie müssen das gleiche tun Links:

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

(ersetzt x64 mit x86, wenn Sie auf einem 32- sind Bit-System).

1

Erste, erforderliche Lesung: What is an undefined reference/unresolved external symbol error and how do I fix it? Bitte lesen Sie mindestens Antworten zu externen Bibliothekskomponenten, dann kehren Sie zu dieser Frage zurück.


OK, also in Ihrem Fall führen Sie einen Link-Befehl ohne Angabe der externen Bibliothek. Woher? Warum können Sie ein Beispiel kompilieren, das mit dem Paket geliefert wird?

Werfen Sie einen Blick auf das Makefile. Es sagt

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

Diese Regelmake drei Dinge erzählt: was zu bauen (die erste Zeile vor dem Doppelpunkt), was ihm von (nach dem Doppelpunkt) zu bauen, und wie zu baue es (die zweite Zeile). Also, wenn Sie einen Befehl ausführen, wie

make Sample 

make hat eine ziemlich gute Idee, was zu tun ist.

Nun, wenn Sie sagen

make Prueba1 

die Dinge ein wenig anders sind. Es gibt keine speziellen Regeln im Makefile, die über Prueba1 sprechen, also holt make seine (riesige) Reihe von eingebauten Regeln und sucht nach einer, die es erlauben würde, Prueba1 für Sie zu bauen. Es gibt zwar eine solche eingebaute Regel, aber sie weiß nicht über libLeap.so.Aus diesem Grund sehen Sie es nicht in Ihrem Build-Protokoll.

Natürlich kann man so etwas zu Ihrer Make-Datei hinzufügen:

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

aber ich wage zu vermuten, dass es etwas alt werden, wenn Sie zu Prueba99 bekommen. Können wir es besser machen?

Ja können wir, aber lassen Sie uns zuerst eine andere Frage beantworten. Wie kommt es, dass Ihr Kompilierungsbefehl über -std=c++0x weiß?

Dies liegt daran, dass Sie dieses Flag zu einem integrierten Makro CXXFLAGS hinzugefügt haben, und die integrierte Regel verwendet dieses Makro. Übrigens müssen Sie -std=c++0x nicht zweimal angeben.

Gibt es eine Möglichkeit, die Bibliotheken ähnlich wie Compiler-Flags zu behandeln? Absolut! Es gibt zwei eingebaute Makros LDLIBS und LDFLAGS. Wenn Sie sie auf diese Weise zu definieren:

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

oder (alternativ)

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

Ihr Make-Datei sollten alle C++ Programm gegen libLeap aufbauen können (wenn es als eine einzige C++ Quelldatei geschrieben wird).

Es ist wahrscheinlich klug, -I../include zu CPPFLAGS hinzuzufügen, da die Beispielregel es verwendet. Ihre Kompilation scheint ohne sie zu funktionieren, aber sie kann fehlschlagen, wenn Sie z. ändere dein Arbeitsverzeichnis.

Verwandte Themen