0

I've been struggling to find why my linker gets an unresolved external symbol error. The error looks like this:

Error
LNK2019
unresolved external symbol "public: __thiscall Shader::Shader(char const *)" (??0Shader@@QAE@PBD@Z) referenced in function "public: __thiscall GridWorldGPGPU::GridWorldGPGPU(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,unsigned int)" (??0GridWorldGPGPU@@QAE@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@I@Z)
DV2556_Project  
grid_world_GPGPU.obj
1       

As far as I understand it it has something to do with that my linker finds the declaration of the Shader::Shader(char const *)-function but cannot find the definition. I have been staring at this for hours and cannot figure out why the linker becomes sad.

grid_world_GPGPU.h:

#ifndef GRID_WORLD_GPGPU_H
#define GRID_WORLD_GPGPU_H

#include "grid_world.h"
#include <../swift_open_gl.h>

class GridWorldGPGPU : public GridWorld {
private:
    Shader* compute_shader_ = nullptr;

public:
    GridWorldGPGPU(std::string in_path_shader, unsigned int in_side = 1);
};

#endif // !GRID_WORLD_GPGPU_H

grid_world_GPGPU.cpp:

GridWorldGPGPU::GridWorldGPGPU(std::string in_path_shader, unsigned int in_side)
    : GridWorld(in_side) {
    this->compute_shader_ = new Shader(in_path_shader.c_str());
}

The Shader-class is defined in the swift_open_gl.h file:

#ifndef SWIFT_OPEN_GL_H
#define SWIFT_OPEN_GL_H

#include <glad/glad.h>
#include <GLFW/glfw3.h>

class Shader {
public:
    Shader(const char* cs_path);
};
#endif // !SWIFT_OPEN_GL_H

And swift_open_gl.cpp has this:

#include "..\swift_open_gl.h"

inline Shader::Shader(const char * cs_path) {
    //Do stuff
}

I've tried with and without the inline (Visual Studio added it when I tried moving the function definition between the .h-file and a .cpp-file, so I decided to try it) and i have verified that the #include <../swift_open_gl.h> doesn't occur anywhere else in the project than the files listed above.

An extra set of eyes to look over this would be appreciated!

Hoxbot
  • 105
  • 9
  • 1
    cpp files are compiled to object files. Object files are linked to executables. It can be done implicitly when you compile all cpp files together, or explicitly when you first compile and generate object files. After that link the object files together. If you use an IDE, all the files should be added to the same project, and the IDE will care how the files are compiled and linked. – armagedescu May 07 '20 at 17:44
  • `inline Shader::Shader(const char * cs_path) {` get rid of the `inline` and make sure that `swift_open_gl.cpp` is part of your project. – drescherjm May 07 '20 at 17:56
  • Does this answer your question? [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – ChrisMM May 08 '20 at 22:27

1 Answers1

1

Provide default constructor of the class as well.

class Shader {
    public:
    Shader(){} // default constructor
    Shader(const char* cs_path);    
    };

Dont use inline in Shader::Shader(const char* cs_path){} definition

user9559196
  • 157
  • 6