0

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.

drescherjm
  • 10,365
  • 5
  • 44
  • 64
Johnny
  • 1
  • 1
  • If my link doesn't help, please edit your question to include the contents of the `cpp` file that defines those functions, plus more details on how you are compiling your project. – 0x5453 Feb 04 '21 at 13:50
  • Are you linking all the object files? – Devolus Feb 04 '21 at 13:52
  • ***i am using vscode on a mac*** Did you modify your tasks.json to have it build more than 1 file? The default builds only the active file. The documentation explains this here: [https://code.visualstudio.com/docs/cpp/config-clang-mac#_modifying-tasksjson](https://code.visualstudio.com/docs/cpp/config-clang-mac#_modifying-tasksjson) – drescherjm Feb 04 '21 at 14:08
  • 1
    You have tagged this question as both `C` and `C++`. One can have member functions, the other cannot. What exactly are you attempting to compile this as? – ryyker Feb 04 '21 at 14:23

0 Answers0