今天,我遇到了一个问题,使用了我之前制作的工具:Singleton模板化类。
template <class C>
class Singleton {
/*! \class Singleton
* \brief Singleton Template
*/
public:
/*!
* \brief Public access interface
* \return Singleton instance
*/
static C *getInstance (){
// Create new instance if not initialised
if (_singleton == NULL){
_singleton = new C;
}
return (static_cast<C*> (_singleton));
}
/*!
* \brief Public access destructor
*/
static void kill(){
// Clean Singleton object and free memory
if (_singleton != NULL){
delete _singleton;
_singleton = NULL;
}
}
protected:
/*!
* \brief Empty constructor
*/
Singleton() = default;
/*!
* \brief Destructor
*/
virtual ~Singleton() = default;
private:
static C *_singleton; /*!< Unique instance */
};
template <class C>
C *Singleton<C>::_singleton = NULL;
当我尝试在(非常基本的)继承的类上调用TextureManager :: getInstance()时,就会出现问题:
class TextureManager : public Singleton<TextureManager> {
friend class Singleton<TextureManager>;
/*! \class TextureManager
* \brief Textures Container
*/
public:
protected:
/*!
* \brief Empty constructor
*/
TextureManager();
/*!
* \brief Destructor
*/
~TextureManager() override;
private:
};
在编译期间,链接器向我显示了我理解的错误,但我并不真正理解原因:
build/main.o: In function `Singleton<TextureManager>::getInstance()':
main.cpp (.text._ZN9SingletonI14TextureManagerE11getInstanceEv[_ZN9SingletonI14TextureManagerE11getInstanceEv]+0x24): undefined reference to `TextureManager::TextureManager()'
似乎找不到TextureManager.cpp中的构造函数的定义
TextureManager::TextureManager() = default;
TextureManager::~TextureManager() = default;
这段代码适用于我使用premake编译的旧项目,因此我的猜测是我当前的makefile很垃圾,但我找不到原因...
INCLUDE = -I/usr/X11R6/include/
LIBDIR = -L/usr/X11R6/lib
SRCDIR := sources
BUILDDIR := build
TARGET := bin/game
SRCEXT := cpp
SOURCES := $(shell find $(SRCDIR) -type f -name *.$(SRCEXT))
OBJECTS := $(patsubst $(SRCDIR)/%,$(BUILDDIR)/%,$(SOURCES:.$(SRCEXT)=.o))
FLAGS = -std=c++11 -Wall
CC = g++
CFLAGS = $(FLAGS) $(INCLUDE)
LIBS = -lglut -lGL -lGLU -lGLEW -lm
$(TARGET): $(OBJECTS)
@echo "Linking..."
@echo "$(CC) $(CFLAGS) $^ -o $@ $(LIBDIR) $< $(LIBS)"; $(CC) $(CFLAGS) -o $@ $(LIBDIR) $< $(LIBS)
$(BUILDDIR)/%.o: $(SRCDIR)/%.$(SRCEXT)
@mkdir -p $(@D)
@echo "$(CC) $(CFLAGS) -o $@ $(LIBDIR) $< $(LIBS)"; $(CC) $(CFLAGS) -c -o $@ $(LIBDIR) $< $(LIBS)
clean:
@echo "Cleaning..."
@echo "$(RM) -r $(BUILDDIR) $(TARGET)"; $(RM) -r $(BUILDDIR) $(TARGET)
.PHONY: clean
现在停了几个小时,
我所有的希望都寄托在你身上:p
编辑:完整的make输出
g++ -std=c++11 -Wall -I/usr/X11R6/include/ -o build/main.o -L/usr/X11R6/lib sources/main.cpp -lglut -lGL -lGLU -lGLEW -lm
g++ -std=c++11 -Wall -I/usr/X11R6/include/ -o build/Tools/ThreadManager.o -L/usr/X11R6/lib sources/Tools/ThreadManager.cpp -lglut -lGL -lGLU -lGLEW -lm
g++ -std=c++11 -Wall -I/usr/X11R6/include/ -o build/Graphics/GraphicalEngine.o -L/usr/X11R6/lib sources/Graphics/GraphicalEngine.cpp -lglut -lGL -lGLU -lGLEW -lm
g++ -std=c++11 -Wall -I/usr/X11R6/include/ -o build/Graphics/TextureManager.o -L/usr/X11R6/lib sources/Graphics/TextureManager.cpp -lglut -lGL -lGLU -lGLEW -lm
Linking...
g++ -std=c++11 -Wall -I/usr/X11R6/include/ build/main.o build/Tools/ThreadManager.o build/Graphics/GraphicalEngine.o build/Graphics/TextureManager.o -o bin/game -L/usr/X11R6/lib build/main.o -lglut -lGL -lGLU -lGLEW -lm
build/main.o: In function `Singleton<TextureManager>::getInstance()':
main.cpp:(.text._ZN9SingletonI14TextureManagerE11getInstanceEv[_ZN9SingletonI14TextureManagerE11getInstanceEv]+0x24): undefined reference to `TextureManager::TextureManager()'
collect2: error: ld returned 1 exit status
makefile:18: recipe for target 'bin/game' failed
make: *** [bin/game] Error 1
最佳答案
问题在于您仅在$(TARGET)
规则中使用第一个依赖项:
$(CC) $(CFLAGS) -o $@ $(LIBDIR) $< $(LIBS)
# ^^
我的评论实际上是错误的,因为您在
@echo
和实际命令之间使用了不同的命令:@echo "$(CC) $(CFLAGS) $^ -o $@ $(LIBDIR) $< $(LIBS)"
# ^^ ^^
我在此处添加此
echo
行没有任何意义,我将按照以下说明进行操作:$(TARGET): $(OBJECTS)
@echo "Linking..."
$(CC) $(CFLAGS) $^ -o $@ $(LIBDIR) $(LIBS)
# ^^
如果您这样做了,我想您会早日看到错误的。