Find answers, ask questions, and connect with our <br>community around the world.

  • Posted by Raghu Karthik on January 9, 2024 at 10:16 am

    In UseBookClass example.
    ——————————————————————————————————————-
    When i run the following its working fine (let’s call this method 1 ):
    $ g++ -Wall -c BookClass.cpp

    $ g++ -Wall -c UseBookClass.cpp

    g++ -o UseBookClass UseBookClass.o BookClass.o
    ——————————————————————————————————————-
    But when i use make file (Let’s call this method 2) the following error shows up :

    $ make UseBookClass

    g++ UseBookClass.cpp -o UseBookClass

    /usr/bin/ld: /tmp/cc5bWWLg.o: in function main':</p><p>UseBookClass.cpp:(.text+0xab): undefined reference toBook::set_year(int)’

    /usr/bin/ld: UseBookClass.cpp:(.text+0xd6): undefined reference to Book::get_year() const'</p><p>collect2: error: ld returned 1 exit status</p><p style="cursor: text;">make: *** [<builtin>: UseBookClass] Error 1<br>-------------------------------------------------------------------------------------------------------------------<br>It worked when i included an extra line <span style="background-color: var(--bb-content-background-color); font-family: inherit; font-size: inherit; color: var(--bb-body-text-color); cursor: text;">#include "BookClass.cpp" in UseBookClass.cpp file. But method 1 has not worked now with the following error<br></span></p><p>$ g++ -o UseBookClass UseBookClass.o BookClass.o</p><p>/usr/bin/ld: BookClass.o: in functionBook::set_year(int)’:

    BookClass.cpp:(.text+0x0): multiple definition of Book::set_year(int)'; UseBookClass.o:UseBookClass.cpp:(.text+0x0): first defined here</p><p>/usr/bin/ld: BookClass.o: in functionBook::get_year() const’:

    BookClass.cpp:(.text+0x5e): multiple definition of `Book::get_year() const’; UseBookClass.o:UseBookClass.cpp:(.text+0x5e): first defined here

    collect2: error: ld returned 1 exit status
    ——————————————————————————————————————-
    what is happening here why is method1 and method2 are not working for the same code. they diifer by a line #include “BookClass.cpp” in UseBookClass.cpp file.

    Nishant replied 5 months ago 3 Members · 7 Replies
  • 7 Replies
  • Raghu Karthik

    Member
    January 9, 2024 at 10:28 am

    Question is displayed like a backend code . so I am using all images to describe the question . please go through the images

  • Yagna Murthy Ayachitula

    Member
    January 9, 2024 at 11:52 am

    You don’t need to include a header file in the main function file. Just try it like that. hope you already included it in the function file.

    • Raghu Karthik

      Member
      January 9, 2024 at 12:05 pm

      yeah if i don’t include header (#include BookClass.cpp ) file make UsebookClass is not working , but it is working (in this case header (#include BookClass.cpp ) should not be included) by manually linking in command prompt. this is my doubt. Linker is not linking properly when using make file. why? I cross checked with the video several times , Every line of my code is same.

  • Nishant

    Member
    January 10, 2024 at 5:10 am

    Hi Raghu,

    Can you please try by copy-pasting the following lines into your Makefile,

    BookClass.o : BookClass.cpp Book.hpp

    (Tab Spacing)g++ -Wall -c BookClass.cpp

    UseBookClass.o : UseBookClass.cpp Book.hpp

    (Tab Spacing)g++ -Wall -c UseBookClass.cpp

    UseBookClass : UseBookClass.o BookClass.o

    (Tab Spacing)g++ -o UseBookClass UseBookClass.o BookClass.o

    You need to only include Book.hpp into the BookClass.cpp and UseBookClass.cpp files. Please let me know if that works.

  • Raghu Karthik

    Member
    January 10, 2024 at 6:53 am

    I am getting the same error(attached in pic).

    I also ran in sequence “make BookClass.o” , “make UseBookClass.o” and ” make UseBookClass” . The first two ran and printed in the terminal the following “g++ -c -o BookClass.o BookClass.cpp” & “g++ -c -o UseBookClass.o UseBookClass.cpp” respectively and *.o files created.
    But the 3rd one throws an error (new error : it reads in several lines the following : undefined reference to std::<many things>, it failed to link standard c++ library here.).
    So, I copied last line of make file into command line “g++ -o UseBookClass UseBookClass.o BookClass.o” then it created executable file with no issue.

    When I directly run make file in single step ” make UseBookClass” *.o files are not created and the error is displayed (see pic)

    • Raghu Karthik

      Member
      January 18, 2024 at 1:41 pm

      Need support with this issue,
      This still didn’t resolve yet. after running make file main issue is *.o files are not getting created. during tutorial all 3 commands of makefile ran, then *.o files are created and linked . But in my case they aren’t running. only linker is trying to run which means only last line of makefile is running and hence i am getting the error that linker is not able to find *.o files.

  • Nishant

    Member
    January 19, 2024 at 2:10 pm

    Hi Raghu,

    I will connect with you over a Zoom call and resolve the issue. You can raise a request to set up a Zoom call on Sunday.

Log in to reply.

error: Content is protected !!