0

Why am I getting an undedined reference to this function?

I have three files: window.h: #include <SDL2/SDL.h> #include <SDL2/glew.h> class Window { public: Window() {} ~Window() {} void run(); void initSDL(); private: SDL_Window* _window; int _width = 1024; int _height = 678; }; window.cpp: #include "window.h" int main() { void Window::Window() {   _window = nullptr; } void Window::~Window() { } void Window::run() {   initSDL(); } void Window::initSDL() {   SDL_Init(SDL_INIT_EVERYTHING);   _window = SDL_CreateWindow("Neptune's Limit", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, _width, _height, SDL_WINDOW_OPENGL); } } main.cpp: #include <iostream> #include "window.h" #include "window.cpp" int main(int argc, char* argv[]) {    Window window;    window.run();    std::cout << "Enter any character to continue" << std::endl;    char a;    std::cin >> a;    return 0; } When I run it, nevermind all the confusing SDL functions, I get an undefined reference to Window::run(). Why?

9th Sep 2018, 4:35 PM
jacksonofgames 28
jacksonofgames 28 - avatar
6 Answers
+ 1
I'm guessing it is a runtime error because window is uninitialized. You shouldn't be including window.cpp.
9th Sep 2018, 4:48 PM
John Wells
John Wells - avatar
+ 1
You need link your program. In simple case your program consist from only one cpp file. In that case compiler build a program for you from that file and a standard library. In your case you have 2 cpp file an custom library. It means that you need more complex compilation procedure. You need compile all your cpp file into obj file by separate compilation call. Then your need to link your program from all your obj files and custom libraries. You can look at compiler flags for more information. Also you could look at CMake tool that simplify build procedure.
9th Sep 2018, 6:57 PM
Sergey Ushakov
Sergey Ushakov - avatar
+ 1
jacksonofgames 28 you did it by including. It is wrong way. You unable to include sdl library.
10th Sep 2018, 3:36 AM
Sergey Ushakov
Sergey Ushakov - avatar
0
I kinda thought not, but I had already tried it without including window.cpp and it didnt work either
9th Sep 2018, 6:26 PM
jacksonofgames 28
jacksonofgames 28 - avatar
0
So I need to find some way to integrate window.cpp into the main.cpp compilation
10th Sep 2018, 2:56 AM
jacksonofgames 28
jacksonofgames 28 - avatar
0
Its okay I worked it into the command line
12th Sep 2018, 3:00 AM
jacksonofgames 28
jacksonofgames 28 - avatar