Online since 2002. Over 3300 puzzles, 2600 worldwide members, and 270,000 messages.

TwistyPuzzles.com Forum

It is currently Wed Apr 16, 2014 8:06 am

All times are UTC - 5 hours



Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: Need Help with g++ linker
PostPosted: Sat May 18, 2013 6:38 pm 
Offline

Joined: Sun Nov 23, 2008 2:18 am
I am trying to figure out how to use g++ to compile and link C++ programs with multiple source files.

Attached is a small card game program I wrote, and have already successfully compiled and tested a version that places everything in a single source file. At present, I have only separated one of the program's classes into a separate source file, figuring I should get the code to compile in this form before continueing with modulrizing the source.

I have also attach a text file containing the output of the following command:

Code:
g++ *.cpp


when run in the directory containing the attached source and header files.

Also, does anyone know a simple method for creating Windows executables of C++ programs from the linux command line?


Attachments:
File comment: Error Messages from g++.
error.txt [4.54 KiB]
Downloaded 24 times
File comment: Program's main source file
main.cpp.txt [20.73 KiB]
Downloaded 19 times
File comment: Card object header
Card.h.txt [869 Bytes]
Downloaded 18 times
File comment: Card object source
Card.cpp.txt [1.75 KiB]
Downloaded 22 times

_________________
Just so you know, I am blind.

I pledge allegiance to the whole of humanity, and to the world in which we live: one people under the heavens, indivisible, with Liberty and Equality for all.

My Shapeways Shop
Top
 Profile  
 
 Post subject: Re: Need Help with g++ linker
PostPosted: Sat May 18, 2013 7:14 pm 
Offline
User avatar

Joined: Thu Dec 31, 2009 8:54 pm
Location: Bay Area, California
Alright well I basically don't know any C++. I think OO programming encourages the most moronic coding practices like:

Code:
type getSomething() { return something; }


However you're getting linker errors because everything is not defined right and the linker can't find the symbols. It isn't a linker issue though, it's just that the issue shows up at link-time.

The issue has to do with your header and how you're using it. You've defined your Card class in Card.h and it looks fine. The trouble is you declare the Card class again in Card.cpp.

What you need to do include "Card.h" in Card.cpp and DO NOT declare class Card {...} in Card.cpp.

Usually constructors and destructors are defined in the header but they don't have to be.

When you want to define a member method for the Card class in Card.cpp you do something like:

Code:
char Card::getSuit() {return suit;}


You could probably play namepace tricks so that you don't have to do the Card:: part but I don't know enough C++ to tell you how that works.


Regarding compiling for Windows, you need a different toolchain. See http://www.blogcompiler.com/2010/07/11/ ... -on-linux/

_________________
Prior to using my real name I posted under the account named bmenrigh.


Top
 Profile  
 
 Post subject: Re: Need Help with g++ linker
PostPosted: Sun May 19, 2013 10:48 am 
Offline
User avatar

Joined: Thu May 31, 2007 7:13 pm
Location: Bruxelles, Belgium
As Brandon wrote, you have to separate the declaration and the definition of functions.
The error message is a bit confusing. I think the problem is duplicate definitions rather than no definition. Anyway the following code works.
Replace the Card.cpp with the code below and type as follows.
Code:
g++ -o card  main.cpp Card.cpp

or do step by step like:
Code:
g++ -c main.cpp
g++ -c Card.cpp
g++ -o card  main.o Card.o

Code:
//Program Name:   Card Object
//Author:   Jeffery Wright
//Date:      2013/05/01
//Description:Defines playing card object.

#include <iostream>
#include <cstdlib>
#include <fstream>
#include "Card.h"

using namespace std;


   //Constructs a blank card.
   Card::Card() {
      suit = ' ';
      rank = ' ';
      value = 0;
      color = " ";
      faceUp = false;
   } //end constructor

   //Constructs a card from given parameters.
   Card::Card(char s, char r, int v, string c, bool f) {
      suit = s;
      rank = r;
      value = v;
      color = c;
      faceUp = f;
   } //end constructor
   
   //Loads a card from given ifstream.
   Card::Card(ifstream &in) {
   in >> suit;
   in >> rank;
   in >> value;
   in >> color;
   in >> faceUp;
} //end constructor

   //accessors:
   char Card::getSuit() {return suit;}
   char Card::getRank() {return rank;}
   int Card::getValue() {return value;}
   string Card::getColor() {return color;}
   bool Card::getFaceUp() {return faceUp;}

   //Mutators
   void Card::setSuit(char s) {suit = s;}
   void Card::setRank(char r) {rank = r;}
   void Card::setValue(int v) {value = v;}
   void Card::setColor(string c) {color = c;}
   void Card::flip() {faceUp = !faceUp;}

   //Matchers
   bool Card::matchSuit(Card other) {return suit == other.getSuit();}
   bool Card::matchRank(Card other) {return rank == other.getRank();}
   bool Card::matchValue(Card other) {return value == other.getValue();}
   bool Card::matchColor(Card other) {return color == other.getColor();}

//Print the card.
void Card::print() {
   if (faceUp) {
      cout << rank << suit;
   } else {
      cout << "[]";
   } //end if
} //end function

//Saves the card to a file.
void Card::save(ofstream &out) {
   out << suit << " " << rank << " " << value << " " << color << " " << faceUp << endl;
} //end function

bmenrigh wrote:
Alright well I basically don't know any C++. I think OO programming encourages the most moronic coding practices like:
Code:
type getSomething() { return something; }


It's my favorite style. :lol:

_________________
Virtual Magic Polyhedra
Applet(Online)
Executable Jar Installer
Win32 Executable(Download)
troubleshooting


Top
 Profile  
 
 Post subject: Re: Need Help with g++ linker
PostPosted: Sun May 26, 2013 2:20 pm 
Offline
User avatar

Joined: Thu Dec 21, 2006 5:32 pm
Location: Bay Area, CA
bmenrigh wrote:
Alright well I basically don't know any C++. I think OO programming encourages the most moronic coding practices like:
Code:
type getSomething() { return something; }
The point of such busy work is that when you later change getSomething() to do some computation instead of just a member variable fetch, you change it in one single place instead of everywhere it is used.
Encapsulation has its merits, even if it does often lead to some redundancy. This redundancy may seem moronic in isolation, but as a pattern it sets one up for easier maintenance over the longer term.

Dave :)

_________________
Image
LitwinPuzzles.com has info on my puzzles.


Top
 Profile  
 
 Post subject: Re: Need Help with g++ linker
PostPosted: Sun May 26, 2013 2:54 pm 
Offline

Joined: Sun Nov 23, 2008 2:18 am
getters and setters can get a bit tedious, but it does offer some protection from junk data rendering an object uselessly corrupted.

Anyways, thanks for the help with my code. I have now successfully separated the other classes in my Card Games program as well as gotten a few other multi-file programs I have written to successfully compile so that I can start testing code properly.

Have not quite figured out how to get mingw set-up, though I guess offering pre-compiled Windows binaries of my work is a bit of a lower priority at the moment. Bit confused as to what I should do next after installing mingw via apt.

_________________
Just so you know, I am blind.

I pledge allegiance to the whole of humanity, and to the world in which we live: one people under the heavens, indivisible, with Liberty and Equality for all.

My Shapeways Shop


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  

Forum powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group