0

I'm trying to create a game engine. So far, I just wanted to test if my window creation program worked. I got an error:

LNK2019 unknown external symbol __imp_glewInit called in function "public: __cdecl window::window(int,int,char const*)"

I work with Visual Studio 2019 and I added additional include directory with glew library. What's more interesting, the problem occurs in function, that is defined in file with path myproject/classes/window.cpp, where myproject is my project directory. I have files directly inside the myproject directory and when i use glew in them, everything works alright. Here's my code: window.h

#pragma once
class window
{
public:
    GLFWwindow* wnd;

    window(int width, int height, const char* title);
    void close();
};

window.cpp

#include "pch.h"
#include "..\headers.h"

#include "window.h"

window::window(int width, int height, const char* title)
{
    glfwWindowHint(GLFW_SAMPLES, 4);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_COMPAT_PROFILE, GL_TRUE);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

    wnd = glfwCreateWindow(width, height, title, NULL, NULL);

    if (wnd == NULL) { glfwTerminate(); return; }

    glfwMakeContextCurrent(wnd);

    if (glewInit() != GLEW_OK) {
        glfwTerminate();
        return;
    }
}

void window::close() {
    glfwDestroyWindow(this->wnd);
}

Also, there is no "pch.h" file in myproject/classes directory. When i try "..\pch.h", Visual Studio gives me an error.

Am I doing something wrong? Should I somehow tell VS to do something with the classes directory?

PS headers.h file is in the root directory and contains

#pragma once

#include <GL/glew.h>
#include <GLFW/glfw3.h>

I tried to put above two lines into the window.cpp file, but it didn't work as well - I got exactly the same error.

Directory tree:

Pancake2d
|
|-classes
|--window.cpp
|--window.h
|-core (empty so far)
|-Debug (traditional vs debug directory)
|-lib (glew, glfw, glm here, all linked to project with vs)
|-dllmain.cpp
|-framework.h
|-headers.h
|-opengl.dll
|-pancake2d.cpp
|-pancake2d.h
|-Pancake2d.vcxproj
|-Pancake2d.vcxproj.filters
|-Pancake2d.vcxproj.user
|-pch.cpp
|-pch.h

Update: Rrright... So... the problem is no more. I did nothing and it worked. All I did was I just added line glewInit() in pancake2d.cpp. I got a warning, that I'm declaring a function glewInit, and import it in classes/window.cpp, so I changed the line to if (glewInit() != GLEW_OK) return false;, which is basically the same line I used in "classes/window.cpp". I compiled - no errors, no warnings. I comment the line and compile - no errors, no warnings. I delete the line and compile - no errors, no warnings.

Update 2: The problem seemed to be not putting the glew dependency in linker. Seems like I have added header files to additional folders for include, but I was missing Linker settings.

genpfault
  • 51,148
  • 11
  • 85
  • 139
Sherlock Holmes
  • 191
  • 1
  • 8
  • you need to link to the glew library – Alan Birtles Aug 04 '20 at 08:45
  • Did you add glew.lib as a linker input? – Botje Aug 04 '20 at 08:46
  • Yes, I did. I tried this function before, but out of class and in pancake2d/pancake2d.cpp file and it worked. (Pancake2d is the name of engine). The problem occured first, when I put it into another file. I tries putting the files out of classes dir, but it also didn't work – Sherlock Holmes Aug 04 '20 at 09:05
  • Show us your directory tree. Do you have multiple projects in your solution? – Botje Aug 04 '20 at 09:08
  • Directory tree written. – Sherlock Holmes Aug 04 '20 at 09:18
  • Are you sure you're building the full project and not just the singular window.cpp? What is the full build output? – Botje Aug 04 '20 at 09:55
  • And please show us the full linker command (you can see this in the linker settings of the project, or turn on the /VERBOSE flag) – Botje Aug 04 '20 at 10:01
  • Output should be dll in a seperate folder (I set the output directory). Full linker command is `opengl32.lib;glu32.lib;lib\glfw-3.1.2\src\Debug\glfw3.lib;lib\Debug\GLEW_1130.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)` – Sherlock Holmes Aug 04 '20 at 10:36
  • Normally everything was correct with it, but compiling crashes when trying to access glew functions in another file – Sherlock Holmes Aug 04 '20 at 10:37

0 Answers0