尝试包含微型 obj 加载器头文件时出现 C++ 链接器错误

Gre*_*ims 2 c++

我正在尝试使用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
Run Code Online (Sandbox Code Playgroud)

然而,这给了我以下链接器错误:

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
Run Code Online (Sandbox Code Playgroud)

其中函数定义是:

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);
Run Code Online (Sandbox Code Playgroud)

看起来参数类型对我来说是正确的。

我已将该.h文件包含skeleton.cpp

#include "tiny_obj_loader.h"
Run Code Online (Sandbox Code Playgroud)

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)/* 
Run Code Online (Sandbox Code Playgroud)

Bor*_*der 6

请务必#define TINYOBJLOADER_IMPLEMENTATION在实际包含之前tiny_obj_loader.h。否则,预处理器将已经在该文件上运行,并且您将无法获得实现。