如何在Windows的GCC中链接CS50 C库 [英] How to link the cs50 C library in gcc on windows

查看:237
本文介绍了如何在Windows的GCC中链接CS50 C库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是С编程的新手,一直在尝试使用MinGW / GCC编译我的代码,但是我尝试包含cs50(cs50.c,cs50.h)库,但编译器找不到它。帮助我编译谁知道这是怎么回事。

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.

我试图给出这样的命令: gcc -LC:\Users\apple\Desktop -lcs50 mario.c
但是结果是这样的:

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

或:

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

这是我的代码:

#include <stdio.h>
#include <cs50.h>

int main()
{
    int num = GetInt();
    printf("%d\n",num);
}


推荐答案

gcc -LC:\Users\apple\Desktop -lcs50 mario.c

这里有两个问题。


  1. 总是在 .c 之后传递库文件,否则它们实际上不会做任何事情(除非 main 在库中)。

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

您似乎有一个名为cs50.a的库; -lcs50想要找到一个名为 libcs​​50.a libcs​​50.so 的文件。

You appear to have a library called cs50.a; -lcs50 wants to find a file called libcs50.a or libcs50.so.

解决此问题的最简单方法是不打扰 -L -l <​​/ code>,然后将您的库直接传递给 gcc ,例如:

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

这篇关于如何在Windows的GCC中链接CS50 C库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆