C的误差:未定义参考作用,但它被定义 [英] C error: undefined reference to function, but it IS defined

查看:300
本文介绍了C的误差:未定义参考作用,但它被定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是一个简单的程序,但我不断收到此编译器错误。我使用MinGW的编译器。

Just a simple program, but I keep getting this compiler error. I'm using MinGW for the compiler.

下面是头文件, point.h

//type for a Cartesian point
typedef struct {
  double x;
  double y;
} Point;

Point create(double x, double y);
Point midpoint(Point p, Point q);

和这里的 point.c

//This is the implementation of the point type
#include "point.h"

int main() {
  return 0;
}
Point create(double x, double y) {
  Point p;
  p.x = x;
  p.y = y;
  return p;
}

Point midpoint(Point p, Point q) {
  Point mid;
  mid.x = (p.x + q.x) / 2;
  mid.y = (p.y + q.y) / 2;
  return mid;
}

和这里的地方编译器问题进来我不断收到:

And here's where the compiler issue comes in. I keep getting:

testpoint.c:未定义参考创建(双X,双Y)

testpoint.c: undefined reference to 'create(double x, double y)'

虽然在point.c定义

While it is defined in point.c.

这是所谓的单独文件的 testpoint.c

This is a separate file called testpoint.c:

#include "point.h"
#include <assert.h>
#include <stdio.h>
int main() {
  double x = 1;
  double y = 1;
  Point p = create(x, y);

  assert(p.x == 1);
  return 0;
}

我在一个无所适从的问题可能是。

I'm at a loss as to what the issue could be.

推荐答案

你是如何做的编译和链接?你需要指定这两个文件,​​是这样的:

How are you doing the compiling and linking? You'll need to specify both files, something like:

GCC testpoint.c point.c

gcc testpoint.c point.c

...所以它知道到职能从两个连接在一起。随着code,因为它是写现在,但是,你再碰上相反的问题:的多个定义。你需要/想消除一个(当属之一point.c)。

...so that it knows to link the functions from both together. With the code as it's written right now, however, you'll then run into the opposite problem: multiple definitions of main. You'll need/want to eliminate one (undoubtedly the one in point.c).

编辑:在一个较大的程序时,您通常编译和链接分开,以避免重新编译任何东西,也没有改变。你通常需要指定必须通过makefile文件做了什么,并使用制作做的工作。在这种情况下,你有这样的事情:

In a larger program, you typically compile and link separately to avoid re-compiling anything that hasn't changed. You normally specify what needs to be done via a makefile, and use make to do the work. In this case you'd have something like this:

OBJS=testpoint.o point.o

testpoint.exe: $(OBJS)
    gcc $(OJBS)

首先是只为对象文件的名称的宏。你得到的是与 $(OBJS)扩大。第二个是一个规则来告诉make 1)可执行取决于物体的文件,和2)告诉它如何创建可执行文件时/如果它是过时的相比,对象文件。

The first is just a macro for the names of the object files. You get is expanded with $(OBJS). The second is a rule to tell make 1) that the executable depends on the object files, and 2) telling it how to create the executable when/if it's out of date compared to an object file.

请大多数版本(包括一个在MinGW的我pretty知道)有一个内置的隐含规则来告诉他们如何创建一个从C源文件的目标文件。它通常看起来大致是这样的:

Most versions of make (including the one in MinGW I'm pretty sure) have a built-in "implicit rule" to tell them how to create an object file from a C source file. It normally looks roughly like this:

.c.o:
    $(CC) -c $(CFLAGS) $<

这假定C编译器的名称是在宏命名为CC(如 CC = GCC 隐含定义),并允许您指定一个关心的任何标志宏名为 CFLAGS (例如, CFLAGS = -O3 打开优化)和 $&LT ; 是一个特殊的宏,扩展到源文件的名称

This assumes the name of the C compiler is in a macro named CC (implicitly defined like CC=gcc) and allows you to specify any flags you care about in a macro named CFLAGS (e.g., CFLAGS=-O3 to turn on optimization) and $< is a special macro that expands to the name of the source file.

您通常这些信息存储在一个名为的Makefile 文件,并建立你的程序,你只需要输入制作在命令行。它含蓄地查找名为的Makefile 文件,并运行的任何规则包含。

You typically store this in a file named Makefile, and to build your program, you just type make at the command line. It implicitly looks for a file named Makefile, and runs whatever rules it contains.

这样做的很好的一点是,制作自动查找在文件上的时间戳,所以它只会重新编译自上次已更改的文件,你编译它们(即文件,其中的.C文件中有一个较新的时间戳比匹配的.o文件)。

The good point of this is that make automatically looks at the timestamps on the files, so it will only re-compile the files that have changed since the last time you compiled them (i.e., files where the ".c" file has a more recent time-stamp than the matching ".o" file).

另外要注意:1)有很多的变化中如何使用让当涉及到大型项目,以及2)也有很多替代品进行的。我只打在这里高点最低限度。

Also note that 1) there are lots of variations in how to use make when it comes to large projects, and 2) there are also lots of alternatives to make. I've only hit on the bare minimum of high points here.

这篇关于C的误差:未定义参考作用,但它被定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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