0

I am coding a OpenGL renderer (I'm mentioning this in case it hast something to to with my error) and getting this error whenever I try to use a std::string in my code:

Undefined symbols for architecture arm64:
  "std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::__init(char const*, unsigned long)", referenced from:
      std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::basic_string<std::__1::nullptr_t>(char const*) in main.cpp.o
  "std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::~basic_string()", referenced from:
      _main in main.cpp.o
  "std::terminate()", referenced from:
      ___clang_call_terminate in main.cpp.o
  "___cxa_begin_catch", referenced from:
      ___clang_call_terminate in main.cpp.o
  "___gxx_personality_v0", referenced from:
      _main in main.cpp.o
      Dwarf Exception Unwind Info (__eh_frame) in main.cpp.o
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

My makefile looks like this:

TARGET_EXEC := render

BUILD_DIR := ./build
SRC_DIRS := ./src

CC := clang
CXX := clang++
SRCS := $(shell find $(SRC_DIRS) -name '*.cpp' -or -name '*.c' -or -name '*.s')
OBJS := $(SRCS:%=$(BUILD_DIR)/%.o)
DEPS := $(OBJS:.o=.d)
LDFLAGS := -L/opt/homebrew/Cellar/glfw/3.3.8/lib/ /opt/homebrew/Cellar/glfw/3.3.8/lib/libglfw.3.3.dylib -framework OpenGL -framework Cocoa -framework IOKit -framework CoreVideo -framework CoreFoundation
INC_DIRS := $(shell find $(SRC_DIRS) -type d)
INC_FLAGS := -I ./inc -I /opt/homebrew/Cellar/glfw/3.3.8/include/
CPPFLAGS := $(INC_FLAGS) -MMD -MP
CFLAGS := 
CXXFLAGS := -Wall -g -Wno-deprecated

$(BUILD_DIR)/$(TARGET_EXEC): $(OBJS)
    $(CC) $(OBJS) -o $@ $(LDFLAGS)

$(BUILD_DIR)/%.c.o: %.c
    @mkdir -p $(dir $@)
    $(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $@

$(BUILD_DIR)/%.cpp.o: %.cpp
    @mkdir -p $(dir $@)
    $(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $< -o $@

.PHONY: clean
clean:
    @rm -r $(BUILD_DIR)
-include $(DEPS)

this is my main.cpp (its the OpenGL tutorial from The Cherno)

#include <GLFW/glfw3.h>
#include <stdio.h>
#include <string>

static unsigned int CompileShader(unsigned int type, const std::string &source)
{
    unsigned int id = glCreateShader(type);
    const char *src = source.c_str();
    glShaderSource(id, 1, &src, nullptr);
    glCompileShader(id);

    int result;
    glGetShaderiv(id, GL_COMPILE_STATUS, &result);
    if (result == GL_FALSE) {
        int length = 0;
        glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length);
        char *message = (char *)alloca(length * sizeof(char));
        glGetShaderInfoLog(id, length, &length, message);
        printf("Failed to compile %sshader!", (type == GL_VERTEX_SHADER ? "vertex" : "fragment"));
        printf("%s", message);
        glDeleteShader(id);
        return 0;
    }

    return id;
}

static unsigned int CreateShader(const std::string &vertexShadersrc, const std::string &fragmentShadersrc)
{
    unsigned int program = glCreateProgram();
    unsigned int vs = CompileShader(GL_VERTEX_SHADER, vertexShadersrc);
    unsigned int fs = CompileShader(GL_FRAGMENT_SHADER, fragmentShadersrc);

    glAttachShader(program, vs);
    glAttachShader(program, fs);
    glLinkProgram(program);
    glValidateProgram(program);

    glDeleteShader(vs);
    glDeleteShader(fs);

    return program;
}

int main(void)
{
    GLFWwindow *window;

    /* Initialize the library */
    if (!glfwInit())
        return -1;

    /* Create a windowed mode window and its OpenGL context */
    window = glfwCreateWindow(640, 480, "Window", NULL, NULL);
    if (!window)
    {
        glfwTerminate();
        return -1;
    }

    /* Make the window's context current */
    glfwMakeContextCurrent(window);

    printf("%s", glGetString(GL_VERSION));

    float positions[6] = {
        -0.5f,-0.5f,
         0.0f, 0.5f,
         0.5f,-0.5f,
    };

    unsigned int buffer;
    glGenBuffers(1, &buffer);
    glBindBuffer(GL_ARRAY_BUFFER, buffer);
    glBufferData(GL_ARRAY_BUFFER, 6 * sizeof(float), positions, GL_STATIC_DRAW);

    glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 2, 0);
    glEnableVertexAttribArray(0);

    std::string vertexShadersrc = 
        "#version 330 core\n"
        "\n"
        "layout(location = 0) in vec4 position;"
        "\n"
        "void main()\n"
        "{\n"
        "   gl_Position = position;\n"
        "}\n"
        ;
    std::string fragmentShadersrc = 
        "#version 330 core\n"
        "\n"
        "layout(location = 0) out vec4 position;"
        "\n"
        "void main()\n"
        "{\n"
        "   color = vec4(1.0, 0.0, 0.0, 1.0 );\n"
        "}\n"
        ;

    unsigned int shader = CreateShader(vertexShadersrc, fragmentShadersrc);

    glUseProgram(shader);
    /* Loop until the user closes the window */
    while (!glfwWindowShouldClose(window))
    {

        /* Render here */
        glClear(GL_COLOR_BUFFER_BIT);

        // Triangle begin
        glDrawArrays(GL_TRIANGLE_STRIP, 0, 3);
        // Triangle end

        /* Swap front and back buffers */
        glfwSwapBuffers(window);

        /* Poll for and process events */
        glfwPollEvents();
    }

    glfwTerminate();
    return 0;
}

I researched and found other people having this issue with other self-written classes but since std::string is a class from the C++ stdlib I didn't find anything usefull. I tried doing -lstring (to maybe link the string library) (In case you didn't know this error also appears when you you a header without linking the binaries)

Please help me out with this.

Thank You.

SparkDev
  • 66
  • 9
  • 4
    Please add a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) in addition to the issue you are experiencing – The Dreams Wind Oct 28 '22 at 21:14
  • 1
    The problem is in your code or how you are building it. We can't help seeing only the errors. – drescherjm Oct 28 '22 at 21:16
  • I updated It now can someone please look at this and maybe help me? – SparkDev Oct 29 '22 at 07:53
  • btw i am getting the same error when I'm using `std::cout << "somestring";` Thats bc I use `printf("somestring");` instead – SparkDev Oct 29 '22 at 07:54
  • Specifically https://stackoverflow.com/a/67236678/775806 – n. m. could be an AI Oct 29 '22 at 07:59
  • This hasn't helped me at all I am getting an error for a class thats in the *stdlib* not for a self written class like in the example you sent me it isn't even a library that has to be linked instead of solving my problem you closed my question and sent a link that doesnt answer my point. I guess Stackoverflow is just a shitty platform for grumpy people who complain if a question is duplicated or 'not well written' I am trying to solve my errors and run my code but no you people close my question. I'm done with Stackoverflow. – SparkDev Oct 29 '22 at 09:17
  • @n.m. I'm tagging you so you can read my message – SparkDev Oct 29 '22 at 09:26
  • "This hasn't helped me at all" I have no idea what you have done. If you are getting errors with a modified makefile, post the complete modified makefile, otherwise it is a wild guess game. – n. m. could be an AI Oct 29 '22 at 09:35
  • @n.m. what do do you mean with modified makefile I posted the makefile i use I simply type in my shell `make` and it gets compiled. The Makefile I use is the **exact** same I use to compile my project ( src/main.c is the only file in my project) – SparkDev Oct 29 '22 at 09:40
  • Your makefile is wrong. I told you exactly why it is wrong. I am giving you the link again https://stackoverflow.com/a/67236678/775806. Please read it. – n. m. could be an AI Oct 29 '22 at 09:44
  • @n.m. I have but when you know it so exactly then say it? – SparkDev Oct 29 '22 at 09:47
  • I think the linked answer explains exactly what your error is. Is there something unclear in it? I'll say it again here. **When you have C++ code, don't use clang as a linker, use clang++ instead**. – n. m. could be an AI Oct 29 '22 at 09:50
  • @n.m. I read it and found nothing that even mentions the use of a Makefile or the compilation process. All answers have to do with the source code – SparkDev Oct 29 '22 at 09:51
  • @n.m. In my Makefile `CXX := clang++` – SparkDev Oct 29 '22 at 09:52
  • In your Makefile `CC := clang` and **you are using CC to link**. You owe me $250. – n. m. could be an AI Oct 29 '22 at 09:53
  • @n.m. Oh Shoot i've seen it Thank you so much – SparkDev Oct 29 '22 at 09:53
  • This helped me SOOO much – SparkDev Oct 29 '22 at 09:54
  • I'm sorry that I didn't read my makefile – SparkDev Oct 29 '22 at 09:54

0 Answers0