1

When I learned the C language singly linked list, I encountered this error message "Linker tool error LNK2019", so I thought why this is caused? At first I thought it was a global variable that was not defined by me, but in my test, only my singly linked list code can reproduce the error, and it can’t even generate an exe. Therefore, I want to ask why this occurs. This question (is it related to the compilation process of vs2019)

My questions:I was deliberately letting the function named "fake" not be defined. I wanted to reproduce this error, so I didn’t define the function in other codes, but they reported different error messages directly. What I’m curious about is that vs. When compiling the source code into an obj file, what happened to make it appear this error.

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

typedef struct Link {
    int elem;
    struct Link* next;
}link;

link* initLink();

void fake(link* p);

int main() {
    printf("test:\n");
    link* p = initLink();
    fake(p);
    return 0;
}

link* initLink() {
    link* p = NULL;
    link* temp = (link*)malloc(sizeof(link));
    temp->elem = 1;
    temp->next = NULL;
    p = temp;  
    for (int i = 2; i < 5; i++) {
        link* a = (link*)malloc(sizeof(link));
        a->elem = i; 
        a->next = NULL;
        temp->next = a;
        temp = temp->next;


    }
    return p;
}

At this time it did not appear an error like LINK2019:

#include <stdio.h> 
void display(); 
int main() 
{ printf("test:\n"); 
int* p = NULL; 
display(p); //At this time it did not appear an error like LINK2019 
return 0; }

Instead, this error message appears" Error C2660 "display": Function does not accept 1 parameter"

  • You are missing `fake()` definition – qrdl Dec 03 '21 at 07:19
  • You are calling a function 'fake', but it's not defined in your code. This is probably the problem, although I'd expect a warning message from the linker about an undefined symbol. – Paul Hankin Dec 03 '21 at 07:19
  • I was deliberately letting the function named "fake" not be defined. I wanted to reproduce this error, so I didn’t define the function in other codes, but they reported different error messages directly. What I’m curious about is that vs. When compiling the source code into an obj file, what happened to make it appear this error. – Tonia James Dec 03 '21 at 08:13
  • I'm sorry I didn't make the question clear. – Tonia James Dec 03 '21 at 08:18
  • @ToniaJames what were the other errors? When you use a function you declared, but don't define then linking will fail. When you don't even declare a function and try to use it, then compiling will fail, because it doesn't even know the name. – PeterT Dec 03 '21 at 08:19
  • #include void display(); int main() { printf("test:\n"); int* p = NULL; display(p); //At this time it did not appear an error like LINK2019 return 0; } @PeterT – Tonia James Dec 03 '21 at 08:38
  • @ToniaJames you should edit that code into your question – PeterT Dec 03 '21 at 11:16
  • @ToniaJames I can not reproduce that. It still shows `error LNK2019: unresolved external symbol _display referenced in function _main` for me when I try to compile and link it. Are you trying to link it into an exe? If you just compile this into for example a static library, then it won't force resolving of all symbols. – PeterT Dec 03 '21 at 11:24
  • Sorry, my test code and error message have been re-edited in the question@PeterT – Tonia James Dec 03 '21 at 11:58
  • 1
    @ToniaJames oh, are you compiling this in C mode? or as a *.cpp file? For C++ the function signature needs to have the correct arguments like `void display(int*);`. Because C++ supports function overloading you can't use `void display();` when you want to describe a function that takes arguments. If you compile it as a *.c file then newer versions of msvc should display the same link error for both code examples you posted. – PeterT Dec 03 '21 at 12:37
  • Thank you for your answers! That means a lot to me!For starters, do you have any good GitHub project source code that can encourage me to join small development?PeterT answers wers top notch@PeterT – Tonia James Dec 03 '21 at 13:15

0 Answers0