Dolarun
@Dolarun

Почему линковщик жалуется на undefined reference?

Я подключил библиотеки, но линковщик всё равно не пропускает.
Структура проекта:
620f78290bbb0851588522.png
Консоль:
g++ -LC:\Users\79306\Documents\GitHub\2DPrimitiveLib\source\deps\lib -IC:\Users\79306\Documents\GitHub\2DPrimitiveLib\source\deps\include -Wl,--start-group -lglfw3 -lopengl32 -lglad -Wl,--end-group Window.cpp main.cpp -o exe.exe
c:/users/79306/desktop/development-libraries/mingw/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\79306\AppData\Local\Temp\ccUvV73h.o:Window.cpp:(.text+0x2e): undefined reference to `glfwInit'
c:/users/79306/desktop/development-libraries/mingw/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\79306\AppData\Local\Temp\ccUvV73h.o:Window.cpp:(.text+0x3d): undefined reference to `glfwWindowHint'
c:/users/79306/desktop/development-libraries/mingw/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\79306\AppData\Local\Temp\ccUvV73h.o:Window.cpp:(.text+0x4c): undefined reference to `glfwWindowHint'
c:/users/79306/desktop/development-libraries/mingw/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\79306\AppData\Local\Temp\ccUvV73h.o:Window.cpp:(.text+0x5b): undefined reference to `glfwWindowHint'
c:/users/79306/desktop/development-libraries/mingw/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\79306\AppData\Local\Temp\ccUvV73h.o:Window.cpp:(.text+0x8d): undefined reference to `glfwCreateWindow'
c:/users/79306/desktop/development-libraries/mingw/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\79306\AppData\Local\Temp\ccUvV73h.o:Window.cpp:(.text+0x153): undefined reference to `glfwDestroyWindow'
c:/users/79306/desktop/development-libraries/mingw/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\79306\AppData\Local\Temp\ccUvV73h.o:Window.cpp:(.text+0x163): undefined reference to `glfwTerminate'
c:/users/79306/desktop/development-libraries/mingw/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\79306\AppData\Local\Temp\ccUvV73h.o:Window.cpp:(.rdata$.refptr.glad_glViewport[.refptr.glad_glViewport]+0x0): undefined reference to `glad_glViewport'
collect2.exe: error: ld returned 1 exit status

main.cpp
#include "Window.hpp"

int main()
{
    Window win(233, 322);
    return 0;
}


Window.hpp
#ifndef Window_DEF
#define Window_DEF

#include "deps/include/glad/glad.h"
#include "deps/include/GLFW/glfw3.h"
#include "Point.hpp"
#include <iostream>

class Window
{
private:
    GLFWwindow *window;
    Point currentPoint;

public:
    static int IsInitGLFW;
    Window(int n, int m);

    Point current() const
    {
        return currentPoint;
    }
    Point current(const Point &p);

    ~Window();
};

#endif


Window.cpp
#include "Window.hpp"

int Window::IsInitGLFW = 0;
Window::Window(int n, int m)
{
    if (IsInitGLFW == 0)
    {
        glfwInit();
        glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
        glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
        glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
        IsInitGLFW++;
    }
    window = glfwCreateWindow(n, m, "Window", NULL, NULL);
    // Setting rendering viewport
    glViewport(0, 0, n, m);
    if (window == NULL)
    {
        std::cout << "Failed to create GLFW window" << std::endl;
    }
};

Point Window::current(const Point &p)
{
    currentPoint = p;
    return currentPoint;
}

Window::~Window()
{
    glfwDestroyWindow(window);
    if (IsInitGLFW == 1)
    {
        glfwTerminate();
        IsInitGLFW--;
    }
}

g++ -LC:\Users\79306\Documents\GitHub\2DPrimitiveLib\source\deps\lib -IC:\Users\79306\Documents\GitHub\2DPrimitiveLib\source\deps\include -Wl,--start-group -lglfw3 -lopengl32 -lglad -Wl,--end-group Window.cpp main.cpp -o exe.exe - использую такую команду.
  • Вопрос задан
  • 907 просмотров
Решения вопроса 1
@res2001
Developer, ex-admin
Проверьте, содержатся ли эти символа в подключаемых библиотеках.
Проверить можно с помощью objdump или nm.
glfw3 вы сами собирали? Возможно вы его собрали компилятором С++ и символа у него получились с плюсовым манглингом, а линковщик ищет с Сишным.
Ответ написан
Пригласить эксперта
Ответы на вопрос 1
vt4a2h
@vt4a2h
Senior software engineer (C++/Qt/boost)
У вас в проекте есть файл CMakeLists.txt. Туда и должны быть добавлены все зависимости. Для сборки проекта надо использовать этот генератор систем сборки.
Руками вызывать компилятор и указывать параметры не нужно. Команда вроде cmake --build . --parallel делает всю работу (перед этим надо просто cmake вызвать c нужными параметрами).
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы