This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Multiple c files linking problem

Hi everyone !

I have basic knowledge of c programming. I want to write a multiple c file project due to many functions in my program.
so, i started to learn how to write learn multiple file program in c.

here is the example of three files which i compiled and tested on Dev-cpp compiler.it gives me the output 3.
(I used Dev-cpp to learn how to write multiple c files).

main.c file :


#include <stdio.h>
#include "other.h"

int main (void)
{
    printf("%d\n", getfavoritenumber());
    return 0;
}

other.h


#ifndef _OTHER_H_
#define _OTHER_H_

extern int getfavoritenumber(void);

#endif

other.c


#include "other.h"

int getfavoritenumber(void)
{
    return 3;
}

Now i same code i tested in keil. this gives following :

Build target 'Target 1'
compiling main.c...
linking...
*** WARNING L1: UNRESOLVED EXTERNAL SYMBOL SYMBOL: GETFAVORITENUMBER MODULE: main.obj (MAIN)
*** WARNING L2: REFERENCE MADE TO UNRESOLVED EXTERNAL SYMBOL: GETFAVORITENUMBER MODULE: main.obj (MAIN) ADDRESS: 0010H
Program Size: data=11.0 xdata=0 code=23
"test" - 0 Error(s), 2 Warning(s).

I don't know what to do? Please Help...

Parents
  • Your files look correct.

    But, from the output you showed:

    Build target 'Target 1'
    compiling main.c...
    linking...
    *** WARNING L1: UNRESOLVED EXTERNAL SYMBOL SYMBOL: GETFAVORITENUMBER MODULE: main.obj (MAIN)
    *** WARNING L2: REFERENCE MADE TO UNRESOLVED EXTERNAL SYMBOL: GETFAVORITENUMBER MODULE: main.obj (MAIN) ADDRESS: 0010H
    Program Size: data=11.0 xdata=0 code=23
    "test" - 0 Error(s), 2 Warning(s).
    

    Only main.c has been compiled; not other.c - Have you added other.c to your uVision Project?

    http://www.keil.com/support/man/docs/uv4/

    http://www.keil.com/support/man/docs/uv4/uv4_examples.htm

    If you look in your Keil\C51\Hlp installation folder, you will find a GS51.chm file - this is the C51 Getting Started guide

    There is also PM51.chm - the C51 Primer

    The Primer is also available from the uVision 'Books' pane - under 'Complete User's Guide Selection'

Reply
  • Your files look correct.

    But, from the output you showed:

    Build target 'Target 1'
    compiling main.c...
    linking...
    *** WARNING L1: UNRESOLVED EXTERNAL SYMBOL SYMBOL: GETFAVORITENUMBER MODULE: main.obj (MAIN)
    *** WARNING L2: REFERENCE MADE TO UNRESOLVED EXTERNAL SYMBOL: GETFAVORITENUMBER MODULE: main.obj (MAIN) ADDRESS: 0010H
    Program Size: data=11.0 xdata=0 code=23
    "test" - 0 Error(s), 2 Warning(s).
    

    Only main.c has been compiled; not other.c - Have you added other.c to your uVision Project?

    http://www.keil.com/support/man/docs/uv4/

    http://www.keil.com/support/man/docs/uv4/uv4_examples.htm

    If you look in your Keil\C51\Hlp installation folder, you will find a GS51.chm file - this is the C51 Getting Started guide

    There is also PM51.chm - the C51 Primer

    The Primer is also available from the uVision 'Books' pane - under 'Complete User's Guide Selection'

Children
  • Thanks for your quick reply !

    i have added the other.c file so keil compiled it successfully.

    now i have made some changes to files.

    main function calls ====> get_char function (declared and defined in vvv.h & vvv.c respectively)

    get_char calls ====> getfavoritenumber function (declared and defined in other.h & other.c respectively)

    main.c

    #include <stdio.h>
    #include <p89v51rx2.h>
    #include "other.h"
    #include "vvv.h"
    
    void main (void)
    {
    
        P1=new_char();
            while(1);
    
    }
    

    vvv.h

    
    #ifdef __VVV_H__
    #define __VVV_H__
    
    extern int new_char(void);
    
    #endif
    
    

    vvv.c

    
    #include "vvv.h"
    #include "other.h"
    
    int new_char(void)
    {
    return(getfavoritenumber());
    }
    
    

    other.h

    
    #ifndef __OTHER_H__
    #define __OTHER_H__
    
    extern int getfavoritenumber(void);
    
    #endif
    
    

    other.c

    
    #include "other.h"
    
    int getfavoritenumber(void)
    {
        return 3;
    }
    
    

    this time i have added all three files to my project (i.e. main.c,other.c & vvv.c)

    now i got this warning...

    Build target 'Target 1'
    compiling main.c...
    MAIN.C(9): warning C206: 'new_char': missing function-prototype
    compiling other.c...
    compiling vvv.c...
    linking...
    Program Size: data=9.0 xdata=0 code=31
    "test" - 0 Error(s), 1 Warning(s).

    i have included both header files in my main c file.

  • sorry.

    my mistake.

    main function calls ====> new_char function (declared and defined in vvv.h & vvv.c respectively)

    new_char calls ====> getfavoritenumber function (declared and defined in other.h & other.c respectively)

  • Plz, tell me where u have gone wrong........