How to link the cs50 C library in gcc on windows

烂漫一生 提交于 2020-01-25 07:03:25

问题


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.

  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



回答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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!