2016-08-03 136 views
0

我試圖在linux上編譯我的程序。它在Windows上很好(在Visual Studio 2015下編譯)。但是,當我嘗試做在Linux上一樣,我得到這個錯誤:g ++未定義的編譯錯誤

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,我可以編譯並運行很好。我使用相同的生成文件和相同的文件系統;我使用的是與Sample.cpp相同的#include,因此我將.cpp文件移動到Sample.cpp所在的文件夾中,並嘗試編譯,並且出現錯誤。我不知道如何解決它,有什麼想法?下面是生成文件:

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,但是當我嘗試在我的文件上執行相同的操作時,出現錯誤。謝謝您的幫助!

+1

我有這樣的感覺,使得不能正確識別操作系統。嘗試:UNAME_S:= $(shell uname -s) ifeq($(UNAME_S),Linux) ---你在這裏填寫--- endif ....查看更多:http://stackoverflow.com/ questions/714100/os-detecting-makefile – fritzone

+1

@fritzone它看起來像OP沒有使用makefile。 –

+3

libLeap.so在編譯行中不存在。這意味着您要麼不使用makefile,要麼不使用makefile而不更新它。正如所寫,它只能用於名爲Sample的程序。您必須修改makefile以使用您的程序名稱或(高級任務)來改進它,以便它可以與任何程序名稱一起使用,並使用標準宏如LDLIBS。 –

回答

1

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

不,沒有問題的標題。你只是沒有鏈接到提供這些函數定義的庫。

看看makefile文件如何構建示例程序:它鏈接到libleap.so

你需要做的是相同的:

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

(更換64與86,如果你使用的是32位位系統)。

1

首先,需要閱讀:What is an undefined reference/unresolved external symbol error and how do I fix it?請至少閱讀與外部庫組件相關的答案,然後返回到此問題。


好吧,在你的情況下,你正在運行一個鏈接命令,而不指定外部庫。怎麼來的?爲什麼你能夠編譯包裝附帶的樣品?

看看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++源文件)。

自從Sample規則使用它以後,將-I../include添加到CPPFLAGS可能是明智的做法。你的編輯似乎沒有它,但它可能會失敗,如果你改變你的工作目錄。