【发布时间】:2017-01-09 18:16:57
【问题描述】:
我收到以下错误
cc string.c -o string
string.c:7:16: warning: implicit declaration of function 'get_string' is invalid in C99
[-Wimplicit-function-declaration]
string name = get_string();
^
string.c:7:9: warning: incompatible integer to pointer conversion initializing 'string' (aka 'char *')
with an expression of type 'int' [-Wint-conversion]
string name = get_string();
^ ~~~~~~~~~~~~
2 warnings generated.
ld: can't write output file: string for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [string] Error 1
当我尝试时
make string
string.c的代码:
1 #include <stdio.h>
2 #include <cs50.h>
3
4 int main(void)
5 {
6 printf("Name: ");
7 string name = get_string();
8 printf("hello, %s\n", name);
9
10 }
代码与第一周的课程相同。
请记住,我已经下载了 cs50.h 并按照 OSX 系统的 CS50 库手册中的说明进行了安装。
【问题讨论】:
-
这是一个不同的版本,因为我刚刚在
CS_50.h引用中找到了string get_string(void);(名称与cs50.h略有不同)。错误的原因是因为在代码之前引用它的某处,您需要使用函数原型或函数定义(实现)声明get_string()是什么。当不存在时,编译器过去常常对函数做出隐含的假设,但他们不再这样做了。 -
需要在头部声明函数。也许声明和定义已经存在并且将被链接但编译器不知道这一点。添加字符串 get_string();到标题并查看链接器是否正确链接它以及它是否在没有定义的情况下执行。
-
对不起,如果你很傻,你的意思是我必须编辑位于 /usr/local/include 的 CS50.h 吗? (我认为安装把它放在那里)
-
编辑第三方库标题不太可能是正确的做法。通读头文件,看看是否能找到函数或类似函数,这样你就知道要使用什么函数了。
-
属于 cs50.stackexchange.com