【问题标题】:ld: symbol(s) not found for architecture x86_64 when trying to compile projectld:尝试编译项目时找不到架构 x86_64 的符号
【发布时间】:2020-06-16 18:13:59
【问题描述】:

我目前正在学习关于 opengl 的在线课程。

为此,我不得不重新激活我对 c++ 的旧知识。

在进行了一些重构之后,我现在遇到了一个我不理解的链接错误。

我有以下文件:

Mesh.h, Mesh.cpp - 像这样的普通类:

#pragma once

#include </usr/local/Cellar/glew/2.1.0_1/include/GL/glew.h>

class Mesh
{
    public:
        Mesh();

        void createMesh(GLfloat *vertices, unsigned int *indices, unsigned int numOfVerticies, unsigned int numOfIndices);
        void RenderMesh();
        void clearMesh();

        ~Mesh();

    private:
        GLuint VAO, VBO, IBO;
        GLsizei indexCount;
};

和 cpp 实现:

#include "Mesh.h"

Mesh::Mesh() 
{
    VAO = 0;
    VBO = 0;
    IBO = 0;
    indexCount = 0;
}

void Mesh::createMesh(GLfloat *vertices, unsigned int *indices, unsigned int numOfVerticies, unsigned int numOfIndices) {
    indexCount = numOfIndices;

    glGenVertexArrays(1, &VAO);
    glBindVertexArray(VAO);

    glGenBuffers(1, &IBO);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices[0]) * numOfIndices, indices, GL_STATIC_DRAW);

    glGenBuffers(1, &VBO);
    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices[0]) *numOfVerticies, vertices, GL_STATIC_DRAW);

    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
    glEnableVertexAttribArray(0);

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
    glBindBuffer(GL_ARRAY_BUFFER, 0);


    glBindVertexArray(0);    

}

void Mesh::RenderMesh() {
    glBindVertexArray(VAO);
    //glDrawArrays(GL_TRIANGLES, 0, 3);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO);
    glDrawElements(GL_TRIANGLES, indexCount, GL_UNSIGNED_INT, 0);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
    glBindVertexArray(0);
}

void Mesh::clearMesh() {
    if(IBO != 0) {
        glDeleteBuffers(1, &IBO);
        IBO = 0;
    }

    if(VBO != 0) {
        glDeleteBuffers(1, &VBO);
        VBO = 0;
    }

    if(VAO != 0) {
        glDeleteVertexArrays(1, &VAO);
        VAO = 0;
    }

    indexCount = 0;
}


Mesh::~Mesh()
{
    clearMesh();
}

我还有另一个名为 opengl-beginner-chapter-1.cpp 的文件作为主文件(但这里显示太长)。

我创建了以下makefile:

CC = gcc
CFLAGS =
mesh = Mesh
opengl_beginner_chapter1 = opengl-beginner-chapter1
OBJ = $(mesh).o $(opengl_beginner_chapter1).o
FINAL = opengl_beginner_chapter1

mesh: $(mesh).cpp
    $(CC) $(CFLAGS) -c $< -o $(mesh).o

opengl_beginner_chapter1: $(opengl_beginner_chapter1).cpp
    $(CC) $(CFLAGS) -c $< -o $(opengl_beginner_chapter1).o

all: mesh opengl_beginner_chapter1
    $(CC) -L /usr/local/Cellar/glfw/3.3.2/lib -lglfw  -L /usr/local/Cellar/glew/2.1.0_1/lib -lGLEW -framework OpenGL $(OBJ) -o $(FINAL)

当我在我的 mac (mojave) 上运行它时,我收到以下错误:

gcc -v -L /usr/local/Cellar/glfw/3.3.2/lib -lglfw  -L /usr/local/Cellar/glew/2.1.0_1/lib -lGLEW -framework OpenGL Mesh.o opengl-beginner-chapter1.o -o opengl_beginner_chapter1
Apple LLVM version 10.0.1 (clang-1001.0.46.4)
Target: x86_64-apple-darwin18.7.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
 "/Library/Developer/CommandLineTools/usr/bin/ld" -demangle -lto_library /Library/Developer/CommandLineTools/usr/lib/libLTO.dylib -dynamic -arch x86_64 -macosx_version_min 10.14.0 -syslibroot /Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk -o opengl_beginner_chapter1 -L/usr/local/Cellar/glfw/3.3.2/lib -L/usr/local/Cellar/glew/2.1.0_1/lib -lglfw -lGLEW -framework OpenGL Mesh.o opengl-beginner-chapter1.o -L/usr/local/lib -lSystem /Library/Developer/CommandLineTools/usr/lib/clang/10.0.1/lib/darwin/libclang_rt.osx.a
Undefined symbols for architecture x86_64:
  "std::__1::__vector_base_common<true>::__throw_length_error() const", referenced from:
      std::__1::vector<Mesh*, std::__1::allocator<Mesh*> >::__recommend(unsigned long) const in opengl-beginner-chapter1.o
  "std::logic_error::logic_error(char const*)", referenced from:
      std::length_error::length_error(char const*) in opengl-beginner-chapter1.o
  "std::length_error::~length_error()", referenced from:
      std::__1::__throw_length_error(char const*) in opengl-beginner-chapter1.o
  "std::terminate()", referenced from:
      ___clang_call_terminate in opengl-beginner-chapter1.o
  "typeinfo for std::length_error", referenced from:
      std::__1::__throw_length_error(char const*) in opengl-beginner-chapter1.o
  "vtable for std::length_error", referenced from:
      std::length_error::length_error(char const*) in opengl-beginner-chapter1.o
  NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
  "operator delete(void*)", referenced from:
      createTriangle() in opengl-beginner-chapter1.o
      std::__1::__libcpp_deallocate(void*, unsigned long) in opengl-beginner-chapter1.o
  "operator new(unsigned long)", referenced from:
      createTriangle() in opengl-beginner-chapter1.o
      std::__1::__libcpp_allocate(unsigned long, unsigned long) in opengl-beginner-chapter1.o
  "___cxa_allocate_exception", referenced from:
      std::__1::__throw_length_error(char const*) in opengl-beginner-chapter1.o
  "___cxa_begin_catch", referenced from:
      ___clang_call_terminate in opengl-beginner-chapter1.o
  "___cxa_call_unexpected", referenced from:
      std::__1::__vector_base<Mesh*, std::__1::allocator<Mesh*> >::__destruct_at_end(Mesh**) in opengl-beginner-chapter1.o
      std::__1::allocator<Mesh*>::deallocate(Mesh**, unsigned long) in opengl-beginner-chapter1.o
      std::__1::vector<Mesh*, std::__1::allocator<Mesh*> >::max_size() const in opengl-beginner-chapter1.o
      std::__1::__split_buffer<Mesh*, std::__1::allocator<Mesh*>&>::__destruct_at_end(Mesh**, std::__1::integral_constant<bool, false>) in opengl-beginner-chapter1.o
  "___cxa_free_exception", referenced from:
      std::__1::__throw_length_error(char const*) in opengl-beginner-chapter1.o
  "___cxa_throw", referenced from:
      std::__1::__throw_length_error(char const*) in opengl-beginner-chapter1.o
  "___gxx_personality_v0", referenced from:
      createTriangle() in opengl-beginner-chapter1.o
      std::__1::__vector_base<Mesh*, std::__1::allocator<Mesh*> >::__destruct_at_end(Mesh**) in opengl-beginner-chapter1.o
      std::__1::allocator<Mesh*>::deallocate(Mesh**, unsigned long) in opengl-beginner-chapter1.o
      void std::__1::vector<Mesh*, std::__1::allocator<Mesh*> >::__push_back_slow_path<Mesh* const>(Mesh* const&) in opengl-beginner-chapter1.o
      std::__1::vector<Mesh*, std::__1::allocator<Mesh*> >::max_size() const in opengl-beginner-chapter1.o
      std::__1::__throw_length_error(char const*) in opengl-beginner-chapter1.o
      std::__1::__split_buffer<Mesh*, std::__1::allocator<Mesh*>&>::__destruct_at_end(Mesh**, std::__1::integral_constant<bool, false>) in opengl-beginner-chapter1.o
      ...
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

很遗憾,我无法理解此错误消息。

任何建议都非常受欢迎。

【问题讨论】:

  • 尝试使用g++ 命令来构建和链接您的程序。
  • 是的,就是这样。非常感谢:)

标签: c++ macos


【解决方案1】:

尝试g++ 而不是gcc 你正在使用c 编译器来编译c++ 程序。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-16
    • 1970-01-01
    • 2013-11-18
    • 2020-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多