简体   繁体   English

链接器错误未定义对`print'的引用

[英]Linker error undefined reference to `print'

I have a program named "main.c" containing the main() that calls a function whose definition is available in other source file named "nim.c". 我有一个名为“ main.c”的程序,其中包含main(),该程序调用一个函数,该函数的定义在其他名为“ nim.c”的源文件中可用。 I made a header file named "nim.h" that contains the prototype of the required method. 我制作了一个名为“ nim.h”的头文件,其中包含所需方法的原型。 This header file "nim.h" is already included it in my "main.c". 该头文件“ nim.h”已经包含在我的“ main.c”中。 I am putting up all the files that are part of this program. 我放置了该程序中的所有文件。

 //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 !!"); 
    }

you have to tell the linker, that your executable consist of two object files (main.o and nim.o) along with all the burocratic stuff (like libc, win32, etc). 您必须告诉链接器,您的可执行文件由两个目标文件(main.o和nim.o)以及所有繁琐的工作(如libc,win32等)组成。

with gcc you would compile the C-sources: 使用gcc,您可以编译C源代码:

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

I used below command and all succeeded. 我用下面的命令,一切都成功了。

gcc main.c nim.c -o nim

try above command to build and let me know what error do you get exactly? 尝试上面的命令进行构建,让我知道您究竟得到什么错误?

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

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