问题
I'm new to С programming and have been trying to compile my code using MinGW/GCC, but I try to include cs50 (cs50.c, cs50.h) library, and the compiler can't find it. Help me compile who knows what's going on.
I tried to give such command: gcc -LC:\Users\apple\Desktop -lcs50 mario.c
But the result is this:
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: cannot find -lcs50
collect2.exe: error: ld returned 1 exit status
Or:
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\apple\AppData\Local\Temp\cc8KpeUr.o:mario.c:(.text+0x33): undefined reference to `GetInt'
collect2.exe: error: ld returned 1 exit status
This is my code:
#include <stdio.h>
#include <cs50.h>
int main()
{
int num = GetInt();
printf("%d\n",num);
}
回答1:
gcc -LC:\Users\apple\Desktop -lcs50 mario.c
There are two problems here.
Always pass libraries after
.c
files or they won't actually do anything (unlessmain
is in the library).You appear to have a library called cs50.a; -lcs50 wants to find a file called
libcs50.a
orlibcs50.so
.
The easiest way around this problem is to not bother with -L
or -l
and just pass your library directly to gcc
like this:
gcc mario.c cs50.a
回答2:
Since cs50.c
is a single file, you do not need a library at all. You can compile it as needed to save a few steps, it will consume a couple milliseconds more but most of the time you would not notice.
Just use
gcc mario.c cs50.c
and it will work (provided that both files are in the current folder).
回答3:
running in the same problem now - but the solutions described still not work in my case -
This is my code:
#include <cs50.h>
#include <stdio.h>
int main(void)
{
string name = <get_string("What is your name?\n");
printf("hello, %s\n" , name);
}
So is try to start the program: gcc string.c cs50.c
And this is the error i get:
string.c:1:10: fatal error: cs50.h: No such file or directory
#include <cs50.h>
(string.c, cs50.c and cs50.h are in the same directory)
Thanks for your help in advance...
来源:https://stackoverflow.com/questions/57432721/how-to-link-the-cs50-c-library-in-gcc-on-windows