C 链接器错误尝试包含Tiny OBJ加载程序标头文件

C++ Linker error when trying to include tiny obj loader header file

本文关键字:加载 OBJ 程序 文件 Tiny 包含 链接 错误      更新时间:2023-10-16

我正在尝试使用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);

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

我已在

中将.h文件包含在skeleton.cpp
#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。否则,预处理器将已经在该文件上运行,并且您将无法获得实现。