g++ main main.cpp class.o

This is a discussion on g++ main main.cpp class.o within the C++ Programming forums, part of the General Programming Boards category; I have a class.h/class.cpp outside my main file. When compiling my main file, I have to do the following: Code: ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    167

    g++ main main.cpp class.o

    I have a class.h/class.cpp outside my main file. When compiling my main file, I have to do the following:

    Code:
    g++ -Wall -g -o main main.cpp class.o
    Is this normal? Or am I doing something wrong?

    In my main I do have #include "class.h" but I still have to put that class.o on the g++ line, otherwise I get tons of errors:

    Code:
    /tmp/ccbTjMPf.o: In function `Line::draw(Image&)':
    /fs/home1/student/s/myfolder/cs60/myfolder_proj3/Line.h:53: undefined reference to `Image::DrawLine(int, int, int, int, Vec3f const&)'
    /tmp/ccbTjMPf.o: In function `Background::draw(Image&)':
    /fs/home1/student/s/myfolder/cs60/myfolder_proj3/Background.h:72: undefined reference to `Image::DrawLine(int, int, int, int, Vec3f const&)'
    /tmp/ccbTjMPf.o: In function `Circle::draw(Image&)':
    /fs/home1/student/s/myfolder/cs60/myfolder_proj3/Circle.h:52: undefined reference to `Image::DrawCircle(int, int, int, Vec3f const&)'
    /tmp/ccbTjMPf.o: In function `Triangle::draw(Image&)':
    /fs/home1/student/s/myfolder/cs60/myfolder_proj3/Triangle.h:56: undefined reference to `Image::DrawLine(int, int, int, int, Vec3f const&)'
    /fs/home1/student/s/myfolder/cs60/myfolder_proj3/Triangle.h:57: undefined reference to `Image::DrawLine(int, int, int, int, Vec3f const&)'
    /fs/home1/student/s/myfolder/cs60/myfolder_proj3/Triangle.h:58: undefined reference to `Image::DrawLine(int, int, int, int, Vec3f const&)'
    /tmp/ccbTjMPf.o: In function `Quadrilateral::draw(Image&)':
    /fs/home1/student/s/myfolder/cs60/myfolder_proj3/Quadrilateral.h:61: undefined reference to `Image::DrawLine(int, int, int, int, Vec3f const&)'
    /fs/home1/student/s/myfolder/cs60/myfolder_proj3/Quadrilateral.h:62: undefined reference to `Image::DrawLine(int, int, int, int, Vec3f const&)'
    /tmp/ccbTjMPf.o:/fs/home1/student/s/myfolder/cs60/myfolder_proj3/Quadrilateral.h:63: more undefined references to `Image::DrawLine(int, int, int, int, Vec3f const&)' follow
    /tmp/ccbTjMPf.o: In function `CPPDraw::save(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)':
    /fs/home1/student/s/myfolder/cs60/myfolder_proj3/CPPDraw.h:90: undefined reference to `Image::SavePPM(char const*) const'
    /fs/home1/student/s/myfolder/cs60/myfolder_proj3/CPPDraw.h:92: undefined reference to `Image::SaveTGA(char const*) const'
    collect2: ld returned 1 exit status
    Last edited by Paul22000; 05-26-2008 at 07:28 PM.

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    The errors are saying that you are calling certain functions but it can't find them to link them. Do you perhaps have a library you need to link with?

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    167
    Quote Originally Posted by Thantos View Post
    The errors are saying that you are calling certain functions but it can't find them to link them. Do you perhaps have a library you need to link with?
    That's what I mean:

    Code:
    g++ -Wall -g -o main main.cpp class.o // Works fine
    Code:
    g++ -Wall -g -o main main.cpp // Gives errors w/o the class.o object
    Is this normal?

    The reason I ask is because I'm coding a project where I will have to include literally 7 things. So it will look like this:

    Code:
    g++ -Wall -g -o main main.cpp class1.o class2.o class3.o class4.o class5.o class6.o class7.o
    I'm sure there is a way around that, correct? I must be doing something wrong...

  4. #4
    Registered User
    Join Date
    Nov 2007
    Posts
    19
    I presume you have a files called class[x].cpp if you have you should compile like this
    g++ -Wall -g -o main main.cpp class1.cpp class2.cpp ... class7.cpp

    off course all that typing is cumbersome and you can always use makefiles in large projects to make sure compilation of dependencys is reduce to minimum.

    Here is a basic tutorial on makefiles

    http://mrbook.org/tutorials/make/
    Last edited by force of will; 05-26-2008 at 08:53 PM.

  5. #5
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Quote Originally Posted by Paul22000 View Post
    That's what I mean:

    Code:
    g++ -Wall -g -o main main.cpp class.o // Works fine
    Code:
    g++ -Wall -g -o main main.cpp // Gives errors w/o the class.o object
    Is this normal?
    Perfectly. In order to build the executable it needs those other object files in order to link in those functions.

    The reason I ask is because I'm coding a project where I will have to include literally 7 things. So it will look like this:

    Code:
    g++ -Wall -g -o main main.cpp class1.o class2.o class3.o class4.o class5.o class6.o class7.o
    I'm sure there is a way around that, correct? I must be doing something wrong...
    No real way around it. I would suggest looking into build utilities like Make (there are others) that simply the build process.

    Commonly (in this situation) you'd compile each source file into the object file and then run it through the linker to build the executable. So in this case you'd do
    Code:
    g++ -Wall -C g -o main.o main.cpp
    And then
    Code:
    g++ -o main main.o class1.o class2.o
    This is one area where a build script can help.

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    167
    Quote Originally Posted by Thantos View Post
    Perfectly. In order to build the executable it needs those other object files in order to link in those functions.

    No real way around it. I would suggest looking into build utilities like Make (there are others) that simply the build process.
    Awesome! Thanks. Yeah I'm using a makefile, but it just looks odd to have so many files on the g++ line. This is the first time we've had so many classes in one project.

    I was just worried that it looked weird hehe. Thanks

  7. #7
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,246
    Quote Originally Posted by Paul22000 View Post
    Awesome! Thanks. Yeah I'm using a makefile, but it just looks odd to have so many files on the g++ line. This is the first time we've had so many classes in one project.

    I was just worried that it looked weird hehe. Thanks
    7 files is a lot? How about thousands and thousands of them? Code gets big. At that point you'd typically start consolidating things into libraries, though.

  8. #8
    Registered User
    Join Date
    Apr 2008
    Posts
    167
    Quote Originally Posted by brewbuck View Post
    7 files is a lot? How about thousands and thousands of them? Code gets big. At that point you'd typically start consolidating things into libraries, though.
    Ok, hehe.

    It's my first class in C++

    =]

  9. #9
    Registered User
    Join Date
    May 2008
    Location
    Paris
    Posts
    248
    7 files is a lot? How about thousands and thousands of them?
    automate your Makefile. This is where they are useful. In any case, you'll _always_ need to list the files you use for the project. This is what MSVS also does with its project file (or solution file, I always forget).

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by MarkZWEERS View Post
    automate your Makefile. This is where they are useful. In any case, you'll _always_ need to list the files you use for the project. This is what MSVS also does with its project file (or solution file, I always forget).
    It's the project file - the solution file is a collection of projects (one or more).

    I think what Mark means by automate the makefile is something like what you have at the bottom of that make tutorial.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Registered User
    Join Date
    Nov 2007
    Posts
    19
    Your makefile also help you reducing compile time, if you make it right, something like this

    Code:
    HOSTC               = gcc                              #c compiler
    HOSTCXX           = g++                            #c++ compiler
    HOSTCXXFLAGS = -Wall                           #c++ compiler flags
    HOSTCFLAGS     = $(HOSTCXXFLAGS)      #c compiler flags
    
    .SUFIXXES : .o .cpp .c
    
    #teach the make utility how to make a .o from a cpp file
    .cpp.o:          
               $(HOSTCXX) (HOSTCXXFLAGS) -c $<
    
    #teach the make utility how to make a .o from a c file
    .c.o               
                $(HOSTCXX) $(HOSTCXXFLAGS) -c $<      #here i treat c files as c++ files and use g++
    
    #rule to build all
    all:   main 
    
    #here we say main depends on the files class1.o class2.o etc so when make utility comes here it checks all dependencies (for instance class1.o) and since
    #(since we teach make to create a .o file  from c and c++ files it just checks if they are older (checks class1.cpp time stamp of modification against class1.o) and if they 
    #need to be "recreated" it uses the rule to build the .o from the newer .cpp or .c file
    
    main : main.cpp class1.o class2.o class.3 class4.o class5.o class6.o class7.o
               $(HOSTCXX) $(HOSTCXXFLAGS) -o main class1.o class2.o class3.o\
                                                                    class 4.o class 5.o class6.o class7.o
               @echo 'build complete'
    
    clean: #rule to clean files
               rm -f *.o


    This way it only compiles the files that really need compilation.
    Last edited by force of will; 05-27-2008 at 07:44 AM.

  12. #12
    Registered User
    Join Date
    Apr 2008
    Posts
    167
    By the way, when I code for my class, I'm using Putty to connect to the school's computers which are on Unix, so I use Makefiles.

    Random question... Is there any way to get Makefiles to work on... Windows?

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Paul22000 View Post
    By the way, when I code for my class, I'm using Putty to connect to the school's computers which are on Unix, so I use Makefiles.

    Random question... Is there any way to get Makefiles to work on... Windows?
    Sure, you can use gnumake (probably comes with Dev-Cpp and the like or nmake (comes with visual studio) - if your makefile isn't using special features, then using either should be fine.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  14. #14
    Registered User
    Join Date
    Apr 2008
    Posts
    167
    Quote Originally Posted by matsp View Post
    Sure, you can use gnumake (probably comes with Dev-Cpp and the like or nmake (comes with visual studio) - if your makefile isn't using special features, then using either should be fine.

    --
    Mats
    Thanks, I'll have to try those!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. passing params to main()
    By mike11 in forum C++ Programming
    Replies: 14
    Last Post: 06-21-2005, 12:36 PM
  2. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  3. int main (), main (), main (void), int main (void) HELP!!!
    By SmokingMonkey in forum C++ Programming
    Replies: 7
    Last Post: 05-31-2003, 09:46 PM
  4. what to do with the main window
    By stallion in forum Windows Programming
    Replies: 2
    Last Post: 01-28-2003, 07:58 PM
  5. void or int for main?:)
    By bigtamscot in forum C Programming
    Replies: 13
    Last Post: 09-27-2001, 03:11 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21