链接器错误未定义的“打印”引用

时间:2021-06-19 19:26:48

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". I made a header file named "nim.h" that contains the prototype of the required method. This header file "nim.h" is already included it in my "main.c". I am putting up all the files that are part of this program.

我有一个叫“main”的程序。包含调用函数的main(),该函数的定义在其他源文件“nim.c”中可用。我创建了一个名为“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 !!"); 
    }

2 个解决方案

#1


1  

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和nimo .o以及所有的buro官僚主义的东西(如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

#2


0  

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?

尝试上面的命令来构建并告诉我您究竟得到了什么错误?

#1


1  

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和nimo .o以及所有的buro官僚主义的东西(如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

#2


0  

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?

尝试上面的命令来构建并告诉我您究竟得到了什么错误?