2

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.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 !!"); 
    }
5
  • 1
    Have you compiled nim.c as well as main.c ? Without this, the compiler will have found a definition of print (via the include) but the linker won't be able to find an implementation of it. Commented Feb 14, 2013 at 9:36
  • 3
    You need to link with the object file, gcc main.c nim.o, or compile them together, gcc main.c nim.c. Commented Feb 14, 2013 at 9:36
  • Sir, actually when i am compiling nim.c it is giving following undefined reference to `WinMain@16' Commented Feb 14, 2013 at 9:57
  • in nim.h you do not need the "#include <stdio.h>". I would put a "#include "nim.h" into the nim.c. Reason: if you change the definition of print(), and forget to change the declaration, the compiler will throw an error for you. Commented Feb 14, 2013 at 10:00
  • @user2064676 actually its giving you that error at link time, not compile time, and it is because you have he wrong subsystem selected. Right click your project in your project explorer tree, select Properties, goto the Linker settings, select the System option, and change the target system to CONSOLE rather than WINDOWS. Then rebuild the world. Commented Feb 14, 2013 at 10:02

2 Answers 2

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).

with gcc you would compile the C-sources:

gcc nim.c -o nim.o
gcc main.c nim.o <libraries> -o main.exe
Sign up to request clarification or add additional context in comments.

Comments

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?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.