Search code examples
cvisual-studio-codepointersheader-files

Problem with C header file: Undefined Reference Error


I am currently working on a C programming project and have encountered an issue that I'm struggling to resolve.

The problem:

I am using Visual Studio Code (VS Code) as my code editor. The project consists of the following files:

main.c:

#include "list.h"
#include <stdio.h>

int main()
{
    List* l = lst_create();
    l = lst_inset(l,10);
    l = lst_inset(l,20);
    l = lst_inset(l,25);
    l = lst_inset(l,30);
    //l = lst_rem(l,10); 
    lst_print(l);

    return 0;
}

List.c:

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

struct list
{
    int info;
    List *next;
};

List* lst_create()
{
    return NULL;
}

int lst_empty(List *l)
{
    return (l == NULL);
}

List* lst_inset(List *l, int info)
{
    List* lAux = (List*) malloc(sizeof(List));

    lAux -> info = info;
    lAux -> next = l;

    return lAux;
}

List* lst_search(List *l, int info)
{
    List* lAux = l;

    while (lAux != NULL)
    {
        if (lAux -> info == info)
        {
            return lAux;
        }

        lAux = lAux -> next;
    }

    return NULL;
}

void lst_print(List *l)
{
    List* lAux = l;

    while (lAux != NULL)
    {
        printf("Info = %d\n", lAux -> info);
        lAux = lAux -> next;
    }
}

list.h:

#ifndef LIST_H
#define LIST_H

typedef struct list List;

List* lst_create();

int lst_empty(List *l);

List* lst_inset(List *l, int info);

//List* lst_remove(List *l, int info);

//void lst_free(List *l);

void lst_print(List *l);

List* lst_search(List *l, int info);

#endif

I have included "list.h" in "main.c" to use the functions defined in list.c. However, when I attempt to compile the project, I encounter the following linker error:

/usr/bin/ld: /tmp/ccCzNKTb.o: in function 'main':
/home/user/Codes/C/TestProject/src/main.c:7: undefined reference to 'lst_create'
/home/user/Codes/C/TestProject/src/main.c:8: undefined reference to 'lst_insert'
/home/user/Codes/C/TestProject/src/main.c:9: undefined reference to 'lst_insert'
/home/user/Codes/C/TestProject/src/main.c:10: undefined reference to 'lst_insert'
/home/user/Codes/C/TestProject/src/main.c:11: undefined reference to 'lst_insert'
/home/user/Codes/C/TestProject/src/main.c:13: undefined reference to 'lst_print'
collect2: error: ld returned 1 exit status

I have ensured that both main.c and list.c are present in the specified directory. The functions lst_create, lst_insert, and lst_print are declared in list.h and defined in list.c. I have also tested the code in two different C IDEs, but encountered the same "undefined reference" error.I have validated my code by referring to instructional YouTube videos and cross-referenced it with example code. Despite my best efforts, I have not been able to resolve this issue. I even consulted my programming professor, who was unable to identify the problem.


Solution

  • After spending the entire afternoon thinking about the problem, I decided to start over from scratch. I chose one of the IDEs that I had previously tested, created a simple test program, and, to my surprise, it worked perfectly. I began copying my code line by line, testing functions as I went along, and everything worked like a charm.

    I came to the conclusion that the problem was vscode.

    The strange part was that, previously, I had tried to copy and paste the code using Ctrl+C and Ctrl+V, but it hadn't worked. Even when I tried copying and pasting again, it failed. For reasons beyond my mortal understanding, the copy-paste operation seemed to be the issue. Nevertheless, the code is now working perfectly.

    I'd like to express my gratitude to everyone who responded and tried to help with the problem.

    Now my plan is to find a way for VS Code to seamlessly compile all the files together. Compiling via the terminal using gcc -o program *.c appears to be a viable approach, so i intend to explore the possibility of creating a tasks.json file or modifying the settings of the C/C++ Compile Run extension that I had been using to simplify the process.

    EDIT: I love and hate how things just decide to work out. Now vs code decided that it would compile correctly. I didn't change anything, I didn't update anything, I didn't stop using the extension I was using, it simply worked