0

Altough I save my project, I get these errors

When I run parametric_stack_arr.c I got this error

D:/Qt/Tools/mingw810_64/bin/../lib/gcc/x86_64- 
w64-mingw32/8.1.0/../../../../x86_64-w64- mingw32/lib/../lib/libmingw32.a(lib64_libmingw32_a 
-crt0_c.o):crt0_c.c:(.text.startup+0x2e): 
undefined reference to `WinMain'
collect2.exe: error: ld returned 1 exit status

When I run main.c I got this error

C:\Users\xxx\AppData\Local\Temp\ccq0uYEM.o:main.c:(.text+0x6b): undefined reference to `push'
C:\Users\xxx\AppData\Local\Temp\ccq0uYEM.o:main.c:(.text+0x81): undefined reference to `print'
C:\Users\xxx\AppData\Local\Temp\ccq0uYEM.o:main.c:(.text+0x96): undefined reference to `pop'
C:\Users\xxx\AppData\Local\Temp\ccq0uYEM.o:main.c:(.text+0xa4): undefined reference to `push'
C:\Users\xxx\AppData\Local\Temp\ccq0uYEM.o:main.c:(.text+0xba): undefined reference to `print'
collect2.exe: error: ld returned 1 exit status

This is my code

I import parametric_stack_arr.h to main.c

main.c:

#include <stdlib.h>
#include <stdio.h>
#include "parametric_stack_arr.h"

int main(int argc, char *argv[])
{

    printf("hello");
    stack s1;
    stack s2;  
    s1.size = 2;
    s1.arr = NULL;
    s1.top =0;

    s2.size = 2;
    s2.arr = NULL;
    s2.top =0;
    
    int i;
    for(i=1; i<=5; i++)
    {   
        push(i*10, &s1);
    }

    print(&s1);

    int j;
    for(j=1; j<=10; j++)
    {   
        push(pop(&s1), &s2);
    }
   
    print(&s2);


    return 0;
}

I import parametric_stack_arr.h to parametric_stack_arr.c

parametric_stack_arr.c:

#include <stdlib.h>
#include <stdio.h>
#include "parametric_stack_arr.h"




int pop(stack *s)
{
    if(s->arr = NULL){
        printf("error");
        return -1;
    }

    if(s->top <= s->size/4)
    {
        int *arr2 = (int *)malloc(sizeof(int)*(s->size)/2);
        int j;
        for(j =0; j<(s->size); j++)
        {
            arr2[j]=s->arr[j];
        }
        free(s->arr);
        s->arr = arr2;
        s->size /= 2;
    }
    return s->arr[--s->top];
}

void push(int x, stack *s)
{
    if(s->arr = NULL)
    {
        s->arr = (int*) malloc(sizeof(int)*2);
    }
    if(s->top >= s->size)
    {
        int *arr2 = (int *)malloc(sizeof(int)*(s->size)*2);
        int j;
        for(j =0; j<s->size; j++)
        {
            arr2[j]=s->arr[j];
        }
        free(s->arr);
        s->arr = arr2;
        s->size *= 2;
    }
    s->arr[s->top++] = x;
}

void print(stack *s)
{
    printf("\nsize : %d\n", s->size);
    int i;
    for(i=0; i<s->top;i++)
    {
        printf("%d ", s->arr[i]);
    }
}

I add my function prototypes to parametric_stack_arr.h

#ifndef __PARAMETRIC_STACK_ARR_H
#define __PARAMETRIC_STACK_ARR_H

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



typedef struct
{
    int size;
    int top;
    int *arr;

}stack;

int pop(stack *s);
void push(int x, stack *s);
void print(stack *s);

#endif
baseroglu
  • 11
  • 3
  • Where is `parametric_stack_arr.c` and how is this compiled? Have you already read this: https://stackoverflow.com/questions/5559250/c-error-undefined-reference-to-function-but-it-is-defined ? – Bob__ May 12 '22 at 07:05
  • all files are in the same folder@Bob__ – baseroglu May 12 '22 at 07:08
  • I applied this but I still get these errors – baseroglu May 12 '22 at 07:09
  • @baseroglu, can you paste the content of `parametric_stack_arr.c`? – kuro May 12 '22 at 07:17
  • 2
    How do you build your program? Please show complete command line. If you just use VSCode as is, there will probably not multiple sources compiled and linked together. – Gerhardh May 12 '22 at 07:19
  • 1
    Please show the exact full error messages – Weather Vane May 12 '22 at 07:20
  • A) show your actual error. B) show how you build your code. This looks like a linking error. – JHBonarius May 12 '22 at 07:22
  • OTOH `WinMain` is required if you use certain Windows API functions, which you haven't. So perhaps the project is not set up properly. – Weather Vane May 12 '22 at 07:25
  • https://stackoverflow.com/a/22160071/775806 https://stackoverflow.com/questions/12573816 – n. m. could be an AI May 12 '22 at 07:26
  • @JHBonarius I added my error messages – baseroglu May 12 '22 at 07:31
  • 1
    *When I run parametric_stack_arr.c* This C file does not contain a valid program. You cannot build and run it. You must compiler and link both files together. You will need to adjust the json files in VSCode to produce a binary holding both files. And as it was asked already: **HOW** do you try to run/compile your code? – Gerhardh May 12 '22 at 07:32
  • 1
    You are getting linker errors, when you try to make an executable from each of the source files individually. – Weather Vane May 12 '22 at 07:35
  • Okey , How can I adjust the json files? @Gerhardh – baseroglu May 12 '22 at 07:36
  • If the programmer is required to manually change some JSON then that's the opposite of what an IDE is supposed to be doing - the main reason why use IDEs is so that we don't have to mess around with make files manually. If they replaced the make file with some JSON, then that's taking one step forward and two steps back... – Lundin May 12 '22 at 08:00
  • @Lundin they don't consider it an IDE [themselves](https://code.visualstudio.com/Docs/supporting/FAQ#_what-is-the-difference-between-visual-studio-code-and-visual-studio-ide). – Bob__ May 12 '22 at 08:44
  • @Bob__ Ok in that case it seems like a pretty pointless product... beginners should use a proper IDE. – Lundin May 12 '22 at 08:46

0 Answers0