简体   繁体   中英

How to link the cs50 C library in gcc on windows

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);
}
gcc -LC:\Users\apple\Desktop -lcs50 mario.c

There are two problems here.

  1. Always pass libraries after .c files or they won't actually do anything (unless main is in the library).

  2. You appear to have a library called cs50.a; -lcs50 wants to find a file called libcs50.a or libcs50.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

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).

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...

I had the same problem. What i did was that i put the cs50.h and cs50.c files in the same folder or directory as stdio.h ; which you will find in the program files of the compiler you're using. It worked for me. Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM