"未定义参考"在MakeFile中

时间:2015-01-25 19:09:31

标签: c++ linux makefile

我是MakeFile的菜鸟。我已经在stackoverflow上阅读了所有类似的帖子。但仍然不知道该怎么做(我确定这是一个MakeFile问题,因为我可以用XCode运行它)。这是我的MakeFile和错误消息:

####### Detect system architecture

SYSARCH       = i386
ifeq ($(shell uname -m),x86_64)
SYSARCH       = x86_64
endif

####### Compiler, tools and options

CXX           = g++
LINK          = g++
MAKE          = make
DELETEFILE    = rm -f
DELETEDIR     = rm -Rf
DEFINES       = -DQT_WEBKIT -DGL_GLEXT_PROTOTYPES
COMMONFLAGS   = -Wall -Wextra -pipe -msse2 -Wno-reorder 
#-Werror 
####### Detect debug or release

DEBUG         ?= 0
CXXFLAGS          ?=
ifeq ($(DEBUG), 1)
    CXXFLAGS      += $(COMMONFLAGS) -DDEBUG -DOVR_BUILD_DEBUG -g $(DEFINES)
    RELEASETYPE   ?= Debug
    LFLAGS         = 
else
    CXXFLAGS      += $(COMMONFLAGS) -O2 $(DEFINES)
    RELEASETYPE   ?= Release
    LFLAGS         = -O1  # Why O1?
endif

####### Paths

OCULUSWORLDPATH = .
LIBOVRPATH      = ../../LibOVR
COMMONSRCPATH   = ../CommonSrc
3RDPARTYPATH    = ../../3rdParty
INCPATH         = -I. -I.. -I$(COMMONSRCPATH) -I$(LIBOVRPATH)/Include -I$(LIBOVRPATH)/Src
OBJPATH         = ./Obj/Linux/$(RELEASETYPE)/$(SYSARCH)
CXX_BUILD       = $(CXX) -c $(CXXFLAGS) $(INCPATH) -o $(OBJPATH)/

####### Files

LIBS          = -L$(LIBOVRPATH)/Lib/Linux/$(RELEASETYPE)/$(SYSARCH) \
        -lovr \
        -ludev \
        -lpthread \
        -lGL \
        -lX11 \
        -lXrandr \
        -lrt

TARGET        = ./Release/OculusWorldDemo_$(SYSARCH)_$(RELEASETYPE)

####### Rules

all:    $(TARGET)

OCULUSWORLD_SOURCE =    $(OCULUSWORLDPATH)/OculusWorldDemo.cpp \
                        $(OCULUSWORLDPATH)/OculusWorldDemo_Scene.cpp \
                        $(OCULUSWORLDPATH)/Player.cpp \
                        $(OCULUSWORLDPATH)/../CommonSrc/Util/RenderProfiler.cpp \
                        $(OCULUSWORLDPATH)/../CommonSrc/Util/OptionMenu.cpp \
                        $(OCULUSWORLDPATH)/../CommonSrc/Platform/Linux_Gamepad.cpp \
                        $(OCULUSWORLDPATH)/../CommonSrc/Platform/Linux_Platform.cpp \
                        $(OCULUSWORLDPATH)/../CommonSrc/Platform/Platform.cpp \
                        $(OCULUSWORLDPATH)/../CommonSrc/Render/Render_Device.cpp \
                        $(OCULUSWORLDPATH)/../CommonSrc/Render/Render_GL_Device.cpp \
                        $(OCULUSWORLDPATH)/../CommonSrc/Render/Render_LoadTextureDDS.cpp \
                        $(OCULUSWORLDPATH)/../CommonSrc/Render/Render_LoadTextureTGA.cpp \
                        $(OCULUSWORLDPATH)/../CommonSrc/Render/Render_XmlSceneLoader.cpp \
                        $(OCULUSWORLDPATH)/../../3rdParty/TinyXml/tinyxml2.cpp

OCULUSWORLD_OBJECTS = $(patsubst $(OCULUSWORLDPATH)%.cpp,$(OBJPATH)%.o,$(OCULUSWORLD_SOURCE))

OBJECTS = $(OTHER_OBJECTS) $(OCULUSWORLD_OBJECTS)

$(OBJPATH)/%.o: %.cpp
    -mkdir -p $(dir $@)
    $(CXX) -c $(CXXFLAGS) $(INCPATH) -o $@ $<

$(LIBOVRPATH)/Lib/Linux/$(RELEASETYPE)/$(SYSARCH)/libovr.a:
    $(MAKE) -C $(LIBOVRPATH) DEBUG=$(DEBUG)

lib: $(LIBOVRPATH)/Lib/Linux/$(RELEASETYPE)/$(SYSARCH)/libovr.a

run: $(TARGET)
    $(TARGET)

$(TARGET):  $(OBJECTS) lib
    -mkdir -p $(dir $@)
    $(LINK) $(LFLAGS) -o $(TARGET) $(OBJECTS) $(LIBS)

clean:
    -$(DELETEFILE) $(OBJECTS)
    -$(DELETEFILE) $(TARGET)

cleanall:
    -$(DELETEFILE) $(OBJECTS)
    -$(DELETEDIR) ./Release/*

xxx:~/Desktop/ing$ make
true
make -C ./LibOVR
make[1]: Entering directory `/home/bosch3d/Desktop/ing/LibOVR'
make[1]: Nothing to be done for `all'.
make[1]: Leaving directory `/home/bosch3d/Desktop/ing/LibOVR'
make -C ./Samples/OculusWorldDemo
make[1]: Entering directory `/home/bosch3d/Desktop/ing/Samples/OculusWorldDemo'
mkdir -p Release/
g++ -O1   -o ./Release/OculusWorldDemo_x86_64_Release  ./Obj/Linux/Release/x86_64/OculusWorldDemo.o ./Obj/Linux/Release/x86_64/OculusWorldDemo_Scene.o ./Obj/Linux/Release/x86_64/Player.o ./Obj/Linux/Release/x86_64/../CommonSrc/Util/RenderProfiler.o ./Obj/Linux/Release/x86_64/../CommonSrc/Util/OptionMenu.o ./Obj/Linux/Release/x86_64/../CommonSrc/Platform/Linux_Gamepad.o ./Obj/Linux/Release/x86_64/../CommonSrc/Platform/Linux_Platform.o ./Obj/Linux/Release/x86_64/../CommonSrc/Platform/Platform.o ./Obj/Linux/Release/x86_64/../CommonSrc/Render/Render_Device.o ./Obj/Linux/Release/x86_64/../CommonSrc/Render/Render_GL_Device.o ./Obj/Linux/Release/x86_64/../CommonSrc/Render/Render_LoadTextureDDS.o ./Obj/Linux/Release/x86_64/../CommonSrc/Render/Render_LoadTextureTGA.o ./Obj/Linux/Release/x86_64/../CommonSrc/Render/Render_XmlSceneLoader.o ./Obj/Linux/Release/x86_64/../../3rdParty/TinyXml/tinyxml2.o -L../../LibOVR/Lib/Linux/Release/x86_64 -lovr -ludev -lpthread -lGL -lX11 -lXrandr -lrt
./Obj/Linux/Release/x86_64/OculusWorldDemo.o: In function `OculusWorldDemoApp::~OculusWorldDemoApp()':
OculusWorldDemo.cpp:(.text+0x756): undefined reference to `cv::fastFree(void*)'
OculusWorldDemo.cpp:(.text+0x7d6): undefined reference to `cv::fastFree(void*)'
OculusWorldDemo.cpp:(.text+0x836): undefined reference to `cv::Mat::deallocate()'
OculusWorldDemo.cpp:(.text+0x843): undefined reference to `cv::Mat::deallocate()'
./Obj/Linux/Release/x86_64/OculusWorldDemo.o: In function `OculusWorldDemoApp::RenderEyeView(ovrEyeType)':
OculusWorldDemo.cpp:(.text+0x9064): undefined reference to `ReadDataRunML::run(bool)'
./Obj/Linux/Release/x86_64/OculusWorldDemo.o: In function `cv::Mat::~Mat()':
OculusWorldDemo.cpp:(.text._ZN2cv3MatD2Ev[_ZN2cv3MatD5Ev]+0x89): undefined reference to `cv::Mat::deallocate()'
OculusWorldDemo.cpp:(.text._ZN2cv3MatD2Ev[_ZN2cv3MatD5Ev]+0x79): undefined reference to `cv::fastFree(void*)'
./Obj/Linux/Release/x86_64/OculusWorldDemo.o: In function `ReadDataRunML::~ReadDataRunML()':
OculusWorldDemo.cpp:(.text._ZN13ReadDataRunMLD2Ev[_ZN13ReadDataRunMLD5Ev]+0xa6): undefined reference to `cv::fastFree(void*)'
OculusWorldDemo.cpp:(.text._ZN13ReadDataRunMLD2Ev[_ZN13ReadDataRunMLD5Ev]+0x120): undefined reference to `cv::fastFree(void*)'
OculusWorldDemo.cpp:(.text._ZN13ReadDataRunMLD2Ev[_ZN13ReadDataRunMLD5Ev]+0x144): undefined reference to `cv::Mat::deallocate()'
OculusWorldDemo.cpp:(.text._ZN13ReadDataRunMLD2Ev[_ZN13ReadDataRunMLD5Ev]+0x154): undefined reference to `cv::Mat::deallocate()'
./Obj/Linux/Release/x86_64/OculusWorldDemo.o: In function `ReadDataRunML::ReadDataRunML(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, int, double, int, int)':
OculusWorldDemo.cpp:(.text._ZN13ReadDataRunMLC2ESsidii[_ZN13ReadDataRunMLC5ESsidii]+0x1a3): undefined reference to `cv::String::allocate(unsigned long)'
OculusWorldDemo.cpp:(.text._ZN13ReadDataRunMLC2ESsidii[_ZN13ReadDataRunMLC5ESsidii]+0x1eb): undefined reference to `cv::ml::TrainData::loadFromCSV(cv::String const&, int, int, int, cv::String const&, char, char)'
OculusWorldDemo.cpp:(.text._ZN13ReadDataRunMLC2ESsidii[_ZN13ReadDataRunMLC5ESsidii]+0x1f8): undefined reference to `cv::String::deallocate()'
OculusWorldDemo.cpp:(.text._ZN13ReadDataRunMLC2ESsidii[_ZN13ReadDataRunMLC5ESsidii]+0x20b): undefined reference to `cv::String::deallocate()'
OculusWorldDemo.cpp:(.text._ZN13ReadDataRunMLC2ESsidii[_ZN13ReadDataRunMLC5ESsidii]+0x2ec): undefined reference to `cv::fastFree(void*)'
OculusWorldDemo.cpp:(.text._ZN13ReadDataRunMLC2ESsidii[_ZN13ReadDataRunMLC5ESsidii]+0x3db): undefined reference to `cv::Mat::copySize(cv::Mat const&)'
OculusWorldDemo.cpp:(.text._ZN13ReadDataRunMLC2ESsidii[_ZN13ReadDataRunMLC5ESsidii]+0x4c7): undefined reference to `cv::fastFree(void*)'
OculusWorldDemo.cpp:(.text._ZN13ReadDataRunMLC2ESsidii[_ZN13ReadDataRunMLC5ESsidii]+0x5bf): undefined reference to `cv::Mat::copySize(cv::Mat const&)'
OculusWorldDemo.cpp:(.text._ZN13ReadDataRunMLC2ESsidii[_ZN13ReadDataRunMLC5ESsidii]+0x6b8): undefined reference to `cv::fastFree(void*)'
OculusWorldDemo.cpp:(.text._ZN13ReadDataRunMLC2ESsidii[_ZN13ReadDataRunMLC5ESsidii]+0x6c7): undefined reference to `cv::Formatter::get(int)'
OculusWorldDemo.cpp:(.text._ZN13ReadDataRunMLC2ESsidii[_ZN13ReadDataRunMLC5ESsidii]+0x7c1): undefined reference to `cv::Mat::deallocate()'
OculusWorldDemo.cpp:(.text._ZN13ReadDataRunMLC2ESsidii[_ZN13ReadDataRunMLC5ESsidii]+0x861): undefined reference to `cv::Mat::deallocate()'
OculusWorldDemo.cpp:(.text._ZN13ReadDataRunMLC2ESsidii[_ZN13ReadDataRunMLC5ESsidii]+0x86e): undefined reference to `cv::Mat::deallocate()'
OculusWorldDemo.cpp:(.text._ZN13ReadDataRunMLC2ESsidii[_ZN13ReadDataRunMLC5ESsidii]+0x882): undefined reference to `cv::Mat::deallocate()'
OculusWorldDemo.cpp:(.text._ZN13ReadDataRunMLC2ESsidii[_ZN13ReadDataRunMLC5ESsidii]+0x88f): undefined reference to `cv::Mat::deallocate()'
OculusWorldDemo.cpp:(.text._ZN13ReadDataRunMLC2ESsidii[_ZN13ReadDataRunMLC5ESsidii]+0x929): undefined reference to `cv::String::deallocate()'
OculusWorldDemo.cpp:(.text._ZN13ReadDataRunMLC2ESsidii[_ZN13ReadDataRunMLC5ESsidii]+0x975): undefined reference to `cv::String::deallocate()'
collect2: ld returned 1 exit status
make[1]: *** [Release/OculusWorldDemo_x86_64_Release] Error 1
make[1]: Leaving directory `/home/bosch3d/Desktop/ing/Samples/OculusWorldDemo'
make: *** [Samples/OculusWorldDemo/Release/OculusWorldDemo_x86_64_Release] Error 2
xxx:~/Desktop/ing$ 

我已添加:

-lopencv_core \
-lopencv_highgui \
-lopencv_imgproc \
-lopencv_video \
-lopencv_objdetect \

现在的错误是:

./Obj/Linux/Release/x86_64/OculusWorldDemo.o: In function `OculusWorldDemoApp::RenderEyeView(ovrEyeType)':
OculusWorldDemo.cpp:(.text+0x9064): undefined reference to `ReadDataRunML::run(bool)'
./Obj/Linux/Release/x86_64/OculusWorldDemo.o: In function `ReadDataRunML::ReadDataRunML(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, int, double, int, int)':
OculusWorldDemo.cpp:(.text._ZN13ReadDataRunMLC2ESsidii[_ZN13ReadDataRunMLC5ESsidii]+0x1eb): undefined reference to `cv::ml::TrainData::loadFromCSV(cv::String const&, int, int, int, cv::String const&, char, char)'
collect2: ld returned 1 exit status
make[1]: *** [Release/OculusWorldDemo_x86_64_Release] Error 1
make[1]: Leaving directory `/home/bosch3d/Desktop/ing/Samples/OculusWorldDemo'
make: *** [Samples/OculusWorldDemo/Release/OculusWorldDemo_x86_64_Release] Error 2

ReadDataRunML是我自己添加的一个类。我应该如何将它添加到MakeFile?

1 个答案:

答案 0 :(得分:0)

您需要将opencv库添加到您链接的库列表中:

添加到MakeFile的LIBS部分:

-lopencv_core \

并且,根据您使用的opencv功能,您可能需要以下一项或多项:

-lopencv_highgui \
-lopencv_imgproc \
-lopencv_video \
-lopencv_objdetect \

未解决的外部因素仅仅是因为链接器不知道在哪里寻找它们。此外,请确保您的opencv库路径位于lib路径中,或者将其添加到链接器使用的libpath中。您可以使用-L选项添加到库路径,例如-LMyLocalPathToOpenCV(而不是在将其添加到链接器选项的地方进行硬编码,您可能希望定义它并使用宏来引用它,以使您的makefile更清晰,例如{{1} })。