//To note I just started learning SDL2 from 30th April so please be considerate of my lack of knowledge
I made a header file called "window.h" as a class.
And when I recall that window class in the main.cpp open a window with the input that i have entered,
the following error occured
PS C:\Users\Admin\Desktop\SDLTuto> make
g++ -I src/include -L src/lib -o main main.cpp -lmingw32 -lSDL2main -lSDL2
C:\Users\Admin\AppData\Local\Temp\ccymEbqx.o:main.cpp:(.text+0x48): undefined reference to `Window::Window(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, int, int)'
C:\Users\Admin\AppData\Local\Temp\ccymEbqx.o:main.cpp:(.text+0x81): undefined reference to `Window::~Window()'
collect2.exe: error: ld returned 1 exit status
make: *** [Makefile:2: all] Error 1
This is the header file
#pragma once
#include <string>
#include <SDL2/SDL.h>
class Window {
public:
Window(std::string title, int WIDTH, int HEIGHT);
~Window();
inline const bool isClosed() {return closed;}
private:
bool init();
private:
std::string _title;
int _WIDTH = 800;
int _HEIGHT = 600;
bool closed = false;
SDL_Window *window = nullptr;//nullptr = 0
};
And this the code that creates the window
#include "window.h"
#include <iostream>
//constructor
Window::Window(const std::string title, int WIDTH, int HEIGHT):
_title(title), _WIDTH(WIDTH), _HEIGHT(HEIGHT)
{
if(!init()){
closed = true;
}
}
Window::~Window(){
SDL_DestroyWindow(window);
SDL_Quit();
}
bool Window::init(){
if(SDL_Init(SDL_INIT_EVERYTHING)){
std::cerr << "Failed to initialize SDL.\n";
return 0;
}
window = SDL_CreateWindow(_title.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, _WIDTH, _HEIGHT, SDL_WINDOW_RESIZABLE);
if (window == nullptr){
std::cerr << "Failed to create window!\n";
return 0;
}
return true;
}
And this is the main.cpp that calls the opening window cpp file
#include "window.h"
int main(int argc, char *argv[]){
Window window("SDL TEST", 800, 600);
while(!window.isClosed()){
}
return 0;
}
I put the header file in the include folder which contains SDL2 libraries
I have tried making a new project and moving the codes to the new one
Makefile
all:
g++ -I src/include -L src/lib -o main main.cpp -lmingw32 -lSDL2main -lSDL2