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:
input.h
#ifndef INPUT_H
#define INPUT_H
void getInput(char *str, ...);
#endif
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("%s\n", s);
}
Edit - I fixed it by moving the files to the following places
libinput.a = D:\mingw64\lib\gcc\x86_64-w64-mingw32\8.1.0
input.c and input.h = D:\mingw64\x86_64-w64-mingw32\include
CodePudding user response:
You tell the linker to search other directories with -L searchdir
. gcc will pass that option through to the linker.