在Mac OSX狮子编译时的建筑x86_64的未定义符号 - Ç [英] C - Undefined symbols for architecture x86_64 when compiling on Mac OSX Lion
问题描述
我得到的编译在Mac OSX狮子一个非常非常简单的name.c文件中的一些问题。
I'm getting some problems on compiling a very very simple name.c file on Mac OSX Lion.
现在,我开始下面就cs50.net哈佛CS50课程。我不完全新的节目,但我是如何课程已教好奇。
Now, I started following Harvard CS50 course on cs50.net. I'm not totally new to programming but I was curious on how this course has been taught.
这是name.c来源:
This is the source of name.c:
#include <stdio.h>
#include <cs50.h>
int
main(void)
{
printf("State your name:\n");
string name = GetString();
printf("O hai, %s!\n", name);
return 0;
}
正如你所看到的,它需要这个库: https://manual.cs50.net/CS50_Library
现在,当我编译它,出现这种情况:
Now, when I compile it, this happens:
Undefined symbols for architecture x86_64:
"_GetString", referenced from:
_main in name-vAxcar.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [name] Error 1
如果我用我的源文件中相同的GetString()函数cs50.c,它完美的作品:
If I use the same GetString() cs50.c function inside my source file, it works perfectly:
#include <stdio.h>
#include <string.h>
#include <float.h>
#include <limits.h>
#include <stdbool.h>
#include <stdlib.h>
typedef char *string;
string GetString(void);
int
main(void)
{
printf("State your name:\n");
string name = GetString();
printf("O hai, %s!\n", name);
}
string
GetString(void)
{
// CODE
}
为什么会出现这种情况?
我安装的是图书馆,因为它说上面的链接;我查了一下两者cs50.h和libcs50.a分别在/ usr /本地/包括和/ usr / local / lib目录。
Why does this happen? I installed the library as it says on the link above; I checked and both cs50.h and libcs50.a are respectively in /usr/local/include and /usr/local/lib.
感谢您提前对您有所帮助。
Thank you in advance for your help.
推荐答案
您遇到的问题是在链接阶段,没有编制。你没有提供的GetString
,只是其宣言的执行(通过 .H
文件,你的#include
)。
The problem you encounter is in the linking stage, not compiling. You did not provide the implementation of GetString
, only its declaration (through the .h
file you #include
).
要提供实现本身,通常需要反对,其中包括它的库链接;这通常是由 -l </ code>标志做了
G ++
。例如,
To provide the implementation itself, you usually need to link against the library which includes it; this is usually done by the -l
flag to g++
. For example,
g++ file.cpp -lcs50
你的第二个样品code做链接,因为你手动(并明确)为的GetString
的实施,虽然空单。
这篇关于在Mac OSX狮子编译时的建筑x86_64的未定义符号 - Ç的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!