简体   繁体   English

为什么CC在标头中看不到我的函数定义?

[英]Why does CC not see my function definition in header?

I'm writing a simple application in ANSI C. I am using GCC in a Unix environment. 我正在ANSI C中编写一个简单的应用程序。我在Unix环境中使用GCC。

I have the following sample application: 我有以下示例应用程序:

    //main.c

#include "foo.h"

int main()
{

int result;

result = add(1,5);

return0;
}

Header: 标头:

  //foo.h
    #ifndef FOO_H_INCLUDED
    #define FF_H_INCLUDED

    int add(int a, int b);

    #endif

Implementation: 实现方式:

//foo.c

int add(int a, int b)
{
return a+b;
}

I am compiling my program with the following command: 我正在使用以下命令编译程序:

 cc main.c -o main.o

The compiler complains that 'reference to add is undefined'. 编译器抱怨“ add引用未定义”。 Is this a linking problem? 这是链接问题吗? How do properly make use of my header? 如何正确利用我的标题?

Thanks! 谢谢!

You need to compile both your source files together: 您需要将两个源文件一起编译:

cc main.c foo.c -o main

Also, in this case, -o produces an executable, so calling it main.o can be misleading. 同样,在这种情况下, -o生成可执行文件,因此将其main.o可能会误导您。


Yet another tidbit, though unrelated to the question: the #ifndef and #define in foo.h don't match. 还有另一个花絮,尽管与问题无关: foo.h#ifndef#define不匹配。

The header is not your current problem. 标头不是您当前的问题。 Your current problem is that you're not compiling the add function definition in foo.c . 当前的问题是您没有在foo.c编译add函数定义。

Try 尝试

cc main.c foo.c -o main.o

If you are trying to compile main.c into an assembled object file, you need to prevent gcc from trying to link. 如果您试图将main.c编译成一个汇编的目标文件,则需要防止gcc尝试链接。 This is done via 这是通过

cc -c main.c -o main.o

You can compile all other object files, then when you have all of your object files ready, you simply do 您可以编译所有其他目标文件,然后在准备好所有目标文件后,只需执行

cc main.o obj1.o anotherOBJ.o -o myExecutableBinary

"undefined reference" is a linker error, not a compiler error. “未定义的引用”是链接器错误,而不是编译器错误。

The compiler sees the declaration in the header, but you have not compiled or linked the definition in foo.c. 编译器会在标头中看到该声明 ,但是您尚未在foo.c中编译或链接该定义 Your title uses the term definition incorrectly. 您的标题使用了错误的术语定义

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么链接器看不到我的功能(用定义宏代替syslog)? - Why the linker does not see my function (define macro to substitute the syslog)? 我可以在头文件中看到该定义,那么为什么编译器会抱怨“未定义的引用”? - I can see the definition in the header file, so why is the compiler complaining of an “undefined reference”? 为什么没有参数的函数(与实际函数定义相比)会编译? - Why does a function with no parameters (compared to the actual function definition) compile? 了解头文件中的函数定义 - Understanding a function definition in a header file 为什么函数定义的顺序会改变输出二进制? - Why does the order of a function definition change the output binary? 为什么C头文件中的函数声明和函数定义具有相同的名称(来自Redis源) - Why can a function declaration and function definition with same name in the C header file (from redis source) 为什么我的浏览器有时无法识别服务器的标头? - Why does my browser sometimes not recognize my server's header? 为什么头文件需要包含在定义文件中? - why the header file needs to be included in the definition file? 为什么在方法定义文件中包含标题? - Why include header in the method definition file? 在另一个头文件中编译定义函数时出现问题 - Having Compiling trouble with definition function in another header
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM