linker error

Discussion in 'C++' started by Victor Bazarov, Jul 16, 2005.

  1. Baloff wrote:
    > I have this linker error which makes me think that the definition file
    > is not being seen by the linker, this code is taken from "Thinking in
    > C++, 2nd Edition, Volume 1, Annotated Solutions Guide"
    > Using Dev-C++ 4.9.9.2, The error is:
    > [Linker error] undefined reference to ‘f(int)’
    > all the following files are in the same dir.


    It's not enough to have files in the same directory. You need to feed
    all C++ files to the compiler so that they are processed into "object"
    files and then feed all "object" files to the linker to produce the
    final executable (and sometimes it can be done in the same command).

    As to how to do that, look in your compiler manual, it falls outside
    the topic of this newsgroup.

    > [..]


    V
    Victor Bazarov, Jul 16, 2005
    #1
    1. Advertising

  2. Baloff wrote:
    > Hello
    > I have this linker error which makes me think that the definition file
    > is not being seen by the linker, this code is taken from "Thinking in
    > C++, 2nd Edition, Volume 1, Annotated Solutions Guide"
    > Using Dev-C++ 4.9.9.2, The error is:
    > [Linker error] undefined reference to 'f(int)'
    > all the following files are in the same dir.
    >
    > thanks
    >
    > //: S03:prototypes.h
    > void f(int);
    > ///:~
    >
    > //: S03:prototypes.cpp {O}
    > // Implements functions declared in Prototypes.h
    > #include <iostream>
    > using namespace std;
    >
    > void f(int i) {
    > cout << "f(" << i << ") returning void\n";
    > }
    > ///:~
    >
    > //: S03:protoTest.cpp
    > //{L} Prototypes
    > #include <iostream>
    > #include "Prototypes.h"
    >
    > int main() {
    > f(1);
    >
    > getchar();
    > return 0;
    > }


    There's no problem with the code. Check your setup, prototypes.cpp is
    probably not included in your project. For further questions concerning
    the environment (as opposed to the language itself), please post in an
    appropriate newsgroup.


    Jonathan
    Jonathan Mcdougall, Jul 16, 2005
    #2
    1. Advertising

  3. Victor Bazarov

    Manfred Guest

    Baloff wrote:
    > Hello
    > I have this linker error which makes me think that the definition file
    > is not being seen by the linker, this code is taken from "Thinking in
    > C++, 2nd Edition, Volume 1, Annotated Solutions Guide"
    > Using Dev-C++ 4.9.9.2, The error is:
    > [Linker error] undefined reference to ‘f(int)’
    > all the following files are in the same dir.
    >
    > thanks
    >
    > //: S03:prototypes.h
    > void f(int);
    > ///:~
    >
    > //: S03:prototypes.cpp {O}
    > // Implements functions declared in Prototypes.h
    > #include <iostream>
    > using namespace std;
    >
    > void f(int i) {
    > cout << "f(" << i << ") returning void\n";
    > }
    > ///:~
    >
    > //: S03:protoTest.cpp
    > //{L} Prototypes
    > #include <iostream>
    > #include "Prototypes.h"
    >
    > int main() {
    > f(1);
    >
    > getchar();
    > return 0;
    > }


    This Code works for me.
    When compiling on the comandline you should call something like
    'g++ Prototypes.cpp ProtoTest.cpp' or adjust your projectsetup.

    best regards

    Manfred
    Manfred, Jul 16, 2005
    #3
  4. Victor Bazarov

    Ken Wilson Guest

    On Sun, 17 Jul 2005 04:10:01 -0700, Baloff <> did
    courageously avow:

    >Hello
    >I have this linker error which makes me think that the definition file
    >is not being seen by the linker, this code is taken from "Thinking in
    >C++, 2nd Edition, Volume 1, Annotated Solutions Guide"
    >Using Dev-C++ 4.9.9.2, The error is:
    >[Linker error] undefined reference to ‘f(int)’
    >all the following files are in the same dir.
    >
    >thanks
    >
    >//: S03:prototypes.h
    >void f(int);
    >///:~
    >
    >//: S03:prototypes.cpp {O}


    In the code below you need to also include your header
    so this file knows where to find the function.
    >// Implements functions declared in Prototypes.h
    >#include <iostream>


    #include "Prototypes.h"
    >using namespace std;
    >
    >void f(int i) {
    > cout << "f(" << i << ") returning void\n";
    >}
    >///:~
    >
    >//: S03:protoTest.cpp
    >//{L} Prototypes
    >#include <iostream>
    >#include "Prototypes.h"
    >
    >int main() {
    > f(1);
    >
    > getchar();
    > return 0;
    >}


    Ken Wilson
    "Coding, coding, over the bounding main()"
    Ken Wilson, Jul 16, 2005
    #4
  5. Ken Wilson wrote:
    > On Sun, 17 Jul 2005 04:10:01 -0700, Baloff <> did
    > courageously avow:
    >
    > >Hello
    > >I have this linker error which makes me think that the definition file
    > >is not being seen by the linker, this code is taken from "Thinking in
    > >C++, 2nd Edition, Volume 1, Annotated Solutions Guide"
    > >Using Dev-C++ 4.9.9.2, The error is:
    > >[Linker error] undefined reference to 'f(int)'
    > >all the following files are in the same dir.
    > >
    > >thanks
    > >
    > >//: S03:prototypes.h
    > >void f(int);
    > >///:~
    > >
    > >//: S03:prototypes.cpp {O}

    >
    > In the code below you need to also include your header
    > so this file knows where to find the function.


    No. The "file" does not need to have a forward declaration for f(). You
    need a forward declaration when you want to use a name which is not
    declared. In this file, you are not using any undeclared name, you are
    just defining a function.

    > >// Implements functions declared in Prototypes.h
    > >#include <iostream>

    >
    > #include "Prototypes.h"
    >
    > >using namespace std;
    > >
    > >void f(int i) {
    > > cout << "f(" << i << ") returning void\n";
    > >}



    Jonathan
    Jonathan Mcdougall, Jul 16, 2005
    #5
  6. Victor Bazarov

    Ken Wilson Guest

    On Sat, 16 Jul 2005 20:36:23 +0200, Manfred <> did
    courageously avow:

    >Baloff wrote:
    >> Hello
    >> I have this linker error which makes me think that the definition file
    >> is not being seen by the linker, this code is taken from "Thinking in
    >> C++, 2nd Edition, Volume 1, Annotated Solutions Guide"
    >> Using Dev-C++ 4.9.9.2, The error is:
    >> [Linker error] undefined reference to ‘f(int)’
    >> all the following files are in the same dir.
    >>
    >> thanks
    >>
    >> //: S03:prototypes.h
    >> void f(int);
    >> ///:~
    >>
    >> //: S03:prototypes.cpp {O}
    >> // Implements functions declared in Prototypes.h
    >> #include <iostream>
    >> using namespace std;
    >>
    >> void f(int i) {
    >> cout << "f(" << i << ") returning void\n";
    >> }
    >> ///:~
    >>
    >> //: S03:protoTest.cpp
    >> //{L} Prototypes
    >> #include <iostream>
    >> #include "Prototypes.h"
    >>
    >> int main() {
    >> f(1);
    >>
    >> getchar();
    >> return 0;
    >> }

    >
    >This Code works for me.
    >When compiling on the comandline you should call something like
    >'g++ Prototypes.cpp ProtoTest.cpp' or adjust your projectsetup.
    >
    >best regards
    >
    >Manfred


    He's using Visual Studio. It doesn't work quite that way, at least
    not if your new to it. I ran his code on my Visual C++ here and it
    was missing the include for the Prototypes header file in the
    Prototypes.cpp file. I am assuming he's just running the default
    compile which automatically does the whole project, no separate
    compilation. It compiles alright. Of course it should. It just won't
    link because the .cpp doesn't know there's an .h file with the rest of
    the answer.

    Ken Wilson
    "Coding, coding, over the bounding main()"
    Ken Wilson, Jul 16, 2005
    #6
  7. Ken Wilson wrote:
    > On Sat, 16 Jul 2005 20:36:23 +0200, Manfred <> did
    > courageously avow:
    >
    > >Baloff wrote:
    > >> Hello
    > >> I have this linker error which makes me think that the definition file
    > >> is not being seen by the linker, this code is taken from "Thinking in
    > >> C++, 2nd Edition, Volume 1, Annotated Solutions Guide"
    > >> Using Dev-C++ 4.9.9.2, The error is:
    > >> [Linker error] undefined reference to 'f(int)'
    > >> all the following files are in the same dir.
    > >>
    > >> thanks
    > >>
    > >> //: S03:prototypes.h
    > >> void f(int);
    > >> ///:~
    > >>
    > >> //: S03:prototypes.cpp {O}
    > >> // Implements functions declared in Prototypes.h
    > >> #include <iostream>
    > >> using namespace std;
    > >>
    > >> void f(int i) {
    > >> cout << "f(" << i << ") returning void\n";
    > >> }
    > >> ///:~
    > >>
    > >> //: S03:protoTest.cpp
    > >> //{L} Prototypes
    > >> #include <iostream>
    > >> #include "Prototypes.h"
    > >>
    > >> int main() {
    > >> f(1);
    > >>
    > >> getchar();
    > >> return 0;
    > >> }

    > >
    > >This Code works for me.
    > >When compiling on the comandline you should call something like
    > >'g++ Prototypes.cpp ProtoTest.cpp' or adjust your projectsetup.
    > >
    > >best regards
    > >
    > >Manfred

    >
    > He's using Visual Studio.


    No, he's using Dev-C++ 4.9.9.2.

    > It doesn't work quite that way, at least
    > not if your new to it. I ran his code on my Visual C++ here and it
    > was missing the include for the Prototypes header file in the
    > Prototypes.cpp file.


    How did you reach that conclusion?

    > I am assuming he's just running the default
    > compile which automatically does the whole project, no separate
    > compilation. It compiles alright. Of course it should. It just won't
    > link because the .cpp doesn't know there's an .h file with the rest of
    > the answer.


    That's plain wrong. Read my other post.


    Jonathan
    Jonathan Mcdougall, Jul 16, 2005
    #7
  8. Victor Bazarov

    Ken Wilson Guest

    On 16 Jul 2005 12:06:52 -0700, "Jonathan Mcdougall"
    <> did courageously avow:

    >Ken Wilson wrote:
    >> On Sat, 16 Jul 2005 20:36:23 +0200, Manfred <> did
    >> courageously avow:
    >>
    >> >Baloff wrote:
    >> >> Hello
    >> >> I have this linker error which makes me think that the definition file
    >> >> is not being seen by the linker, this code is taken from "Thinking in
    >> >> C++, 2nd Edition, Volume 1, Annotated Solutions Guide"
    >> >> Using Dev-C++ 4.9.9.2, The error is:
    >> >> [Linker error] undefined reference to 'f(int)'
    >> >> all the following files are in the same dir.
    >> >>
    >> >> thanks
    >> >>
    >> >> //: S03:prototypes.h
    >> >> void f(int);
    >> >> ///:~
    >> >>
    >> >> //: S03:prototypes.cpp {O}
    >> >> // Implements functions declared in Prototypes.h
    >> >> #include <iostream>
    >> >> using namespace std;
    >> >>
    >> >> void f(int i) {
    >> >> cout << "f(" << i << ") returning void\n";
    >> >> }
    >> >> ///:~
    >> >>
    >> >> //: S03:protoTest.cpp
    >> >> //{L} Prototypes
    >> >> #include <iostream>
    >> >> #include "Prototypes.h"
    >> >>
    >> >> int main() {
    >> >> f(1);
    >> >>
    >> >> getchar();
    >> >> return 0;
    >> >> }
    >> >
    >> >This Code works for me.
    >> >When compiling on the comandline you should call something like
    >> >'g++ Prototypes.cpp ProtoTest.cpp' or adjust your projectsetup.
    >> >
    >> >best regards
    >> >
    >> >Manfred

    >>
    >> He's using Visual Studio.

    >
    >No, he's using Dev-C++ 4.9.9.2.
    >
    >> It doesn't work quite that way, at least
    >> not if your new to it. I ran his code on my Visual C++ here and it
    >> was missing the include for the Prototypes header file in the
    >> Prototypes.cpp file.

    >
    >How did you reach that conclusion?


    Because I physically recreated his code from those files he posted and
    the three wouldn't compile until I told Prototypes.cpp where to find
    the definition for the function. That definition is in the .h file.

    And yes. They will *compile* separately. I tried that. But they
    won't link. That tells me something couldn't find what it was looking
    for. And the error told me exactly which function to trace. That
    header file is what glues the two cpp files together so they both need
    to know where it is.

    >
    >> I am assuming he's just running the default
    >> compile which automatically does the whole project, no separate
    >> compilation. It compiles alright. Of course it should. It just won't
    >> link because the .cpp doesn't know there's an .h file with the rest of
    >> the answer.

    >
    >That's plain wrong. Read my other post.


    I did.

    Ken Wilson
    "Coding, coding, over the bounding main()"
    Ken Wilson, Jul 17, 2005
    #8
  9. Victor Bazarov

    Baloff Guest

    Hello
    I have this linker error which makes me think that the definition file
    is not being seen by the linker, this code is taken from "Thinking in
    C++, 2nd Edition, Volume 1, Annotated Solutions Guide"
    Using Dev-C++ 4.9.9.2, The error is:
    [Linker error] undefined reference to ‘f(int)’
    all the following files are in the same dir.

    thanks

    //: S03:prototypes.h
    void f(int);
    ///:~

    //: S03:prototypes.cpp {O}
    // Implements functions declared in Prototypes.h
    #include <iostream>
    using namespace std;

    void f(int i) {
    cout << "f(" << i << ") returning void\n";
    }
    ///:~

    //: S03:protoTest.cpp
    //{L} Prototypes
    #include <iostream>
    #include "Prototypes.h"

    int main() {
    f(1);

    getchar();
    return 0;
    }
    Baloff, Jul 17, 2005
    #9
  10. Victor Bazarov

    Xie Yubo Guest

    Baloff wrote:
    > Hello
    > I have this linker error which makes me think that the definition file
    > is not being seen by the linker, this code is taken from "Thinking in
    > C++, 2nd Edition, Volume 1, Annotated Solutions Guide"
    > Using Dev-C++ 4.9.9.2, The error is:
    > [Linker error] undefined reference to ‘f(int)’
    > all the following files are in the same dir.
    >
    > thanks
    >
    > //: S03:prototypes.h
    > void f(int);
    > ///:~
    >
    > //: S03:prototypes.cpp {O}
    > // Implements functions declared in Prototypes.h
    > #include <iostream>
    > using namespace std;
    >
    > void f(int i) {
    > cout << "f(" << i << ") returning void\n";
    > }
    > ///:~
    >
    > //: S03:protoTest.cpp
    > //{L} Prototypes
    > #include <iostream>
    > #include "Prototypes.h"
    >
    > int main() {
    > f(1);
    >
    > getchar();
    > return 0;
    > }

    I think, maybe, you cann't add the file "Prototypes.cpp" to the project.
    So the devcpp do it like this:
    g++ ProtoTest.cpp -o ProtoTest.exe,
    In fact, it should be:
    g++ ProtoTest.cpp Prototypes.cpp -o ProtoTest.exe
    So, you can add the "Prototypes.cpp" to the project, and try it again.
    Good luck~~ :)

    --
    Best Regards

    Xie Yubo
    Email: Website: http://xieyubo.cn/
    Harbin Institute of Technology
    Phone: 86-451-86416614 Fax: 86-451-86413309
    Xie Yubo, Jul 17, 2005
    #10
  11. Victor Bazarov

    Ken Wilson Guest

    On Sun, 17 Jul 2005 20:55:16 +0800, Xie Yubo <> did
    courageously avow:

    >Baloff wrote:
    >> Hello
    >> I have this linker error which makes me think that the definition file
    >> is not being seen by the linker, this code is taken from "Thinking in
    >> C++, 2nd Edition, Volume 1, Annotated Solutions Guide"
    >> Using Dev-C++ 4.9.9.2, The error is:
    >> [Linker error] undefined reference to ‘f(int)’
    >> all the following files are in the same dir.
    >>
    >> thanks
    >>
    >> //: S03:prototypes.h
    >> void f(int);
    >> ///:~
    >>
    >> //: S03:prototypes.cpp {O}
    >> // Implements functions declared in Prototypes.h
    >> #include <iostream>
    >> using namespace std;
    >>
    >> void f(int i) {
    >> cout << "f(" << i << ") returning void\n";
    >> }
    >> ///:~
    >>
    >> //: S03:protoTest.cpp
    >> //{L} Prototypes
    >> #include <iostream>
    >> #include "Prototypes.h"
    >>
    >> int main() {
    >> f(1);
    >>
    >> getchar();
    >> return 0;
    >> }

    >I think, maybe, you cann't add the file "Prototypes.cpp" to the project.
    >So the devcpp do it like this:
    > g++ ProtoTest.cpp -o ProtoTest.exe,
    >In fact, it should be:
    > g++ ProtoTest.cpp Prototypes.cpp -o ProtoTest.exe
    >So, you can add the "Prototypes.cpp" to the project, and try it again.
    >Good luck~~ :)


    I have to admit I am getting a bit confused. I figured that many
    people can't be on the wrong track so I moved the code to my Red Hat
    box and the Linux people are correct in that environment. It will
    compile and link to an executable without an include for Prototypes.h
    in the Prototypes.cpp. However, the exact same code will not compile
    and link on a Windows box with Visual Studio but generates the linker
    error that initially started this thread.

    I would venture a guess, and it is a guess, that one of these
    compilers is not acting in proper fashion. Which compiler is
    following the standard on this? Does anyone know what the standard
    says about this?

    What bothers me is every text I've ever read tells me when I have a
    separate .h and .cpp that if I want into the contents of the .h from
    my .cpp I had better put an include in. Or is g++ a little more lax
    on this and will automatically make the connection between a .cpp and
    a .h file with the same name? If so, what happens to my code if I
    change the name of the .cpp file but not the .h file because the .h is
    referenced in too many other files?

    Thanks for your patience.

    Ken Wilson
    "Coding, coding, over the bounding main()"
    Ken Wilson, Jul 17, 2005
    #11
  12. Victor Bazarov

    Xie Yubo Guest

    Ken Wilson wrote:
    > On Sun, 17 Jul 2005 20:55:16 +0800, Xie Yubo <> did
    > courageously avow:
    >
    >
    >>Baloff wrote:
    >>
    >>>Hello
    >>>I have this linker error which makes me think that the definition file
    >>>is not being seen by the linker, this code is taken from "Thinking in
    >>>C++, 2nd Edition, Volume 1, Annotated Solutions Guide"
    >>>Using Dev-C++ 4.9.9.2, The error is:
    >>>[Linker error] undefined reference to ‘f(int)’
    >>>all the following files are in the same dir.
    >>>
    >>>thanks
    >>>
    >>>//: S03:prototypes.h
    >>>void f(int);
    >>>///:~
    >>>
    >>>//: S03:prototypes.cpp {O}
    >>>// Implements functions declared in Prototypes.h
    >>>#include <iostream>
    >>>using namespace std;
    >>>
    >>>void f(int i) {
    >>> cout << "f(" << i << ") returning void\n";
    >>>}
    >>>///:~
    >>>
    >>>//: S03:protoTest.cpp
    >>>//{L} Prototypes
    >>>#include <iostream>
    >>>#include "Prototypes.h"
    >>>
    >>>int main() {
    >>> f(1);
    >>>
    >>> getchar();
    >>> return 0;
    >>>}

    >>
    >>I think, maybe, you cann't add the file "Prototypes.cpp" to the project.
    >>So the devcpp do it like this:
    >> g++ ProtoTest.cpp -o ProtoTest.exe,
    >>In fact, it should be:
    >> g++ ProtoTest.cpp Prototypes.cpp -o ProtoTest.exe
    >>So, you can add the "Prototypes.cpp" to the project, and try it again.
    >>Good luck~~ :)

    >
    >
    > I have to admit I am getting a bit confused. I figured that many
    > people can't be on the wrong track so I moved the code to my Red Hat
    > box and the Linux people are correct in that environment. It will
    > compile and link to an executable without an include for Prototypes.h
    > in the Prototypes.cpp. However, the exact same code will not compile
    > and link on a Windows box with Visual Studio but generates the linker
    > error that initially started this thread.
    >
    > I would venture a guess, and it is a guess, that one of these
    > compilers is not acting in proper fashion. Which compiler is
    > following the standard on this? Does anyone know what the standard
    > says about this?
    >
    > What bothers me is every text I've ever read tells me when I have a
    > separate .h and .cpp that if I want into the contents of the .h from
    > my .cpp I had better put an include in. Or is g++ a little more lax
    > on this and will automatically make the connection between a .cpp and
    > a .h file with the same name? If so, what happens to my code if I
    > change the name of the .cpp file but not the .h file because the .h is
    > referenced in too many other files?
    >
    > Thanks for your patience.
    >
    > Ken Wilson
    > "Coding, coding, over the bounding main()"

    I think you should know that there is no relation between the name of .h
    and .cpp. Although people like make the same name of .h and .cpp if the
    ..h file contains the declaration of functions which are implemented in
    ..cpp. But it's not a must! In another word, you can declare the
    functions in A.h and implement them in B.cpp, it doesn't matter.

    The .h would be included in some .cpp. It only tells the compiler what
    some identified words are. It doesn't tell the compiler or linker where
    is the implementation.

    If you use:
    g++ A.cpp B.cpp -o A.exe
    it may be completed as follow steps:
    1. g++ -c A.cpp A.o ("-c" means: only compile not link)
    2. g++ -c B.cpp B.o
    3. ld A.o B.o -o A.exe

    You can see that only the last step is to link. The linker will search
    all the files you specified in the command-line to find where is the
    implementation of functions. So the linker doesn't use .h file.


    --
    Best Regards

    Xie Yubo
    Email: Website: http://xieyubo.cn/
    Harbin Institute of Technology
    Phone: 86-451-86416614 Fax: 86-451-86413309
    Xie Yubo, Jul 17, 2005
    #12
  13. Victor Bazarov

    Ken Wilson Guest

    On Sun, 17 Jul 2005 22:11:05 +0800, Xie Yubo <> did
    courageously avow:

    >Ken Wilson wrote:
    >> On Sun, 17 Jul 2005 20:55:16 +0800, Xie Yubo <> did
    >> courageously avow:
    >>
    >>
    >>>Baloff wrote:
    >>>
    >>>>Hello
    >>>>I have this linker error which makes me think that the definition file
    >>>>is not being seen by the linker, this code is taken from "Thinking in
    >>>>C++, 2nd Edition, Volume 1, Annotated Solutions Guide"
    >>>>Using Dev-C++ 4.9.9.2, The error is:
    >>>>[Linker error] undefined reference to ‘f(int)’
    >>>>all the following files are in the same dir.
    >>>>
    >>>>thanks
    >>>>
    >>>>//: S03:prototypes.h
    >>>>void f(int);
    >>>>///:~
    >>>>
    >>>>//: S03:prototypes.cpp {O}
    >>>>// Implements functions declared in Prototypes.h
    >>>>#include <iostream>
    >>>>using namespace std;
    >>>>
    >>>>void f(int i) {
    >>>> cout << "f(" << i << ") returning void\n";
    >>>>}
    >>>>///:~
    >>>>
    >>>>//: S03:protoTest.cpp
    >>>>//{L} Prototypes
    >>>>#include <iostream>
    >>>>#include "Prototypes.h"
    >>>>
    >>>>int main() {
    >>>> f(1);
    >>>>
    >>>> getchar();
    >>>> return 0;
    >>>>}
    >>>
    >>>I think, maybe, you cann't add the file "Prototypes.cpp" to the project.
    >>>So the devcpp do it like this:
    >>> g++ ProtoTest.cpp -o ProtoTest.exe,
    >>>In fact, it should be:
    >>> g++ ProtoTest.cpp Prototypes.cpp -o ProtoTest.exe
    >>>So, you can add the "Prototypes.cpp" to the project, and try it again.
    >>>Good luck~~ :)

    >>
    >>
    >> I have to admit I am getting a bit confused. I figured that many
    >> people can't be on the wrong track so I moved the code to my Red Hat
    >> box and the Linux people are correct in that environment. It will
    >> compile and link to an executable without an include for Prototypes.h
    >> in the Prototypes.cpp. However, the exact same code will not compile
    >> and link on a Windows box with Visual Studio but generates the linker
    >> error that initially started this thread.
    >>
    >> I would venture a guess, and it is a guess, that one of these
    >> compilers is not acting in proper fashion. Which compiler is
    >> following the standard on this? Does anyone know what the standard
    >> says about this?
    >>
    >> What bothers me is every text I've ever read tells me when I have a
    >> separate .h and .cpp that if I want into the contents of the .h from
    >> my .cpp I had better put an include in. Or is g++ a little more lax
    >> on this and will automatically make the connection between a .cpp and
    >> a .h file with the same name? If so, what happens to my code if I
    >> change the name of the .cpp file but not the .h file because the .h is
    >> referenced in too many other files?
    >>
    >> Thanks for your patience.
    >>
    >> Ken Wilson
    >> "Coding, coding, over the bounding main()"

    >I think you should know that there is no relation between the name of .h
    >and .cpp. Although people like make the same name of .h and .cpp if the
    >.h file contains the declaration of functions which are implemented in
    >.cpp. But it's not a must! In another word, you can declare the
    >functions in A.h and implement them in B.cpp, it doesn't matter.
    >
    >The .h would be included in some .cpp. It only tells the compiler what
    >some identified words are. It doesn't tell the compiler or linker where
    >is the implementation.
    >
    >If you use:
    > g++ A.cpp B.cpp -o A.exe
    >it may be completed as follow steps:
    >1. g++ -c A.cpp A.o ("-c" means: only compile not link)
    >2. g++ -c B.cpp B.o
    >3. ld A.o B.o -o A.exe
    >
    >You can see that only the last step is to link. The linker will search
    >all the files you specified in the command-line to find where is the
    >implementation of functions. So the linker doesn't use .h file.


    I didn't ask what happens under g++. As I said, I ran the code with
    both compilers and saw the results. It's not really whether something
    is or isn't done here that bothers me at this point, it's the
    inconsistency between the two tools that are essentially both supposed
    to be doing exactly the same job.

    What if you're trying to write portable code with thousands of files
    missing header include statements and you bring that code over to my
    Windows box? Things are going to break left, right and sideways and
    be a long drawn out process to correct. I'm not seeking to apportion
    blame on the guys who write these compilation I'm just trying to get a
    clearer picture of what is standard and what is up to the compiler
    vendor to implement.


    Ken Wilson
    "Coding, coding, over the bounding main()"
    Ken Wilson, Jul 17, 2005
    #13
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Wolfgang Rueckert

    Linker error - unsolved external symbol

    Wolfgang Rueckert, Jun 30, 2003, in forum: C++
    Replies:
    0
    Views:
    696
    Wolfgang Rueckert
    Jun 30, 2003
  2. Martin Magnusson

    Linker error with static const field

    Martin Magnusson, Oct 20, 2003, in forum: C++
    Replies:
    2
    Views:
    434
    Ron Natalie
    Oct 20, 2003
  3. Arne Schmitz
    Replies:
    3
    Views:
    351
    E. Robert Tisdale
    Apr 16, 2004
  4. TR
    Replies:
    3
    Views:
    8,138
    New_user
    Jun 10, 2004
  5. quad
    Replies:
    3
    Views:
    2,293
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=
    Jun 9, 2007
Loading...

Share This Page