1

我只是在玩代码块并编写一个非常简单的程序,但我得到了未定义的引用错误......我将完全相同的代码复制到其他地方并在终端中编译它并且工作正常,但就是不起作用在代码块中。

我的 main.c 是:

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

int main()
{
    printhello();
    return 0;
}

然后我的 subfunc.c 是:

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

void printhello(void)
{
    printf("hello world in function\n");
}

然后我的 subfunc.h 是:

void printhello(void);

谢谢

更新

其实..我想通了。我右键单击文件 subfunc.c 和 subfunc.h 并选择属性并转到构建选项卡,我发现编译文件和链接文件的选项没有勾选。谢谢

4

2 回答 2

2

在此处输入图像描述
请参阅上图并与您的项目进行比较。仅与红色圆圈进行比较。忽略剩余的(剩余的都是我的项目/作业)在 my_subfunction.h 中写

    #ifndef __MY_SUBFUNC_H__
    #define __MY_SUBFUNC_H__


    #include<stdio.h>
    void printhello(void);
    #endif

在 main.c

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

int main()
{
    printhello();
    return 0;
}

和 my_subfun.c:-

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

void printhello(void)
{
    printf("hello world in function\n");
}

我希望这会有所帮助。因为这对我有用。

于 2012-04-16T05:52:13.533 回答
1

You didn't state exactly what the undefined error was but I'm betting it has something to do with either your main() or printhello() function.

Make sure you're linking both object files main.obj and subfunc.obj in your project after compiling your main.c and subfunc.c. This should automatically happen in codeblocks assuming you included both main.c and subfunc.c for the current project you're building.

于 2012-04-16T06:15:05.473 回答