problem in modular programing

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ashu

    #1

    problem in modular programing

    lets look at the code given below
    here i m trying to do mudular programming


    /* this is my main prog.*/
    /*mmod.c*/
    #include<stdio. h>
    #include "mod1.h"
    int main(void)
    {
    int n;
    puts("enter the value of n");
    scanf("%d",&n);
    printf("the square of %d is :- %ld",n,sqr(n)) ;
    return 0;
    }

    /*this is my secondary module */
    /*mod1.c*/
    #include "mod1.h"

    long sqr(int x)
    {
    return ((long)((x)*(x) ));
    }

    /*this is my header file*/
    /*mod1.h*/
    long sqr(int x);


    when i compile main & secondary module,every thing is ok, but as i am

    trying to link them,there is a linker error i.e,undefined symbol _sqr

    in mmod.exe

  • Vladimir S. Oka

    #2
    Re: problem in modular programing


    ashu wrote:[color=blue]
    > lets look at the code given below
    > here i m trying to do mudular programming
    >
    >
    > /* this is my main prog.*/
    > /*mmod.c*/
    > #include<stdio. h>
    > #include "mod1.h"
    > int main(void)
    > {
    > int n;
    > puts("enter the value of n");
    > scanf("%d",&n);
    > printf("the square of %d is :- %ld",n,sqr(n)) ;
    > return 0;
    > }
    >
    > /*this is my secondary module */
    > /*mod1.c*/
    > #include "mod1.h"
    >
    > long sqr(int x)
    > {
    > return ((long)((x)*(x) ));
    > }
    >
    > /*this is my header file*/
    > /*mod1.h*/
    > long sqr(int x);
    >
    >
    > when i compile main & secondary module,every thing is ok, but as i am
    >
    > trying to link them,there is a linker error i.e,undefined symbol _sqr
    >
    > in mmod.exe[/color]

    <OT>
    Did you remember to tell your linker about mod1.o[bj]? The exact way
    will depend on your toolchain.
    </OT>

    --
    BR, Vladimir

    Comment

    • osmium

      #3
      Re: problem in modular programing

      "ashu" writes:
      [color=blue]
      > lets look at the code given below
      > here i m trying to do mudular programming
      >
      >
      > /* this is my main prog.*/
      > /*mmod.c*/
      > #include<stdio. h>
      > #include "mod1.h"
      > int main(void)
      > {
      > int n;
      > puts("enter the value of n");
      > scanf("%d",&n);
      > printf("the square of %d is :- %ld",n,sqr(n)) ;
      > return 0;
      > }
      >
      > /*this is my secondary module */
      > /*mod1.c*/
      > #include "mod1.h"
      >
      > long sqr(int x)
      > {
      > return ((long)((x)*(x) ));
      > }
      >
      > /*this is my header file*/
      > /*mod1.h*/
      > long sqr(int x);
      >
      >
      > when i compile main & secondary module,every thing is ok, but as i am
      >
      > trying to link them,there is a linker error i.e,undefined symbol _sqr
      >
      > in mood.exe[/color]

      Looks OK to me. This kind of problem relates to your compiler setup and
      this group doesn't discuss such things. So repost to a group dealing with
      your compiler. But first,search the group you select for words such as make
      file, IDE, project. Use the advanced search of Google groups. It is
      unlikely you are the first person to have this problem.


      Comment

      • tmp123

        #4
        Re: problem in modular programing


        ashu wrote:[color=blue]
        > lets look at the code given below
        > here i m trying to do mudular programming
        >
        >
        > /* this is my main prog.*/
        > /*mmod.c*/
        > #include<stdio. h>
        > #include "mod1.h"
        > int main(void)
        > {
        > int n;
        > puts("enter the value of n");
        > scanf("%d",&n);
        > printf("the square of %d is :- %ld",n,sqr(n)) ;
        > return 0;
        > }
        >
        > /*this is my secondary module */
        > /*mod1.c*/
        > #include "mod1.h"
        >
        > long sqr(int x)
        > {
        > return ((long)((x)*(x) ));
        > }
        >
        > /*this is my header file*/
        > /*mod1.h*/
        > long sqr(int x);
        >
        >
        > when i compile main & secondary module,every thing is ok, but as i am
        >
        > trying to link them,there is a linker error i.e,undefined symbol _sqr
        >
        > in mmod.exe[/color]


        Hi,

        I do not see any error in your sources that can cause this problem.
        It could be a problem with the way you compile and link. In particular,
        check the order of the .c files.
        If you are doing it with line commands or a makefile, please, post it.

        Kind regards.

        PS: To all readers: I known the compiler commands are OT, but it is the
        best way to discard that error was in the source.

        Comment

        • pemo

          #5
          Re: problem in modular programing

          tmp123 wrote:[color=blue]
          > ashu wrote:[color=green]
          >> lets look at the code given below
          >> here i m trying to do mudular programming
          >>
          >>
          >> /* this is my main prog.*/
          >> /*mmod.c*/
          >> #include<stdio. h>
          >> #include "mod1.h"
          >> int main(void)
          >> {
          >> int n;
          >> puts("enter the value of n");
          >> scanf("%d",&n);
          >> printf("the square of %d is :- %ld",n,sqr(n)) ;
          >> return 0;
          >> }
          >>
          >> /*this is my secondary module */
          >> /*mod1.c*/
          >> #include "mod1.h"
          >>
          >> long sqr(int x)
          >> {
          >> return ((long)((x)*(x) ));
          >> }
          >>
          >> /*this is my header file*/
          >> /*mod1.h*/
          >> long sqr(int x);
          >>
          >>
          >> when i compile main & secondary module,every thing is ok, but as i am
          >>
          >> trying to link them,there is a linker error i.e,undefined symbol _sqr
          >>
          >> in mmod.exe[/color]
          >
          >
          > Hi,
          >
          > I do not see any error in your sources that can cause this problem.
          > It could be a problem with the way you compile and link. In
          > particular, check the order of the .c files.
          > If you are doing it with line commands or a makefile, please, post it.
          >
          > Kind regards.
          >
          > PS: To all readers: I known the compiler commands are OT, but it is
          > the best way to discard that error was in the source.[/color]


          Sounds like you're possibly using Visual C?

          The code looks ok, btw, you don't need to include mod1.h in mod1.c - but
          it's doing no harm.

          If you've got the MS compiler set up to run from a command prompt, then this
          should work ...

          cl mmod.c mod1.c [output in mmod.exe]

          Or, if you just want to use the linker ...

          link mmod.obj mod1.obj [output in mmod.exe]

          To set up the Microsoft compiler so that it works from a command prompt,
          open a command prompt, navigate to ...\Microsoft Visual Studio\VC98\Bin and
          run vcvars32


          --
          ==============
          *Not a pedant*
          ==============


          Comment

          • Richard G. Riley

            #6
            Re: problem in modular programing

            On 2006-02-22, ashu <riskyashish@ya hoo.com> wrote:[color=blue]
            > lets look at the code given below
            > here i m trying to do mudular programming
            >
            >
            > /* this is my main prog.*/
            > /*mmod.c*/
            > #include<stdio. h>
            > #include "mod1.h"
            > int main(void)
            > {
            > int n;
            > puts("enter the value of n");
            > scanf("%d",&n);
            > printf("the square of %d is :- %ld",n,sqr(n)) ;
            > return 0;
            > }
            >
            > /*this is my secondary module */
            > /*mod1.c*/
            > #include "mod1.h"
            >
            > long sqr(int x)
            > {
            > return ((long)((x)*(x) ));
            > }
            >
            > /*this is my header file*/
            > /*mod1.h*/
            > long sqr(int x);
            >
            >
            > when i compile main & secondary module,every thing is ok, but as i am
            >
            > trying to link them,there is a linker error i.e,undefined symbol _sqr
            >
            > in mmod.exe
            >[/color]

            You are doing something wrong in the linker stage. Can you get the
            compiler specific options to compile & link? Check them.

            e.g using gcc under linux the following works fine

            gcc -g mmod.c mod1.c
            ../a.out

            (runs fine)

            One thing : you dont need to include mod1.h into mod1.c : unless
            someone can tell me a good reason why one should do so?

            There will be a lot of people telling you this is OT for this NG and
            they will be kind of right but stick with it - it can all be
            confusing when you start :) Learn about make files. Use google.

            --
            Remove evomer to reply

            Comment

            • tmp123

              #7
              Re: problem in modular programing


              Richard G. Riley wrote:[color=blue]
              >
              > One thing : you dont need to include mod1.h into mod1.c : unless
              > someone can tell me a good reason why one should do so?[/color]

              Verify prototypes?

              Comment

              • tmp123

                #8
                Re: problem in modular programing


                pemo wrote:[color=blue]
                > tmp123 wrote:[color=green]
                > > ashu wrote:[color=darkred]
                > >> lets look at the code given below
                > >> here i m trying to do mudular programming[/color][/color][/color]
                ....[color=blue][color=green]
                > >
                > > I do not see any error in your sources that can cause this problem.
                > > It could be a problem with the way you compile and link. In
                > > particular, check the order of the .c files.
                > > If you are doing it with line commands or a makefile, please, post it.
                > >[/color]
                >
                > Sounds like you're possibly using Visual C?
                >[/color]

                O, no, me not: VC is too expensive for my bank account.
                Was you trying to reply to the OP?

                Comment

                • Chris Dollin

                  #9
                  Re: problem in modular programing

                  pemo wrote:
                  [color=blue]
                  > The code looks ok, btw, you don't need to include mod1.h in mod1.c - but
                  > it's doing no harm.[/color]

                  You /should/ so include the .h in the .c; how else can you portably be
                  sure the compiler is going to detect mismatches between the declaration
                  and the definition?

                  --
                  Chris "was stirred, now shaken" Dollin
                  RIP Andreas "G'Kar" Katsulas, May 1946 - February 2006

                  Comment

                  • Richard G. Riley

                    #10
                    Re: problem in modular programing

                    On 2006-02-22, Chris Dollin <kers@hpl.hp.co m> wrote:[color=blue]
                    > pemo wrote:
                    >[color=green]
                    >> The code looks ok, btw, you don't need to include mod1.h in mod1.c - but
                    >> it's doing no harm.[/color]
                    >
                    > You /should/ so include the .h in the .c; how else can you portably be
                    > sure the compiler is going to detect mismatches between the declaration
                    > and the definition?
                    >[/color]

                    How does that affect including mod1.h into mod1.c?

                    --
                    Remove evomer to reply

                    Comment

                    • David Resnick

                      #11
                      Re: problem in modular programing

                      Richard G. Riley wrote:[color=blue]
                      > On 2006-02-22, Chris Dollin <kers@hpl.hp.co m> wrote:[color=green]
                      > > pemo wrote:
                      > >[color=darkred]
                      > >> The code looks ok, btw, you don't need to include mod1.h in mod1.c - but
                      > >> it's doing no harm.[/color]
                      > >
                      > > You /should/ so include the .h in the .c; how else can you portably be
                      > > sure the compiler is going to detect mismatches between the declaration
                      > > and the definition?
                      > >[/color]
                      >
                      > How does that affect including mod1.h into mod1.c?
                      >
                      > --
                      > Remove evomer to reply[/color]

                      If you include it, it lets the compiler diagnose mismatches between
                      the definitions and declarations as was said above. Often then
                      clearer what is going on. Not "required", just a good idea.

                      Code standards in my group say to include your own header
                      *first*. That guarantees that anything needed by your header itself
                      (e.g. including <stdio.h> for FILE that is in a declaration) will be
                      there
                      assuming that your file can then compile -- it won't be masked
                      by inclusion of <stdio.h> in some other header above yours.
                      That prevents other users of the header (if any) from
                      having to figure out why your header isn't working.

                      -David

                      Comment

                      • Richard G. Riley

                        #12
                        Re: problem in modular programing

                        On 2006-02-22, David Resnick <lndresnick@gma il.com> wrote:[color=blue]
                        > Richard G. Riley wrote:[color=green]
                        >> On 2006-02-22, Chris Dollin <kers@hpl.hp.co m> wrote:[color=darkred]
                        >> > pemo wrote:
                        >> >
                        >> >> The code looks ok, btw, you don't need to include mod1.h in mod1.c - but
                        >> >> it's doing no harm.
                        >> >
                        >> > You /should/ so include the .h in the .c; how else can you portably be
                        >> > sure the compiler is going to detect mismatches between the declaration
                        >> > and the definition?
                        >> >[/color]
                        >>
                        >> How does that affect including mod1.h into mod1.c?
                        >>
                        >> --
                        >> Remove evomer to reply[/color]
                        >
                        > If you include it, it lets the compiler diagnose mismatches between
                        > the definitions and declarations as was said above. Often then
                        > clearer what is going on. Not "required", just a good idea.
                        >[/color]

                        I can buy into that. But does it give any real advantage considering
                        the users of that external do include the header? And then theres the
                        linking to hilite errors too.
                        [color=blue]
                        > Code standards in my group say to include your own header
                        > *first*. That guarantees that anything needed by your header itself
                        > (e.g. including <stdio.h> for FILE that is in a declaration) will be
                        > there
                        > assuming that your file can then compile -- it won't be masked
                        > by inclusion of <stdio.h> in some other header above yours.
                        > That prevents other users of the header (if any) from
                        > having to figure out why your header isn't working.[/color]

                        Interesting. It was never a problem I came across but it makes some
                        sense. But I'm a little confused about what you mean by my *header*
                        needing things. I normally try to keep module headers as simple
                        declarations of that modules capabilities - the c module itself
                        includes the headers that it needs. If you follow me :)
                        [color=blue]
                        >
                        > -David
                        >[/color]


                        --
                        Remove evomer to reply

                        Comment

                        • CBFalconer

                          #13
                          Re: problem in modular programing

                          pemo wrote:[color=blue]
                          >[/color]
                          .... snip ...[color=blue]
                          >
                          > The code looks ok, btw, you don't need to include mod1.h in mod1.c
                          > - but it's doing no harm.[/color]

                          Yes he should. That checks that the .h and .c files are compatible.

                          --
                          "If you want to post a followup via groups.google.c om, don't use
                          the broken "Reply" link at the bottom of the article. Click on
                          "show options" at the top of the article, then click on the
                          "Reply" at the bottom of the article headers." - Keith Thompson
                          More details at: <http://cfaj.freeshell. org/google/>
                          Also see <http://www.safalra.com/special/googlegroupsrep ly/>

                          Comment

                          • Pedro Graca

                            #14
                            Re: problem in modular programing

                            pemo wrote:
                            [snip: ashu's example of modular programming and tmp123 answer][color=blue]
                            > you don't need to include mod1.h in mod1.c - but
                            > it's doing no harm.[/color]

                            Isn't this a standard thing to do (I mean #include WHATEVER.h in
                            WHATEVER.c) to validate the header file?

                            If the implementation is changed without a corresponding change to the
                            header file, I assume the error will not be caught.

                            Test to validate my assumption:

                            tmp$ cat main.c utils.h utils.c
                            /* main.c */
                            #include <stdio.h>
                            #include "utils.h"
                            int main(void) {
                            long x = sqr(42);
                            printf("sqr(42) is %ld.\n\n", x);
                            return 0;
                            }

                            /* utils.h */
                            long sqr(int);

                            /* utils.c */
                            /* changed return type (was long) */
                            double sqr(int a) {
                            return (double)a*a; /* added cast to force double result */
                            }

                            tmp$ gcc -W -Wall -std=c89 -pedantic main.c utils.c
                            tmp$ ./a.out
                            sqr(42) is 0.

                            tmp$ sed -e 's/long/double/' -i utils.h
                            tmp$ gcc -W -Wall -std=c89 -pedantic main.c utils.c
                            tmp$ ./a.out
                            sqr(42) is 1764.

                            --
                            If you're posting through Google read <http://cfaj.freeshell. org/google>

                            Comment

                            • Jordan Abel

                              #15
                              Re: problem in modular programing

                              On 2006-02-22, Richard G. Riley <rgrileyevomer@ gmail.com> wrote:[color=blue]
                              > Interesting. It was never a problem I came across but it makes some
                              > sense. But I'm a little confused about what you mean by my *header*
                              > needing things. I normally try to keep module headers as simple
                              > declarations of that modules capabilities - the c module itself
                              > includes the headers that it needs. If you follow me :)[/color]

                              The header may need to include other headers that declare types which
                              are used for parameters or return values within the header (as with FILE
                              in the example)

                              Comment

                              Working...