gcc 4.2.1链接问题:架构x86_64的未定义符号 [英] gcc 4.2.1 Linking issue: Undefined Symbols for Architecture x86_64

查看:147
本文介绍了gcc 4.2.1链接问题:架构x86_64的未定义符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是的,之前有人问过,但是我在SO和其他地方提出的每个答案都与编译g ++而不是g ++的C ++代码有关,或者与标准库有关的问题。到目前为止,这里没有任何内容适合我。



所以,我正在经历一个关于C的基本速成课程,并试图编译一个用于说明的例子链接到您创建的文件,而不是来自标准库。以下是代码:

math_functions.h

  int sum(int x,int y); 
float average(float x,float y,float z);

math_functions.c

  int sum(int x,int y)
{
return(x + y);


float float(float x,float y,float z)
{
return(x + y + z)/ 3;
}

最后

test3.c

  #include< stdio.h> 
#includemath_functions.h

main()
{
int theSum = sum(8,12);
float theAverage = average(16.9,7.86,3.4);

printf(总数是:%i,theSum);
printf(平均值为:%f \ n,theAverage);
printf(对一个int的平均值是:%i \ n,(int)theAverage);
}

这些都在同一个文件夹中。当我打开终端时,cd到文件夹并运行:

  gcc test3.c -o test3 

code>

我得到:

 架构x86_64的未定义符号:
_average,引用自:
_main in ccmf69Tt.o
_sum,引用自:
_main in ccxms0fF.o
ld:找不到架构x86_64的符号
collect2:ld返回1退出状态

如果我使用gcc或g ++(这是我不应该做的,因为这应该都是C),并且之前编译的所有内容现在可以正常使用

  #include< stdio.h> 

位于顶部。

我可以通过删除

  #includemath_functions.h
test3.c 文件的

,将 math_functions.h 的内容放在main ()和main()之后的 math_functions.c 的内容。是的,这是复制和粘贴,所以它的代码是完全相同的。



所以是的,我可以让我的代码工作,但它破坏了练习的目的,因为我最终不会在任何其他C文件中使用该代码而无需复制它并将其粘贴到一个文件中。



所以我我想知道,有没有一种方法可以解决这个问题,所以我可以包含更多仅仅是标准的C,C ++和Objective-C库?



这是通过框终端,手动输入gcc命令,并通过CodeRunner,它有标准的命令都藏在一个按钮中,所以我不能塞满。



我使用Xcode 4.6.2中的命令行工具(几个小时前安装了它们),我没有实际做过很多工作,然后标准使用Mini Mini,直到2012年的Mac Mini上运行Mountain Lion 10.8.4(12E55)现在)



我的MacBook Air上安装了所有相同的软件,但没有测试过它是否相同还没有完成。



任何指针?如果其他人有这样的工作,并在这里的某个地方工作,请给我指出,我一直在寻找一个小时,但就像我之前说过的,到目前为止我发现的所有解决方案最终都是在那里是C ++代码还是标准库中的奇怪之处。 你只需要编译maths_function。链接器抱怨它没有包含在该模块中的定义。

  gcc test3.c math_functions.c -o test3 


Yes, it's been asked before, but every answer I come up with on SO and elsewhere has to do with compiling C++ code in gcc instead of g++, or a issue of some kind with standard libraries. So far, nothing actually lining up right for me here.

So, I'm going through a basic crash course on C, and trying to compile an example used to illustrate linking to files that you create, rather than from the standard libraries. Here's the code:

math_functions.h

int   sum       (int x, int y);
float average   (float x, float y, float z);

math_functions.c

int sum (int x, int y)
{
  return (x + y);
}

float average (float x, float y, float z)
{
  return (x + y + z) / 3;
}

and finally

test3.c

#include <stdio.h>
#include "math_functions.h"

main ()
{
  int   theSum     = sum (8, 12);
  float theAverage = average (16.9, 7.86, 3.4);

  printf ("the sum is: %i ", theSum);
  printf ("and the average is: %f \n", theAverage);
  printf ("average casted to an int is: %i \n", (int)theAverage);
}

These are all in the same folder. When I open the Terminal, cd to the folder and run:

gcc test3.c -o test3

I get:

Undefined symbols for architecture x86_64:
  "_average", referenced from:
    _main in ccmf69Tt.o
  "_sum", referenced from:
    _main in ccxms0fF.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status

This happens if I use gcc or g++ (which I shouldn't have to do, since this should all be C), and everything I've compiled before now works fine with the

#include <stdio.h>

at the top.

I can get it to compile and run fine by removing the

#include "math_functions.h"

from the test3.c file, putting the contents of math_functions.h before main() and the contents of math_functions.c after main(). And yes, that is copy and paste, so its definitely the same code.

So yeah, I can get my code to work, but it defeats the purpose of the exercise, in that I don't end up being able to use that code in any other C files without copying it and pasting it into the one file...

So I'm wondering, is there a way I can fix this, so I can include more that just the standard C, C++ and Objective-C libraries?

This happens through box the Terminal, manually typing out the gcc command, and through CodeRunner, which has the standard commands all tucked away into a button, so I can't stuff it.

I'm running Mountain Lion 10.8.4 (12E55) on a 2012 Mac Mini, using the Command Line Tools from Xcode 4.6.2 (installed them just a few hours ago, I haven't actually done much then standard use of the Mini till now)

I have all the same software installed on my MacBook Air, but haven't tested it to see if the same goes down yet.

Any pointers? If someone else has had this and worked it out somewhere here on SO, please point me at it, I have been looking for round an hour but like I said before, all the solutions that I've found so far end up being when there is C++ code or something weird with the standard libraries.

解决方案

You just need to compile the maths_function. The linker is complaining it does not have the definitions contained in that module.

gcc test3.c math_functions.c -o test3

这篇关于gcc 4.2.1链接问题:架构x86_64的未定义符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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