toom
toom

Reputation: 13316

ld: symbol(s) not found for architecture x86_64 when trying to compile project

I am currently working through an online course about opengl.

to do so I had to reactivate my old knowledge about c++.

After having done some refactoring I now encounter an linking error that I do not understand.

I have the following files:

Mesh.h, Mesh.cpp - a normal class like this:

#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;
};

and the cpp implementation:

#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();
}

I also have another file called opengl-beginner-chapter-1.cpp which acts as the main file (but it is too long to show it here).

And I created the following 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)

When I run this on my mac (mojave) I get the following error:

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)

Unfortunately I am unable to understand this error message.

Any advise is highly appriciated.

Upvotes: 0

Views: 1241

Answers (1)

user13292935
user13292935

Reputation:

Try g++ instead of gcc you are using c compiler to compile c++ program.

Upvotes: 1

Related Questions