"Undefined reference to WinMain"

108 views Asked by At

I'm currently working on a coding project and having some difficulties with this error message

ygwin/11/../../../../lib/libcygwin.a(libcmain.o): in function `main':
/usr/src/debug/cygwin-3.3.5-1/winsup/cygwin/lib/libcmain.c:37: undefined reference to `WinMain'
collect2: error: ld returned 1 exit status

I have everything properly installed this is the first time I have had this error. I know this is a segmentation fault error. I have a lot of mallocs in my code.

MAIN.C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "image.h"

// open the file, create an ImagePPM, and return the pointer
// return NULL if the file cannot be opened

ImagePPM *readPPM(char *filename)
{
    FILE *inFile = NULL;
    inFile = fopen(filename,"r");
    if (inFile == NULL){
        return NULL;
    }
    
    ImagePPM *image = malloc(sizeof(struct _imagePPM));
    fscanf(inFile, "%*s");
    
    int numRows, numCols, limit = 0;
    fscanf(inFile, "%d %d %d", &numRows , &numCols, &limit);
    image -> pixels = (struct _pixel **)malloc(numRows*sizeof(struct _pixels *));
    for (int i = 0 ;i < numRows; i++){
        image->pixels[i] = (struct _pixel*)malloc(numCols*sizeof(struct _pixel));

    }
    for (int i = 0; i < numRows; i++){
        for (int j = 0; j < numCols; j++){
            int red, green, blue = 0;
            fscanf(inFile, "%d %d %d", &red, &green, &blue);
            (&(image->pixels[i][j]))->red = red;
            (&(image->pixels[i][j]))->green = green;
            (&(image->pixels[i][j]))->blue = blue;
        }
    }

    strcpy(image->magic, "made it here");
    image -> maxVal = limit;
    image -> numRows = numRows;
    image -> numCols = numCols;
    
    fclose(inFile);

return image;
}
// open the file and write the ImagePPM to the file
// return 1 on success
// return 0 if the file cannot be opened

int writePPM(ImagePPM *pImagePPM, char *filename)
{
    FILE *outFile = fopen(filename,"w");
    if (outFile == NULL){
        return 0;
    }

    fprintf(outFile, "%s\n%d\t%d\n%d\n", pImagePPM -> magic, pImagePPM ->numRows, pImagePPM -> numCols , pImagePPM -> maxVal);
    for (int i = 0; i < pImagePPM -> numCols; i++){
        for (int j = 0; j < pImagePPM -> numRows; j++){
            Pixel *PH = &(pImagePPM -> pixels[i][j]);
            fprintf(outFile, "%d %d %d ", PH -> red , PH -> green , PH -> blue);
        }
        fprintf(outFile, "\n");
        fprintf(outFile,"made it here");

    }

    fclose(outFile);


return 1;
}

// free the ImagePPM and its pixels
// everything with a malloc needs a free

void freePPM(ImagePPM *pImagePPM)
{
    int length = pImagePPM -> numCols;
    for(int i = length -1 ; i >= 0; i--){
        free(pImagePPM -> pixels[i]);
    }
    free(pImagePPM);
    return;
}

// open the file, create an ImagePGM, and return the pointer
// return NULL if the file cannot be opened

ImagePGM *readPGM(char *filename)
{

    FILE *inFile = NULL;
    inFile = fopen(filename, "r");
    if (inFile == NULL){
        return NULL;
    }
    ImagePGM *image = malloc(sizeof(struct _imagePGM));
    fscanf(inFile, "%*s");

    int numRows, numCols , limit = 0;
    fscanf(inFile, "%d %d %d", &numRows , &numCols , &limit);

    image -> pixels = (int **)malloc(numRows*sizeof(int*));
    for(int i = 0; i <numRows; i++){
        image -> pixels[i] = (int*)malloc(numCols*sizeof(int));
    }
    for (int i = 0; i < numRows; i++){
        for (int j = 0; j < numCols; j++){
            int total = 0;
            fscanf(inFile, "%d", &total);
            image -> pixels[i][j] = total;
        }
    }

    strcpy (image -> magic, "made it");
    image -> maxVal = limit;
    image -> numRows = numRows;
    image -> numCols = numCols;

    fclose(inFile);

    return image;
}

// open the file and write the ImagePGM to the file
// return 1 on success
// return 0 if the file cannot be opened

int writePGM(ImagePGM *pImagePGM, char *filename)
{
    FILE *outFile = NULL; 
    outFile = fopen(filename, "w");
    if (outFile == NULL){
        return 0;
    }
    fprintf(outFile, "%s\n%d\t%d\n%d\n",
    pImagePGM->magic, pImagePGM->numRows,
    pImagePGM->numCols, pImagePGM->maxVal);

    for (int i = 0 ; i < pImagePGM -> numCols; i++){
        for (int j = 0; j < pImagePGM -> numRows; j++){
            fprintf(outFile, "%d ", pImagePGM -> pixels[i][j]);
        }
        fprintf(outFile, "\n");

    }
    fclose(outFile);
return 1;
}

// free the ImagePGM and its pixels
// everything with a malloc needs a free

void freePGM(ImagePGM *pImagePGM)
{
    int numRows = pImagePGM -> numCols;
        for (int i = numRows -1; i >=0; i ++){
            free(pImagePGM->pixels[i]);
         }
         free(pImagePGM);
    return ; // maybe NULL or zero
}

ImagePGM *convertToPGM(ImagePPM *pImagePPM)
{
    int x ,y , max;
    x = pImagePPM -> numCols;
    y = pImagePPM -> numRows;
    max = pImagePPM -> maxVal;
                                            // space error 
    ImagePGM *image = malloc(sizeof(struct _imagePPM));
    image -> pixels = (int **)malloc(x*sizeof(int*));
    for (int i = 0; i < x; i++){
        image -> pixels[i] = (int*)malloc(y*sizeof(int));
        // error for struct needs to be fixed


    }
    strcpy(image -> magic, "made it here");
    image -> numRows = x;
    image -> numCols = y;
    image -> maxVal = max;

    for (int i = 0; i < x; i++){
        for (int j = 0; j < y; j++){
            int total = 0;
            total += pImagePPM->pixels[i][j].red;
            total += pImagePPM->pixels[i][j].green;
            total += pImagePPM->pixels[i][j].blue;
            total /=3; // converting to grey scale by / itself and 3
            image -> pixels [i][j] = total;

        }
    }
    return image;
}

ImagePGM *shrinkPGM(ImagePGM *pImagePGM)
{
    int oldRow , oldCol, x, y, max;
    oldRow = pImagePGM -> numRows;
    oldCol = pImagePGM -> numCols;
    x = oldRow/2;
    y = oldCol/2;
    max = pImagePGM -> maxVal;
    ///////////
    printf("Made it here");

    ImagePGM *image = malloc(sizeof(struct _imagePPM));
    image -> pixels = (int **)malloc(x*sizeof(int*));
    for (int i = 0; i < x; i++){
        image -> pixels[i] = (int*)malloc(y*sizeof(int));

    }
    
    strcpy(image -> magic, "made it here");
    image -> numRows = x;
    image -> numCols = y;
    image -> maxVal = max; // could be limit instead of max or max val


    for(int i = 0; i < x; i++){
        for(int j = 0; j < y; j++){
            int total = 0;
            total += pImagePGM->pixels[i*2][j*2];
            total += pImagePGM->pixels[i*2][j*2+1];
            total += pImagePGM->pixels[i*2+1][j*2];
            total += pImagePGM->pixels[i*2+1][j*2+1];
            total /= 4;
            image->pixels[i][j] = total;
        }
    }
    return image;
}

IMAGE.H

#ifndef _IMAGE_H_
#define _IMAGE_H_

typedef struct _pixel {
    int red;
    int green;
    int blue;
} Pixel;

typedef struct _imagePPM {
    char magic[3]; // magic P3
    int numRows; // number of rows
    int numCols; // number of columns
    int maxVal; // max value of pixel colors
    Pixel **pixels; // actual pixel data, a 2D array
} ImagePPM;

typedef struct _imagePGM {
    char magic[3]; // magic P2
    int numRows; // number of rows
    int numCols; // number of columns
    int maxVal; // max value of pixel colors
    int **pixels; // actual pixel data, a 2D array
} ImagePGM;

ImagePPM *readPPM(char *filename);
int writePPM(ImagePPM *pImagePPM, char *filename);
void freePPM(ImagePPM *pImagePPM);

ImagePGM *readPGM(char *filename);
int writePGM(ImagePGM *pImagePGM, char *filename);
void freePGM(ImagePGM *pImagePGM);

ImagePGM *convertToPGM(ImagePPM *pImagePPM);
ImagePGM *shrinkPGM(ImagePGM *pImagePGM);

#endif

interface.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "image.h"

int main(int argc, char **argv)
{
    if (argc != 4)
    {
        printf("Usage: %s copy-ppm input.ppm output.ppm\n", argv[0]);
        printf("       %s copy-pgm input.pgm output.pgm\n", argv[0]);
        printf("       %s grayscale input.ppm output.pgm\n", argv[0]);
        printf("       %s shrink input.pgm output.pgm\n", argv[0]);
        return 1;
    }

    char *command = argv[1];
    char *inputFilename = argv[2];
    char *outputFilename = argv[3];

    if (strcmp(command, "copy-ppm") == 0)
    {
        ImagePPM *pImagePPM = readPPM(inputFilename);
        if (pImagePPM == NULL)
        {
            printf("Unable to read the PPM file: %s\n", inputFilename);
            return 2;
        }
        int success = writePPM(pImagePPM, outputFilename);
        if (!success)
        {
            printf("Unable to write to the file: %s\n", outputFilename);
            freePPM(pImagePPM);
            return 3;
        }
        freePPM(pImagePPM);
    }
    else if (strcmp(command, "copy-pgm") == 0)
    {
        ImagePGM *pImagePGM = readPGM(inputFilename);
        if (pImagePGM == NULL)
        {
            printf("Unable to read the PGM file: %s\n", inputFilename);
            return 4;
        }
        int success = writePGM(pImagePGM, outputFilename);
        if (!success)
        {
            printf("Unable to write to the file: %s\n", outputFilename);
            freePGM(pImagePGM);
            return 5;
        }
        freePGM(pImagePGM);
    }
    else if (strcmp(command, "grayscale") == 0)
    {
        ImagePPM *pImagePPM = readPPM(inputFilename);
        if (pImagePPM == NULL)
        {
            printf("Unable to read the PPM file: %s\n", inputFilename);
            return 6;
        }
        ImagePGM *pImagePGM = convertToPGM(pImagePPM);
        int success = writePGM(pImagePGM, outputFilename);
        if (!success)
        {
            printf("Unable to write to the file: %s\n", outputFilename);
            freePPM(pImagePPM);
            freePGM(pImagePGM);
            return 7;
        }
        freePPM(pImagePPM);
        freePGM(pImagePGM);
    }
    else if (strcmp(command, "shrink") == 0)
    {
        ImagePGM *pOrig = readPGM(inputFilename);
        if (pOrig == NULL)
        {
            printf("Unable to read the PGM file: %s\n", inputFilename);
            return 8;
        }
        ImagePGM *pShrink = shrinkPGM(pOrig);
        int success = writePGM(pShrink, outputFilename);
        if (!success)
        {
            printf("Unable to write to the file: %s\n", outputFilename);
            freePGM(pOrig);
            freePGM(pShrink);
            return 9;
        }
        freePGM(pOrig);
        freePGM(pShrink);
    }
    else
    {
       printf("Unrecognized command\n");
       return 10;
    }

    return 0;
}

0

There are 0 answers