Sergio Delgado
Sergio Delgado

Reputation: 1

Function definition compiled inside .h file, but not on .cpp file, using C++17 and VS code

I was trying to implement in a .cpp file the definition of setColor(), which was correctly compiled before within the .h file from Sphere class, but now I get the next linker error:

Starting build...
cmd /c chcp 65001>nul && g++.exe -std=c++17 -g -I include -L lib src\* -o bin\helloworld.exe -lfreeglut -lopengl32 -lglu32
C:/msys64/ucrt64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\sdelgado\AppData\Local\Temp\ccIOwL6S.o: in function `main':
C:\msys64\home\sdelgado\helloworld/src/main.cpp:55:(.text+0x1b3): undefined reference to `Sphere::setColor(unsigned char, unsigned char, unsigned char)'
collect2.exe: error: ld returned 1 exit status

sphere.h

class Sphere
{
public:
    float radio;
    float x;
    float y;
    unsigned char red;
    unsigned char green;
    unsigned char blue;
    void setColor( unsigned char r, unsigned char g, unsigned char b);
};

sphere.cpp

#include "sphere.h"
void Sphere::setColor(unsigned char r,unsigned char g, unsigned char b)
{
    red=r;
    green=g;
    blue=b;
}

main.cpp

#include "sphere.h"
Sphere sphere;
main()
{
[...]
sphere.setColor(200,0,0); 
}

Is it maybe necessary to add some other include that I am skipping?

Upvotes: -1

Views: 55

Answers (1)

Sergio Delgado
Sergio Delgado

Reputation: 1

It was necessary to include every .h and .cpp files on the src folder.

Upvotes: 0

Related Questions