Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

C++ compiling problem

Options
  • 14-04-2008 7:44pm
    #1
    Registered Users Posts: 5,217 ✭✭✭


    I am having trouble with some code. Was wondering if i could get some help or an explantaion.

    Using OSX 10.5.2 and gcc version 4.0.1 (Apple Inc. build 5465).

    I have a Flights.h file which seems to compile fine. A Flights.cpp file that gives me this error:
    Ulver:OO Design moju$ g++ Flights.cpp
    Undefined symbols:
      "_main", referenced from:
          start in crt1.10.5.o
    ld: symbol(s) not found
    collect2: ld returned 1 exit status
    

    I was wondering can anyone tell me what this means.


Comments

  • Registered Users Posts: 83,306 ✭✭✭✭Overheal


    what compiler are you using? I've never seen an error like that.

    is it possible you have duplicate main() functions?

    edit: C++ on a Mac? I thought they just ran on a Sadist derivation of latin..


  • Registered Users Posts: 17,727 ✭✭✭✭Sherifu


    Usually that is from how the function names are written to the file. Try using "nm" on the object code, and see what was put there (probably something like "_main_") and adjust accordingly.


  • Registered Users Posts: 5,217 ✭✭✭Matthewthebig


    Sherifu wrote: »
    Usually that is from how the function names are written to the file. Try using "nm" on the object code, and see what was put there (probably something like "_main_") and adjust accordingly.

    Use nm on the object file? Sorry I don't understand.

    I'm very new to C++.

    I could put my code up here if it would be of any help?


  • Registered Users Posts: 3,008 ✭✭✭colly10


    Use nm on the object file? Sorry I don't understand.

    I'm very new to C++.

    I could put my code up here if it would be of any help?

    Ye post it up and it might (assuming it's not massive)
    Is it running on a 64 bit processor by any chance?


  • Registered Users Posts: 5,217 ✭✭✭Matthewthebig


    The values are just for testing.
    Laptop's running on a Core2Duo.
    #include<iostream>
    #include<string>
    using std::cout;
    using std::endl;
    
    #include "Flights.h"
    
    bool Flights::setDepartureCity(string dCity)
    {
    	return true;
    }
    string Flights::getDepartureCity()
    {
    	return "This better work";
    }
    bool Flights::setArrivalCity(string aCity)
    {
    	return true;
    }
    string Flights::getArrivalCity()
    {
    	return "This better work";
    }
    bool Flights::setDepartureDate(string dDay)
    {
    	return true;
    }
    int Flights::getDepartureDate()
    {
    	return 4;
    }
    bool Flights::setDepartureTime(int time)
    {
    	return true;
    }
    int Flights::getDepartureTime()
    {
    	return 4;
    }
    bool Flights::setArrivalDate(string ADay)
    {
    	return true;
    }
    int Flights::getArrivalDate()
    {
    	return 4;
    }
    bool Flights::setArrivalTime(int time)
    {
    	return true;
    }
    int Flights::getArrivalTime()
    {
    	return 4;
    }
    bool Flights::setFlightNumber(string flight)
    {
    	return true;
    }
    string Flights::getFlightNumber()
    {
    	return "Ldasgassdgasgfas";
    }
    bool Flights::removeFlight()
    {
    	return true;
    }
    bool Flights::addPassenger()
    {
    	return true;
    }
    bool Flights::isFull()
    {
    	return true;
    }
    string Flights::getManifest()
    {
    	return "Ldasgassdgasgfas";
    }
    


  • Advertisement
  • Registered Users Posts: 3,008 ✭✭✭colly10


    Where's your main function, I assume thats not Flights.cpp?
    Does the file Flights.cpp begin with an uppercase F and your definitly in the right folder ye? Sorry this is basic, just trying to rule out possibilities


  • Registered Users Posts: 5,217 ✭✭✭Matthewthebig


    #include <string>
    #include <iostream>
    using namespace std;
    
    #include "Flights.h"
    
    int main()
    {
    	Flights flight;
    	bool b;
    	string s = ":p";
    	b = flight.setArrivalCity(s);
    	if(b)
    		cout << "wtf" << endl;
    	return 0;
    }
    
    is my main
    #include<iostream>
    #include<string>
    using namespace std;
    
    class Flights
    {
    	public:
    		bool setDepartureCity(string);
    		string getDepartureCity();
    		
    		bool setArrivalCity(string);
    		string getArrivalCity();
    		
    		bool setDepartureDate(string);
    		int getDepartureDate();
    		bool setDepartureTime(int);
    		int getDepartureTime();
    		
    		bool setArrivalDate(string);
    		int getArrivalDate();
    		bool setArrivalTime(int);
    		int getArrivalTime();
    		
    		bool setFlightNumber(string);
    		string getFlightNumber();
    		
    		bool removeFlight();
    		
    		bool addPassenger();
    		
    		bool isFull();
    
    		string getManifest();
    			
    	private:
    		string dCity, aCity, flightNumber;
    		int dTime, aTime;	
    		string dDay, aDay;
    		int planeSize;
    };
    
    is my .h file

    All in the same folder


  • Registered Users Posts: 3,008 ✭✭✭colly10


    Ok, well I fixed one of the functions in your .h file -
    bool (string);
    
    cause it was unnamed. Then I took all the functions out of the first bit of code ye posted up and put them into Flights.cpp
    It compiled fine and ran on DJGPP on XP. The only thing I can think of is that your using a compiler that may not have ever been designed to run on a core2duo (cause it came with OSX) and that there may be something that needs to be edited to get it to run (a long shot)

    Have ye used this compiler much? Sorry I couldn't be more help


  • Registered Users Posts: 5,217 ✭✭✭Matthewthebig


    First time really using it.

    I'd be surprised that it wouldn't work, because the compiler came with the OSX.


  • Registered Users Posts: 981 ✭✭✭fasty


    If your main is in a separate file, you need to compile that too... Assuming your main is in main.cpp
    g++ main.cpp flights.cpp
    

    then at the command line
    ./a.out
    

    to run it!

    This runs fine for me on Leopard. You should google for a few g++ tutorials to tell you about makefiles, and various other commands. I'm mostly a Windows programmer so I can't help too much.
    Overheal wrote: »
    edit: C++ on a Mac? I thought they just ran on a Sadist derivation of latin..

    Ooof, ignorance or humour? :P
    colly10 wrote: »
    The only thing I can think of is that your using a compiler that may not have ever been designed to run on a core2duo (cause it came with OSX) and that there may be something that needs to be edited to get it to run (a long shot)

    Not in this case, and why would g++ be not designed to run on a core2duo? :confused:


  • Advertisement
  • Registered Users Posts: 5,217 ✭✭✭Matthewthebig


    you are a bloody star!

    thank you so much


  • Registered Users Posts: 2,082 ✭✭✭Tobias Greeshman


    I am having trouble with some code. Was wondering if i could get some help or an explantaion.

    Using OSX 10.5.2 and gcc version 4.0.1 (Apple Inc. build 5465).

    I have a Flights.h file which seems to compile fine. A Flights.cpp file that gives me this error:
    Ulver:OO Design moju$ g++ Flights.cpp
    Undefined symbols:
      "_main", referenced from:
          start in crt1.10.5.o
    ld: symbol(s) not found
    collect2: ld returned 1 exit status
    
    I was wondering can anyone tell me what this means.
    The reason you're getting this _main symbol missing error is due to the fact that Flights.cpp does not have a global function called main. So you are trying to create the program from flights.cpp without an entry point into the application, every app needs an entry point called main.

    I know you've already solved this. But here's a bit of an explanation
    $ g++ -c flights.cpp
    
    You now should have a file called flight.o in the current directory. This is the object file. Now you can do the same with main.cpp to compile it into main.o
    $ g++ -c main.cpp
    
    Finally to link into the app
    $ g++ -o app main.o flights.o
    

    Now it can be ran as ./app

    I know this is a lot more long winded than doing the "g++ -o app main.cpp flights.cpp" but if you get to learn about makefiles, they'll normally generate .o files for all the sources and then link them into your app, or library.


Advertisement