I am recently making a basic image processing program in C++. I have created a C headfile to declare functions and two .cpp files: One to define functions from said headfile and, two to execute said functions I am following a tutorial, so i dont quite understand a few of the data types. Here is the first file that i use to declare the image proc functions that i am having trouble with:
#include <stdint.h>
#include <cstdio>
enum ImageType {
PNG, JPG, BMP, TGA
};
struct Image {
uint8_t* data;
size_t size;
int w, h, channels;
Image(const char* filename);
Image(int w, int h, int channels);
Image(const Image& img);
~Image();
bool read(const char* filename);
bool write(const char* filename);
ImageType getFileType(const char* filename);
};
and here is the error message:
Undefined symbols for architecture x86_64:
"Image::write(char const*)", referenced from:
_main in itras-8e652b.o
"Image::Image(char const*)", referenced from:
_main in itras-8e652b.o
"Image::Image(Image const&)", referenced from:
_main in itras-8e652b.o
"Image::Image(int, int, int)", referenced from:
_main in itras-8e652b.o
"Image::~Image()", referenced from:
_main in itras-8e652b.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
and the cpp file:
#define STB_IMAGE_IMPLEMENTATION
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "image.h"
#include "stb_image.h"
#include "stb_image_write.h"
Image::Image(const char* filename){
if(read(filename)) {
printf("Read %s succesfully\n", filename);
} else {
printf("Failed to read %s :(\n", filename);
};
}
Image::Image(int w, int h, int channels) : w(w), h(h), channels(channels) {
size = w*h*channels;
data = new uint8_t[size];
}
Image::Image(const Image& img) : Image(img.w, img.h, img.channels) {
memcpy(data, img.data, img.size);
}
Image::~Image(){
stbi_image_free(data);
}
bool Image::read(const char* filename){
data = stbi_load(filename, &w, &h, &channels, 0);
return data != NULL;
}
bool Image::write(const char* filename){
ImageType type = getFileType(filename);
int success;
switch (type) {
case PNG:
success = stbi_write_png(filename, w, h, channels, data, w*channels);
case BMP:
success = stbi_write_bmp(filename, w, h, channels, data);
case TGA:
success = stbi_write_tga(filename, w, h, channels, data);
case JPG:
success = stbi_write_jpg(filename, w, h, channels, data, 100);
break;
}
return success != 0;
}
ImageType Image::getFileType(const char* filename) {
const char* ext = strrchr(filename, '.');
if (ext != nullptr) {
if (strcmp(ext, ".png")==0) {
return PNG;
}
else if (strcmp(ext, ".jpg")==0) {
return JPG;
}
else if (strcmp(ext, ".bmp")==0) {
return BMP;
}
else if (strcmp(ext, ".tga")==0) {
return TGA;
}
}
return PNG;
}
If it helps, i am using vscode on a mac, not xcode. Thank you.