I’am currently learning OpenGL through the site https://learnopengl.com/ and i was trying to make a class for the texture, but it is giving an linking error that it does not main file.
This is the away that the code is working
Application.cpp
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
int main()
{
// CODE
// load and create a texture
// -------------------------
unsigned int texture;
// texture
// ---------
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
// set the texture wrapping parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
// set texture filtering parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// load image, create texture and generate mipmaps
int width, height, nrChannels;
stbi_set_flip_vertically_on_load(true); // tell stb_image.h to flip loaded texture's on the y-axis.
unsigned char* data = stbi_load("resources/textures/container.jpg", &width, &height, &nrChannels, 0);
if (data)
{
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
}
else
{
std::cout << "Failed to load texture" << std::endl;
}
stbi_image_free(data);
}
The problem is when i tried to implement this code in a separated class it gives a link error
Texture.h
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
class Texture
{
public:
unsigned int ID{};
Texture(const char* texPath);
private:
};
Texture.cpp
#include "Texture.h"
Texture::Texture(const char* texPath)
{
// Create the texture in OpenGL
glGenTextures(1, &ID);
glBindTexture(GL_TEXTURE_2D, ID);
// set the texture wrapping parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
// set texture filtering parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// load image, create texture and generate mipmaps
int width;
int height;
int nrChannels;
unsigned char* data = stbi_load(texPath, &width, &height, &nrChannels, 0);
if (data)
{
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
}
else
{
std::cout << "Failed to load texture" << std::endl;
}
stbi_image_free(data);
}
Application.cpp
#include "../Texture/Texture.h"
int main()
{
// CODE
Texture texture1{ "resources/textures/container.jpg" };
}
1>Texture.obj : error LNK2005: “Some function of the stb_image.h” already defined in Application.obj
Torta de limão is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1