skip to Main Content

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 = %dn", 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.

2

Answers


  1. Chosen as BEST ANSWER

    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.


  2. So there are 2 main categories of errors when working with compiled languages.

    • Linker Errors
    • Source Errors

    Source errors are found when there is invalid syntax inside a specific file.

    Linker errors are found when there is invalid links inside a specific file.

    The Linker here has reported that it cannot find the specific functions. That is because you did not provide the information to the compiler which in turn, did not provide that information to the linker.

    Depending on your specific compiler, you will find some flags which allow you to specify which source files (.c / .cpp) to include in your program. There are usually wildcard specifiers also (*) which allows you to include all source in a given folder.

    When developing compiled software, there is another tool which adds further confusion. The IDE. The IDE is simply a tool used to write code with various tricks to make writing code easier. The IDE is NOT a compiler nor a linker. It just knows where to find them in different circumstances. VSCode is great at finding your compiler.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search