简体   繁体   English

如何在 Windows 上的 gcc 中链接 cs50 C 库

[英]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.我是 С 编程的新手,一直在尝试使用 MinGW/GCC 编译我的代码,但我尝试包含 cs50 (cs50.c, cs50.h) 库,但编译器找不到它。 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:我试图给出这样的命令: gcc -LC:\\Users\\apple\\Desktop -lcs50 mario.c但结果是这样的:

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).总是在.c文件之后传递库,否则它们实际上不会做任何事情(除非main在库中)。

  2. You appear to have a library called cs50.a;您似乎有一个名为 cs50.a 的库; -lcs50 wants to find a file called libcs50.a or libcs50.so . -lcs50 想要找到一个名为libcs50.alibcs50.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:解决此问题的最简单方法是不要使用-L-l ,只需将您的库直接传递给gcc如下所示:

gcc mario.c cs50.a

Since cs50.c is a single file, you do not need a library at all.由于cs50.c是单个文件,因此您根本不需要库。 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 因此,请尝试启动程序: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) (string.c,cs50.c和cs50.h在同一目录中)

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 ;我所做的是将 cs50.h 和 cs50.c 文件放在与 stdio.h 相同的文件夹或目录中; which you will find in the program files of the compiler you're using.您可以在您使用的编译器的程序文件中找到它。 It worked for me.它对我有用。 Hope this helps.希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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