Thread: facing problem with multiple source file porgram.

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    4

    facing problem with multiple source file porgram.

    Hi ,
    This is first time,i am trying to execute a program with multiple source files.
    I have A.C ,B.C & a header file "header.h"(all attached here).A.C is supposed to use a function named "disp()" which is defined in B.C.
    I declared the function "disp()" in the "header.h" and included the header in both A.C and B.C. But it is showing an error message as follows:
    "Linker error: Undefined symbol _disp in module A.c"
    I am using Turbo C++ 3.0 version.please help.Thank you for the help..
    Attached Files Attached Files
    • File Type: c B.C (96 Bytes, 177 views)
    • File Type: h HEADER..H (60 Bytes, 187 views)
    • File Type: c A.C (77 Bytes, 172 views)

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well the first thing you need is a decent compiler for the modern age (and compatible with your operating system).

    Unlike darth vader, this sad devotion to the ancient ways does NOT confer such users with special magical powers.

    Pick one of these, all are used by the regulars, so there is no shortage of help.
    Microsoft Express Downloads - Visual Studio Express and SQL Server Express
    smorgasbordet - Pelles C
    Code::Blocks

    Oh, and you need to make sure ALL your .c files are added to the project, not just the one containing main().
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    4
    Thank u for the response.I will certainly use any one of the modern compilers suggested by you.
    But just for the sake of my curiosity,please tell "what is wrong with the code??".

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Do you have both source files listed in the project?

    Look at this example

    Compile one source file only, get unresolved symbol
    $ gcc a.c
    /tmp/ccgoIGQn.o: In function `main':
    a.c:(.text+0x7): undefined reference to `disp'
    collect2: ld returned 1 exit status

    Compile both, job is done.
    $ gcc a.c b.c
    b.c: In function ‘disp’:
    b.c:5: warning: incompatible implicit declaration of built-in function ‘printf’
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    1) you are redeclaring disp() in your b.c file... the declaration in the header is enough.
    2) you need to #include <stdio.h> in b.c as well as a.c

    3) A and B are realy lousy file names for even a project this trivial.
    4) void disp() is an invalid function definition that will work only in that crappy TurboC compiler...s/b... void disp(void)
    5) It's int main (void) and it returns an errorlevel value at the end (usually 0).

  6. #6
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    There were few things which I has to change, before i got this running on my machine. But most of the issue have noted down but other in the previous post. But the other thing which i wanted specify is that C is a very typed language, even an alphabet can make a difference. In your A.c file you include the header file like

    Code:
    #include "header.h"
    But there isn't file such that. You have HEADER..h. Should be renamed to 'header.h'

    See my compiling process.

    Code:
    Cygwin@Cygwin-PC ~/Code
    $ gcc A.c B.c
    A.c:7:2: warning: no newline at end of file
    
    Cygwin@Cygwin-PC ~/Code
    $ gcc -o output A.o B.o
    
    Cygwin@Cygwin-PC ~/Code
    $ ./output.exe
    the disp function should work
    
    Cygwin@Cygwin-PC ~/Code
    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  7. #7
    Registered User
    Join Date
    May 2011
    Posts
    4
    Quote Originally Posted by CommonTater View Post
    1) you are redeclaring disp() in your b.c file... the declaration in the header is enough.
    2) you need to #include <stdio.h> in b.c as well as a.c

    3) A and B are realy lousy file names for even a project this trivial.
    4) void disp() is an invalid function definition that will work only in that crappy TurboC compiler...s/b... void disp(void)
    5) It's int main (void) and it returns an errorlevel value at the end (usually 0).
    Thanks for the corrections mate.But even after the corrections,compiling the A.c is giving the same error:
    "Linker error: Undefined symbol _disp in module A.c"
    i am using a turbo C compiler.Shuld i use "extern" to declare "void disp(void)" function?

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by learning"c" View Post
    Thanks for the corrections mate.But even after the corrections,compiling the A.c is giving the same error:
    "Linker error: Undefined symbol _disp in module A.c"
    i am using a turbo C compiler.Shuld i use "extern" to declare "void disp(void)" function?
    How to fix this...

    1) uninstall Turbo C... and flush the disks down the toilet.

    2) Go HERE and download the version (32 or 64 bit) relevent to your operating system.

    3) Install

    4) Read the help file... no joke... really read it!

    5) Start your first project.

    6) Rewrite the files using C-99 syntax as I explained.

    7) Build...

    8) Run.

    It's not just your code that's the problem... it's your freaking Fred Flintstone compiler!

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by CommonTater
    4) void disp() is an invalid function definition that will work only in that crappy TurboC compiler...s/b... void disp(void)
    It is fine as part of a function definition and will work with any standard conforming C compiler. Actually, it is also fine as part of a function declaration that is not a function definition, but then writing it as void disp(void); would then make it certain that disp does not have any parameters.

    Quote Originally Posted by learning"c"
    Thanks for the corrections mate.But even after the corrections,compiling the A.c is giving the same error:
    "Linker error: Undefined symbol _disp in module A.c"
    i am using a turbo C compiler.Shuld i use "extern" to declare "void disp(void)" function?
    What exactly were the corrections that you made in the end? One of things to note is that you have a linker error: if you only compiled A.c, then that's where your problem lies. You should compile both source files and link them.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    May 2011
    Posts
    4
    Quote Originally Posted by laserlight View Post
    It is fine as part of a function definition and will work with any standard conforming C compiler. Actually, it is also fine as part of a function declaration that is not a function definition, but then writing it as void disp(void); would then make it certain that disp does not have any parameters.


    What exactly were the corrections that you made in the end? One of things to note is that you have a linker error: if you only compiled A.c, then that's where your problem lies. You should compile both source files and link them.
    now files A.c ,B.c and header.h look like :
    A.c
    Code:
    #include<stdio.h>
    #include "header.h"
    int main(void)
    {
    	disp();
    	return 0;
    }
    B.c :
    Code:
    #include "header.h"
    #include<stdio.h>
    void disp(void)
    {
    	printf("the disp function should work");
    }
    header.h :
    Code:
    #ifndef header_h_
    #define header_h_
    void disp(void);
    #endif
    thank you for the help

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    That's looks fine, though I would have written header_h_ as HEADER_H_. So, look into the linker error by compiling both source files and linking them.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. facing problem with Buffer
    By enggabhinandan in forum C Programming
    Replies: 5
    Last Post: 11-22-2006, 06:26 AM
  2. facing problem in understanding this....
    By enggabhinandan in forum C Programming
    Replies: 10
    Last Post: 10-25-2006, 05:30 AM
  3. fine! tried but facing a problem
    By samirself in forum C Programming
    Replies: 11
    Last Post: 10-07-2004, 08:59 AM
  4. Replies: 1
    Last Post: 05-01-2003, 02:52 PM
  5. Facing problem while linking
    By mehmood in forum C++ Programming
    Replies: 2
    Last Post: 02-02-2003, 05:54 AM