c - 链接器错误 undefined reference to `print'

标签 c linker-errors undefined-reference

我有一个名为“main.c”的程序,其中包含调用一个函数的 main(),该函数的定义在名为“nim.c”的其他源文件中可用。我制作了一个名为“nim.h”的头文件,其中包含所需方法的原型(prototype)。这个头文件“nim.h”已经包含在我的“main.c”中。我正在上传属于该程序的所有文件。

 //main.c
    #include <stdio.h>
    #include "nim.h"
    int main()
    {
         print(); 
         return 0;
    }
//nim.h
    #include<stdio.h>
           void print();
//nim.c
    #include<stdio.h>

    void print()
    {
         printf("hello !!"); 
    }

最佳答案

您必须告诉链接器,您的可执行文件由两个目标文件(main.o 和 nim.o)以及所有官僚文件(如 libc、win32 等)组成。

使用 gcc 可以编译 C 源代码:

gcc nim.c -o nim.o
gcc main.c nim.o <libraries> -o main.exe

关于c - 链接器错误 undefined reference to `print',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14871679/

相关文章:

c - 如何在执行时编译一段C代码,并获得对应函数的指针?

objective-c - 如何改变这个程序的输入? (C语言)

c - Linux 中的 printf()/fprintf() 问题,UTF-8 编码

c++ - 对 std::ios_base::Init::Init() 的 undefined reference

ios - 谷歌地图SDK : Undefined symbols for architecture x86_64

c - C程序中的 undefined reference 错误

c++ - 对 'imp____glewEnableVertexAttribArray' 的 undefined reference

c - 在 C 中用指针修改结构成员

c++ - 使用头文件中定义的 vector 时出现 Unresolved external symbol 错误

c++ - 带cmake的 Armadillo 链接器错误