Linking problem - please help

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

    Linking problem - please help

    I'm in the early stages of developing a class that will represent a metric
    distance by storing both a number and unit (ie KM, M, CM etc).

    I've developed some initial code as a starting point; however, it won't link
    during a compile using VC++.

    The error message I get is "
    LIBCD.lib(crt0. obj) : error LNK2019: unresolved external symbol _main
    referenced in function _mainCRTStartup

    Debug/Message.exe : fatal error LNK1120: 1 unresolved externals"


    I've included the header and source below. Any help would be appreciated.

    Thanks


    //Distance.h as follows:
    *************** *************** **************
    #ifndef DISTANCE_H

    #define DISTANCE_H

    #include <iostream>

    using namespace std;

    class Distance

    {

    public :

    Distance ( int, char ) ; // constructor (takes int value and string unit of
    measure

    ~Distance ( void ) ; // destructor (its name is ~ then class name)

    //access member functions

    int number (void) const;

    char measure (void) const;



    private :

    int nu ; // the value

    char me ; // the unit of measure

    } ;

    // provide an overload of "<<" for easy display

    ostream& operator<< (ostream&, const Distance&);

    #endif



    //Distance.cpp as follows:

    *************** *************** *************** *********
    #include "Distance.h "

    #include <iostream>

    using namespace std;

    /*-------------------------------------------------------*\

    | implementation of member functions |

    \*-------------------------------------------------------*/

    // constructor

    Distance :: Distance ( int n, char m ) : nu(n), me(m) {}

    // access functions

    int Distance :: number (void) const

    {

    return nu;

    }

    char Distance :: measure (void) const

    {

    return me;

    }

    // provide an overload of "<<" for easy display

    ostream& operator<< (ostream& out, const Distance& d)

    {

    out << d.number() << "-" << d.measure();

    return out;

    }

    /*-------------------------------------------------------*\

    | test driver for the Distance class |

    \*-------------------------------------------------------*/

    #ifdef TEST_DISTANCE // .... Distance class .... test driver

    int main ( void )

    {

    // create test input

    Distance a = Distance (5, KM);

    cout << a << endl;



    cin ignore();

    return 0; // normal termination

    }

    #endif




  • Karl Heinz Buchegger

    #2
    Re: Linking problem - please help

    Chiller wrote:[color=blue]
    >
    > I'm in the early stages of developing a class that will represent a metric
    > distance by storing both a number and unit (ie KM, M, CM etc).
    >
    > I've developed some initial code as a starting point; however, it won't link
    > during a compile using VC++.
    >
    > The error message I get is "
    > LIBCD.lib(crt0. obj) : error LNK2019: unresolved external symbol _main
    > referenced in function _mainCRTStartup
    >[/color]

    as the linker says: it cannot find a main function in what you try to link.
    [color=blue]
    >
    > #ifdef TEST_DISTANCE // .... Distance class .... test driver
    >
    > int main ( void )
    >[/color]
    [...][color=blue]
    >
    > #endif[/color]

    I cannot see a definition for TEST_DISTANCE in what you have posted. Is this
    definement set through some compiler options?

    --
    Karl Heinz Buchegger
    kbuchegg@gascad .at

    Comment

    • Karl Heinz Buchegger

      #3
      Re: Linking problem - please help

      Chiller wrote:[color=blue]
      >
      > I'm in the early stages of developing a class that will represent a metric
      > distance by storing both a number and unit (ie KM, M, CM etc).
      >
      > I've developed some initial code as a starting point; however, it won't link
      > during a compile using VC++.
      >
      > The error message I get is "
      > LIBCD.lib(crt0. obj) : error LNK2019: unresolved external symbol _main
      > referenced in function _mainCRTStartup
      >[/color]

      as the linker says: it cannot find a main function in what you try to link.
      [color=blue]
      >
      > #ifdef TEST_DISTANCE // .... Distance class .... test driver
      >
      > int main ( void )
      >[/color]
      [...][color=blue]
      >
      > #endif[/color]

      I cannot see a definition for TEST_DISTANCE in what you have posted. Is this
      definement set through some compiler options?

      --
      Karl Heinz Buchegger
      kbuchegg@gascad .at

      Comment

      • John Harrison

        #4
        Re: Linking problem - please help

        >[color=blue]
        > #ifdef TEST_DISTANCE // .... Distance class .... test driver
        >
        > int main ( void )
        >
        > {
        >
        > // create test input
        >
        > Distance a = Distance (5, KM);
        >
        > cout << a << endl;
        >
        >
        >
        > cin ignore();
        >
        > return 0; // normal termination
        >
        > }
        >
        > #endif
        >[/color]

        The obvious problem seems to be that you haven't defined TEST_DISTANCE. Are
        you sure you developed this code? And what do you think TEST_DISTANCE is for
        anyway? I would just remove it.

        john


        Comment

        • John Harrison

          #5
          Re: Linking problem - please help

          >[color=blue]
          > #ifdef TEST_DISTANCE // .... Distance class .... test driver
          >
          > int main ( void )
          >
          > {
          >
          > // create test input
          >
          > Distance a = Distance (5, KM);
          >
          > cout << a << endl;
          >
          >
          >
          > cin ignore();
          >
          > return 0; // normal termination
          >
          > }
          >
          > #endif
          >[/color]

          The obvious problem seems to be that you haven't defined TEST_DISTANCE. Are
          you sure you developed this code? And what do you think TEST_DISTANCE is for
          anyway? I would just remove it.

          john


          Comment

          • Chiller

            #6
            Re: Linking problem - please help

            I was under the impression that a test driver could be included with a class
            which would enable conditional compilation by wrapping the test in "#ifdef
            TEST_?????" and #endif. The ????? being the name of the class in uppercase.


            "Karl Heinz Buchegger" <kbuchegg@gasca d.at> wrote in message
            news:4076644F.D F8E478F@gascad. at...[color=blue]
            > Chiller wrote:[color=green]
            > >
            > > I'm in the early stages of developing a class that will represent a[/color][/color]
            metric[color=blue][color=green]
            > > distance by storing both a number and unit (ie KM, M, CM etc).
            > >
            > > I've developed some initial code as a starting point; however, it won't[/color][/color]
            link[color=blue][color=green]
            > > during a compile using VC++.
            > >
            > > The error message I get is "
            > > LIBCD.lib(crt0. obj) : error LNK2019: unresolved external symbol _main
            > > referenced in function _mainCRTStartup
            > >[/color]
            >
            > as the linker says: it cannot find a main function in what you try to[/color]
            link.[color=blue]
            >[color=green]
            > >
            > > #ifdef TEST_DISTANCE // .... Distance class .... test driver
            > >
            > > int main ( void )
            > >[/color]
            > [...][color=green]
            > >
            > > #endif[/color]
            >
            > I cannot see a definition for TEST_DISTANCE in what you have posted. Is[/color]
            this[color=blue]
            > definement set through some compiler options?
            >
            > --
            > Karl Heinz Buchegger
            > kbuchegg@gascad .at[/color]


            Comment

            • Chiller

              #7
              Re: Linking problem - please help

              I was under the impression that a test driver could be included with a class
              which would enable conditional compilation by wrapping the test in "#ifdef
              TEST_?????" and #endif. The ????? being the name of the class in uppercase.


              "Karl Heinz Buchegger" <kbuchegg@gasca d.at> wrote in message
              news:4076644F.D F8E478F@gascad. at...[color=blue]
              > Chiller wrote:[color=green]
              > >
              > > I'm in the early stages of developing a class that will represent a[/color][/color]
              metric[color=blue][color=green]
              > > distance by storing both a number and unit (ie KM, M, CM etc).
              > >
              > > I've developed some initial code as a starting point; however, it won't[/color][/color]
              link[color=blue][color=green]
              > > during a compile using VC++.
              > >
              > > The error message I get is "
              > > LIBCD.lib(crt0. obj) : error LNK2019: unresolved external symbol _main
              > > referenced in function _mainCRTStartup
              > >[/color]
              >
              > as the linker says: it cannot find a main function in what you try to[/color]
              link.[color=blue]
              >[color=green]
              > >
              > > #ifdef TEST_DISTANCE // .... Distance class .... test driver
              > >
              > > int main ( void )
              > >[/color]
              > [...][color=green]
              > >
              > > #endif[/color]
              >
              > I cannot see a definition for TEST_DISTANCE in what you have posted. Is[/color]
              this[color=blue]
              > definement set through some compiler options?
              >
              > --
              > Karl Heinz Buchegger
              > kbuchegg@gascad .at[/color]


              Comment

              • John Harrison

                #8
                Re: Linking problem - please help


                "Chiller" <...@...> wrote in message
                news:2acd3fd1f7 20607251312f0e0 8fa5220@news.te ranews.com...[color=blue]
                > I was under the impression that a test driver could be included with a[/color]
                class[color=blue]
                > which would enable conditional compilation by wrapping the test in "#ifdef
                > TEST_?????" and #endif. The ????? being the name of the class in[/color]
                uppercase.[color=blue]
                >[/color]

                Well, that's not right.

                Seems you are under the impression that the C++ pre-processor is more
                sophisticated than it really is. The rules are very simple, if you write

                #ifdef SOMETHING

                some code in here

                #endif

                then the code between the #ifdef and #endif will not be compiled unless
                SOMETHING is defined.

                That's all there is to it, test drivers and classes have no relevance.

                john


                Comment

                • John Harrison

                  #9
                  Re: Linking problem - please help


                  "Chiller" <...@...> wrote in message
                  news:2acd3fd1f7 20607251312f0e0 8fa5220@news.te ranews.com...[color=blue]
                  > I was under the impression that a test driver could be included with a[/color]
                  class[color=blue]
                  > which would enable conditional compilation by wrapping the test in "#ifdef
                  > TEST_?????" and #endif. The ????? being the name of the class in[/color]
                  uppercase.[color=blue]
                  >[/color]

                  Well, that's not right.

                  Seems you are under the impression that the C++ pre-processor is more
                  sophisticated than it really is. The rules are very simple, if you write

                  #ifdef SOMETHING

                  some code in here

                  #endif

                  then the code between the #ifdef and #endif will not be compiled unless
                  SOMETHING is defined.

                  That's all there is to it, test drivers and classes have no relevance.

                  john


                  Comment

                  • Chiller

                    #10
                    Re: Linking problem - please help

                    For anyone reading this thread.

                    As it turns out, I'm also required to declare the test driver in the
                    preprocessor definitions. This must avoid the requirement to declare or
                    undeclare the definition each time I wish to use the test driver.

                    To do this under VC++ it's simply a matter of going into the properties of
                    the project selecting the Preprocessor and adding the name of the test
                    driver, "TEST_DISTA NCE" in this case.


                    Thanks for your help Karl.



                    "John Harrison" <john_andronicu s@hotmail.com> wrote in message
                    news:c55vsi$2of lm9$1@ID-196037.news.uni-berlin.de...[color=blue]
                    >
                    > "Chiller" <...@...> wrote in message
                    > news:2acd3fd1f7 20607251312f0e0 8fa5220@news.te ranews.com...[color=green]
                    > > I was under the impression that a test driver could be included with a[/color]
                    > class[color=green]
                    > > which would enable conditional compilation by wrapping the test in[/color][/color]
                    "#ifdef[color=blue][color=green]
                    > > TEST_?????" and #endif. The ????? being the name of the class in[/color]
                    > uppercase.[color=green]
                    > >[/color]
                    >
                    > Well, that's not right.
                    >
                    > Seems you are under the impression that the C++ pre-processor is more
                    > sophisticated than it really is. The rules are very simple, if you write
                    >
                    > #ifdef SOMETHING
                    >
                    > some code in here
                    >
                    > #endif
                    >
                    > then the code between the #ifdef and #endif will not be compiled unless
                    > SOMETHING is defined.
                    >
                    > That's all there is to it, test drivers and classes have no relevance.
                    >
                    > john
                    >
                    >[/color]


                    Comment

                    • Chiller

                      #11
                      Re: Linking problem - please help

                      For anyone reading this thread.

                      As it turns out, I'm also required to declare the test driver in the
                      preprocessor definitions. This must avoid the requirement to declare or
                      undeclare the definition each time I wish to use the test driver.

                      To do this under VC++ it's simply a matter of going into the properties of
                      the project selecting the Preprocessor and adding the name of the test
                      driver, "TEST_DISTA NCE" in this case.


                      Thanks for your help Karl.



                      "John Harrison" <john_andronicu s@hotmail.com> wrote in message
                      news:c55vsi$2of lm9$1@ID-196037.news.uni-berlin.de...[color=blue]
                      >
                      > "Chiller" <...@...> wrote in message
                      > news:2acd3fd1f7 20607251312f0e0 8fa5220@news.te ranews.com...[color=green]
                      > > I was under the impression that a test driver could be included with a[/color]
                      > class[color=green]
                      > > which would enable conditional compilation by wrapping the test in[/color][/color]
                      "#ifdef[color=blue][color=green]
                      > > TEST_?????" and #endif. The ????? being the name of the class in[/color]
                      > uppercase.[color=green]
                      > >[/color]
                      >
                      > Well, that's not right.
                      >
                      > Seems you are under the impression that the C++ pre-processor is more
                      > sophisticated than it really is. The rules are very simple, if you write
                      >
                      > #ifdef SOMETHING
                      >
                      > some code in here
                      >
                      > #endif
                      >
                      > then the code between the #ifdef and #endif will not be compiled unless
                      > SOMETHING is defined.
                      >
                      > That's all there is to it, test drivers and classes have no relevance.
                      >
                      > john
                      >
                      >[/color]


                      Comment

                      • Karl Heinz Buchegger

                        #12
                        Re: Linking problem - please help

                        Chiller wrote:[color=blue]
                        >
                        > For anyone reading this thread.
                        >
                        > As it turns out, I'm also required to declare the test driver in the
                        > preprocessor definitions. This must avoid the requirement to declare or
                        > undeclare the definition each time I wish to use the test driver.
                        >
                        > To do this under VC++ it's simply a matter of going into the properties of
                        > the project selecting the Preprocessor and adding the name of the test
                        > driver, "TEST_DISTA NCE" in this case.
                        >
                        > Thanks for your help Karl.[/color]

                        2 things:

                        * Please don't top post. Put your reply beneth the text you are replying to,
                        such I have done it right now.

                        * Again: test driver or some other high sophiticated concept has nothing
                        to do with it. It's simple a preprocessor macro, that's used to include
                        or exclude some text. That this text implements the main() function is
                        a coincidence. It could be anything else

                        #include <iostream>

                        #define MY_TEST

                        int main()
                        {

                        #ifdef MY_TEST
                        std::cout << "MY_TEST is defined" << std::endl;
                        #else
                        std::cout << "MY_TEST is NOT defined" << std::endl;
                        #endif
                        }

                        If you compile and run this program, it will output
                        MY_TEST is defined

                        If you then comment the #define line such as

                        #include <iostream>

                        // #define MY_TEST

                        int main()
                        ...

                        it will output
                        MY_TEST is NOT defined

                        It's a simple question of what source code text gets compiled. Parts
                        of the source code text can be hidden by using an #ifdef preprocessor
                        directive. The preprocessor knows next to nothing about C or C++. All
                        it does is some text replacements before the actual compiler sees the
                        source code. That's all it does.

                        Most compilers also allow to #define or #undef - ine preprocessor symbols
                        via the command line interface, or the IDE. But in principle it is the same
                        thing: if the symbol is defined some text gets sent to the compiler, if it
                        is not, the text is hidden from the compiler. In this case. Such preprocessor
                        macros and symbols can be used for a number of other things. But it always
                        turns around: modify the source code text before the actual compiler sees it.

                        --
                        Karl Heinz Buchegger
                        kbuchegg@gascad .at

                        Comment

                        • Karl Heinz Buchegger

                          #13
                          Re: Linking problem - please help

                          Chiller wrote:[color=blue]
                          >
                          > For anyone reading this thread.
                          >
                          > As it turns out, I'm also required to declare the test driver in the
                          > preprocessor definitions. This must avoid the requirement to declare or
                          > undeclare the definition each time I wish to use the test driver.
                          >
                          > To do this under VC++ it's simply a matter of going into the properties of
                          > the project selecting the Preprocessor and adding the name of the test
                          > driver, "TEST_DISTA NCE" in this case.
                          >
                          > Thanks for your help Karl.[/color]

                          2 things:

                          * Please don't top post. Put your reply beneth the text you are replying to,
                          such I have done it right now.

                          * Again: test driver or some other high sophiticated concept has nothing
                          to do with it. It's simple a preprocessor macro, that's used to include
                          or exclude some text. That this text implements the main() function is
                          a coincidence. It could be anything else

                          #include <iostream>

                          #define MY_TEST

                          int main()
                          {

                          #ifdef MY_TEST
                          std::cout << "MY_TEST is defined" << std::endl;
                          #else
                          std::cout << "MY_TEST is NOT defined" << std::endl;
                          #endif
                          }

                          If you compile and run this program, it will output
                          MY_TEST is defined

                          If you then comment the #define line such as

                          #include <iostream>

                          // #define MY_TEST

                          int main()
                          ...

                          it will output
                          MY_TEST is NOT defined

                          It's a simple question of what source code text gets compiled. Parts
                          of the source code text can be hidden by using an #ifdef preprocessor
                          directive. The preprocessor knows next to nothing about C or C++. All
                          it does is some text replacements before the actual compiler sees the
                          source code. That's all it does.

                          Most compilers also allow to #define or #undef - ine preprocessor symbols
                          via the command line interface, or the IDE. But in principle it is the same
                          thing: if the symbol is defined some text gets sent to the compiler, if it
                          is not, the text is hidden from the compiler. In this case. Such preprocessor
                          macros and symbols can be used for a number of other things. But it always
                          turns around: modify the source code text before the actual compiler sees it.

                          --
                          Karl Heinz Buchegger
                          kbuchegg@gascad .at

                          Comment

                          Working...