我正在尝试使用 tiny_obj_loader将 .obj
加载到我的程序中。我有以下代码利用这个库:
void LoadModel(vector<Shape *> &scene, const char *path) {
attrib_t attrib;
vector<shape_t> shapes;
vector<material_t> materials;
std::string error;
bool ret = tinyobj::LoadObj(
&attrib,
&shapes,
&materials,
&error,
path
);
... // Do stuff with the returned data
然而,这给了我以下链接器错误:
Build/skeleton.o: In function `LoadModel(std::vector<Shape*,
std::allocator<Shape*> >&, char const*)':
skeleton.cpp:(.text+0x3025): undefined reference to `tinyobj::LoadObj(tinyobj::attrib_t*, std::vector<tinyobj::shape_t, std::allocator<tinyobj::shape_t> >*, std::vector<tinyobj::material_t, std::allocator<tinyobj::material_t> >*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >*, char const*, char const*, bool)'
Makefile:43: recipe for target 'Build' failed
make: *** [Build] Error 1
函数定义在哪里:
bool LoadObj(attrib_t *attrib, std::vector<shape_t> *shapes,
std::vector<material_t> *materials, std::string *err,
const char *filename, const char *mtl_basedir = NULL,
bool triangulate = true);
看起来参数类型对我来说是正确的。
我在 skeleton.cpp
中包含了 .h
文件
#include "tiny_obj_loader.h"
tiny_obj_loader.h
为文件名,与skeleton.cpp
在同一目录下。
编辑 正在使用的 Makefile 是:
FILE=skeleton
########
# Directories
S_DIR=Source
B_DIR=Build
########
# Output
EXEC=$(B_DIR)/$(FILE)
# default build settings
CC_OPTS=-std=c++11 -c -pipe -Wall -Wno-switch -O3 -xHOST -qopenmp
LN_OPTS=-qopenmp
CC=icpc
########
# SDL options
SDL_CFLAGS := $(shell sdl2-config --cflags)
GLM_CFLAGS := -I../glm/
SDL_LDFLAGS := $(shell sdl2-config --libs)
########
# This is the default action
all:Build
########
# Object list
#
OBJ = $(B_DIR)/$(FILE).o
########
# Objects
$(B_DIR)/$(FILE).o : $(S_DIR)/$(FILE).cpp $(S_DIR)/SDLauxiliary.h $(S_DIR)/TestModelH.h $(S_DIR)/tiny_obj_loader.h
$(CC) $(CC_OPTS) -o $(B_DIR)/$(FILE).o $(S_DIR)/$(FILE).cpp $(SDL_CFLAGS) $(GLM_CFLAGS)
########
# Main build rule
Build : $(OBJ) Makefile
mkdir -p $(B_DIR) && $(CC) $(LN_OPTS) -o $(EXEC) $(OBJ) $(SDL_LDFLAGS)
clean:
rm -f $(B_DIR)/*
最佳答案
在实际包含 tiny_obj_loader.h
之前一定要#define TINYOBJLOADER_IMPLEMENTATION
。否则,预处理器将已经在该文件上运行,您将得不到实现。
关于尝试包含微小的 obj 加载程序头文件时出现 C++ 链接器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48686939/