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.