[SOLVED] Using library gives unknown reference error


This Content is from Stack Overflow. Question asked by work work

I am currently creating a C library to accept input(similar to scanf), but after moving all the necessary files to the relevant places, this error is seen when I type this command (gcc main.c -o main.exe -linput)

undefined reference to `getInput
collect2.exe: error: ld returned 1 exit status

I have moved the C file and the header file to the GCC include folder, and I have moved libinput.a to the lib folder.
I have also done the same thing with another library I made, but that works fine.

After some further digging I found out that when I right-click on the function “getinput” and click “go to definition”, it goes to the input.c file instead of the input.h. In the other libraries, it redirects me to the library.h file and not the library.c file. This might be the problem but I have no idea of how to fix it.

Note- running gcc main.c libinput.a works while having libinput.a in the same directory, but I
would prefer gcc main.c -o main.exe -linput(without having it in the same directory,
similar to how other libraries work). Having input.c and input.h in the same directory
and then linking it also works.
Environment – VS code on windows 10

Here is my code so far:

#ifndef INPUT_H
#define INPUT_H

void getInput(char *str, ...);


input.c –> https://pastebin.com/BUTgXHBG (this is horribly unoptimized btw)

main.c(an example code where I am using the library)

#include <stdio.h>
#include <input.h>

int main()
    char *s = NULL;
    getInput("{s}", &s);
    printf("%sn", s);


You tell the linker to search other directories with -L searchdir. gcc will pass that option through to the linker.

This Question was asked in StackOverflow by work work and Answered by Allan Wind It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?