简体   繁体   English

在头文件中找不到符号

[英]Symbol not found in header file

I have this c program:我有这个c程序:

#include    <sys/types.h>
#include    "ourhdr.h"

int     glob = 6;       /* external variable in initialized data */
char    buf[] = "a write to stdout\n";

int
main(void)
{
    int     var;        /* automatic variable on the stack */
    pid_t   pid;

    var = 88;
    if (write(STDOUT_FILENO, buf, sizeof(buf)-1) != sizeof(buf)-1)
        err_sys("write error");
    printf("before fork\n");    /* we don't flush stdout */

    if ( (pid = fork()) < 0)
        err_sys("fork error");
    else if (pid == 0) {        /* child */
        glob++;                 /* modify variables */
        var++;
    } else
        sleep(2);               /* parent */

    printf("pid = %d, glob = %d, var = %d\n", getpid(), glob, var);
    exit(0);
}

In the header file "ourhdr.h" (located in the same folder) i defined several functions, such as err_sys().在头文件“ourhdr.h”(位于同一文件夹中)中,我定义了几个函数,例如 err_sys()。 I get this error while compiling with gcc:使用 gcc 编译时出现此错误:

In function "main":
undefined reference to "err_sys"

How can i get this working?我怎样才能让它工作? I can post the header file here if needed.如果需要,我可以在这里发布头文件。 Thank you.谢谢你。

** EDIT: ** This is the ourhdr.h file: http://pastebin.com/fMUiG4zU ** 编辑:** 这是ourhdr.h 文件: http ://pastebin.com/fMUiG4zU

You have included the header file with the declaration of err_sys .您已包含带有err_sys声明的头文件。 Ensure you have also an implementation that is passed to the linker.确保您还有一个传递给链接器的实现。 Background:背景:

The compiler compiles a module to an object file: g++ -c modul1.c generates modul1.o g++ -c modul2.c generates modul2.o编译器将模块编译为目标文件: g++ -c modul1.c生成modul1.o g++ -c modul2.c生成modul2.o

Each of the modules can reference functions that are defined in a included header file.每个模块都可以引用在包含的头文件中定义的函数。 Up to here, no actual implementation of that function is needed.到这里为止,不需要该功能的实际实现。

In the next step, the linker links toghether all object files (and libraries): g++ modul1.o modul2.o generates ./a.out or similar在下一步中,链接器将所有目标文件(和库)链接在一起: g++ modul1.o modul2.o生成./a.out或类似的

In the second step, an implementation for all used functions is needed.第二步,需要对所有使用的函数进行实现。 Be sure you provided it!确保你提供了它!

(PS: Some compilers allow compiling and linking with one command, if you have multiple modules you can perhaps add them to a single gcc-call. I'd recomment to use make though) (PS:有些编译器允许使用一个命令进行编译和链接,如果您有多个模块,您可以将它们添加到单个 gcc 调用中。不过我建议使用 make)

I think what's likely to happen is that you have a header, ourhdr.h , containing the definition of the function, but the implementation is in a different file: ourhdr.c .我认为可能发生的情况是,您有一个包含函数定义的标头ourhdr.h ,但实现位于不同的文件中: ourhdr.c In that case, if you try to compile without including ourhdr.c you'll get a reference error:在这种情况下,如果您尝试在不包含ourhdr.c 的情况下进行编译,您将收到一个引用错误:

$ gcc main.c
/bin/ld: /tmp/ccVzNF6w.o: in function `main':
main.c:(.text+0x38): undefined reference to `err_sys'

To fix it, you need to compile like this:要修复它,您需要像这样编译:

$ gcc main.c ourhdr.c

Another option is to define the body of the function in ourhdr.h :另一种选择是在ourhdr.h 中定义函数

// ourhdr.h

#include <stdio.h>

void err_sys(const char* str);

void err_sys(const char* str) {
    fprintf(stderr, "%s\n", str);
}

In that case, gcc main.c should work.在这种情况下, gcc main.c应该可以工作。

对我来说,如果你写的函数名写得正确,它编译和工作正常。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM