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

标签 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

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

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/

相关文章:

c++ - 在struct和main中分配内存的区别?

c++ - 方法返回模板类 C++

c++ - 通过引用捕获对象

c++ - 各种数组操作

c++ - Doxygen 私有(private)函数

c++ - 将大小为 4 字节的管道读入 4 字节 int 如何返回更多数据?

c++ - C++ 类的常量正确性

c++ - 缩放以匹配大小

c++ - 使用 clock() 函数安排任务时出现问题

c++ - 静态库性能 - 可以内联调用吗?