?
Solved

Problems compiling in C++

Posted on 2009-02-19
13
Medium Priority
?
1,070 Views
Last Modified: 2013-11-17
I finished writing this program that computes the fastest way to get from one city to another using a graph.  the way im doing it is that the fastest way is the one that will get you to your destination city with the fewest amount of transfers, meaning the fastest way would be to travel along a single route, zero transfers.   However when I compile the code I get a bunch of errors.

Can any experienced C++ programmers help me fix my program?  Thanks

here are the files
http://www.mediafire.com/?sharekey=6f250c6d962271144c17ca8801618ef7e04e75f6e8ebb871



[user@server CS212]$ g++ main.cpp
/tmp/ccSeNqIS.o: In function `__static_initialization_and_destruction_0(int, int                                                                                                  )':
main.cpp:(.text+0x51): undefined reference to `graph::graph()'
/tmp/ccSeNqIS.o: In function `writeKnowledge(std::basic_istream<char, std::char_                                                                                                  traits<char> >&)':
main.cpp:(.text+0x503): undefined reference to `graph::writeKnowledgeToFile(std:                                                                                                  :basic_string<char, std::char_traits<char>, std::allocator<char> >)'
/tmp/ccSeNqIS.o: In function `listAllRoutes()':
main.cpp:(.text+0x5a2): undefined reference to `graph::printAllRoutes()'
/tmp/ccSeNqIS.o: In function `listAllCities()':
main.cpp:(.text+0x5ea): undefined reference to `graph::printAllCities()'
/tmp/ccSeNqIS.o: In function `listCitiesOfRoute(std::basic_istream<char, std::ch                                                                                                  ar_traits<char> >&)':
main.cpp:(.text+0x711): undefined reference to `route::route()'
main.cpp:(.text+0x73d): undefined reference to `graph::getRoute(std::basic_strin                                                                                                  g<char, std::char_traits<char>, std::allocator<char> >, route&)'
main.cpp:(.text+0x77e): undefined reference to `route::getName()'
main.cpp:(.text+0x7fd): undefined reference to `route::printCities()'
/tmp/ccSeNqIS.o: In function `listRoutesOfCity(std::basic_istream<char, std::cha                                                                                                  r_traits<char> >&)':
main.cpp:(.text+0x99c): undefined reference to `graph::printRoutesOfCity(std::ba                                                                                                  sic_string<char, std::char_traits<char>, std::allocator<char> >)'
/tmp/ccSeNqIS.o: In function `deleteRoute(std::basic_istream<char, std::char_tra                                                                                                  its<char> >&)':
main.cpp:(.text+0xb3c): undefined reference to `graph::removeRoute(std::basic_st                                                                                                  ring<char, std::char_traits<char>, std::allocator<char> >)'
/tmp/ccSeNqIS.o: In function `searchRoute(std::basic_istream<char, std::char_tra                                                                                                  its<char> >&)':
main.cpp:(.text+0xe05): undefined reference to `graph::searchPath(std::basic_str                                                                                                  ing<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char                                                                                                  , std::char_traits<char>, std::allocator<char> >)'
/tmp/ccSeNqIS.o: In function `readRoute(std::basic_istream<char, std::char_trait                                                                                                  s<char> >&)':
main.cpp:(.text+0xfeb): undefined reference to `route::route()'
main.cpp:(.text+0x100f): undefined reference to `route::setName(std::basic_strin                                                                                                  g<char, std::char_traits<char>, std::allocator<char> >)'
main.cpp:(.text+0x11be): undefined reference to `route::addCity(std::basic_strin                                                                                                  g<char, std::char_traits<char>, std::allocator<char> >)'
main.cpp:(.text+0x1227): undefined reference to `graph::addRoute(route)'
collect2: ld returned 1 exit status
0
Comment
Question by:ubuntuguy
  • 6
  • 3
  • 2
  • +1
13 Comments
 
LVL 1

Author Comment

by:ubuntuguy
ID: 23688691
P.S I used Dev C++ on Windows.  I must compile it on  Linux server.  You think maybe that's the problem?
0
 
LVL 43

Accepted Solution

by:
ravenpl earned 1000 total points
ID: 23689614
> [user@server CS212]$ g++ main.cpp
will not do, it wants all files compiled first, then linked.
Try

for i in *.cpp; do g++ -c -I. $i; done
g++ -o prog.exe *.o
0
 
LVL 53

Assisted Solution

by:Infinity08
Infinity08 earned 1000 total points
ID: 23690182
Or better yet, use a proper makefile to build the project.

Note that Dev-C++ already created a makefile for you. All you have to do, is adapt it for the new platform, and you're all set.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 2

Expert Comment

by:dannykurniawan
ID: 23690837
do this:

g++ route.cpp -o route.o
g++ city.cpp -o city.o
g++ graph.cpp -o graph.o
g++ main.cpp -o main.o
g++ main.o city.o graph.o main.o -o proj2

then run proj2
0
 
LVL 1

Author Comment

by:ubuntuguy
ID: 23692663
Hey I tried running the following,
g++ route.cpp -o route.o
g++ city.cpp -o city.o
g++ graph.cpp -o graph.o
g++ main.cpp -o main.o
g++ main.o city.o graph.o main.o -o proj2


but it doesn't compile

[user@server proj2]$ g++ route.cpp -o route.o
/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../crt1.o: In function `_start':
(.text+0x18): undefined reference to `main'
/tmp/ccGaqs2U.o: In function `route::printCities()':
route.cpp:(.text+0xf7): undefined reference to `city::getName()'
/tmp/ccGaqs2U.o: In function `route::hasCity(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)':
route.cpp:(.text+0x1a1): undefined reference to `city::getName()'
/tmp/ccGaqs2U.o: In function `route::addCity(city)':
route.cpp:(.text+0x30e): undefined reference to `city::getName()'
route.cpp:(.text+0x32a): undefined reference to `city::getName()'
route.cpp:(.text+0x411): undefined reference to `city::getName()'
/tmp/ccGaqs2U.o:route.cpp:(.text+0x429): more undefined references to `city::getName()' follow
/tmp/ccGaqs2U.o: In function `route::addCity(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)':
route.cpp:(.text+0x67a): undefined reference to `city::setName(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
collect2: ld returned 1 exit status


user@server proj2]$ g++ city.cpp -o city.o
/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../crt1.o: In function `_start':
(.text+0x18): undefined reference to `main'
collect2: ld returned 1 exit status


[user@server proj2]$ g++ graph.cpp -o graph.or
/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../crt1.o: In function `_start':
(.text+0x18): undefined reference to `main'
/tmp/ccsQdFik.o: In function `graph::printAllRoutes()':
graph.cpp:(.text+0xda): undefined reference to `route::getName()'
/tmp/ccsQdFik.o: In function `graph::removeRoute(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)':
graph.cpp:(.text+0x16e): undefined reference to `route::getName()'
/tmp/ccsQdFik.o: In function `graph::hasRoute(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)':
graph.cpp:(.text+0x206): undefined reference to `route::getName()'
/tmp/ccsQdFik.o: In function `graph::getRoute(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, route&)':
graph.cpp:(.text+0x29a): undefined reference to `route::getName()'
/tmp/ccsQdFik.o: In function `graph::writeKnowledgeToFile(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)':
graph.cpp:(.text+0x3a3): undefined reference to `route::getName()'
graph.cpp:(.text+0x405): undefined reference to `route::cityCount()'
graph.cpp:(.text+0x42d): undefined reference to `route::getCityAtIndex(int)'
graph.cpp:(.text+0x442): undefined reference to `city::getName()'
/tmp/ccsQdFik.o: In function `graph::printRoutesOfCity(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)':
graph.cpp:(.text+0x58d): undefined reference to `route::hasCity(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
graph.cpp:(.text+0x5d4): undefined reference to `route::getName()'
/tmp/ccsQdFik.o: In function `graph::searchPath(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> >)':
graph.cpp:(.text+0x6d5): undefined reference to `route::hasCity(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
graph.cpp:(.text+0x7c8): undefined reference to `route::hasCity(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
graph.cpp:(.text+0x810): undefined reference to `route::cityCount()'
graph.cpp:(.text+0x838): undefined reference to `route::getCityAtIndex(int)'
graph.cpp:(.text+0x85d): undefined reference to `city::getName()'
graph.cpp:(.text+0x872): undefined reference to `route::hasCity(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
graph.cpp:(.text+0x9f9): undefined reference to `route::getName()'
/tmp/ccsQdFik.o: In function `graph::addRoute(route)':
graph.cpp:(.text+0xb42): undefined reference to `route::route()'
graph.cpp:(.text+0xb54): undefined reference to `route::getName()'
graph.cpp:(.text+0xbaa): undefined reference to `route::cityCount()'
graph.cpp:(.text+0xbcf): undefined reference to `route::getCityAtIndex(int)'
graph.cpp:(.text+0xbe4): undefined reference to `route::addCity(city)'
graph.cpp:(.text+0xc9b): undefined reference to `route::getName()'
graph.cpp:(.text+0xcb7): undefined reference to `route::getName()'
graph.cpp:(.text+0xd97): undefined reference to `route::getName()'
graph.cpp:(.text+0xdaf): undefined reference to `route::getName()'
graph.cpp:(.text+0xe2c): undefined reference to `route::getName()'
/tmp/ccsQdFik.o:graph.cpp:(.text+0xe44): more undefined references to `route::getName()' follow
/tmp/ccsQdFik.o: In function `graph::printAllCities()':
graph.cpp:(.text+0xf00): undefined reference to `route::route()'
graph.cpp:(.text+0xf2c): undefined reference to `route::cityCount()'
graph.cpp:(.text+0xf51): undefined reference to `route::getCityAtIndex(int)'
graph.cpp:(.text+0xf66): undefined reference to `route::addCity(city)'
graph.cpp:(.text+0xfb3): undefined reference to `route::printCities()'
/tmp/ccsQdFik.o: In function `graph::qNode::qNode()':
graph.cpp:(.text._ZN5graph5qNodeC1Ev[graph::qNode::qNode()]+0xd): undefined reference to `route::route()'
/tmp/ccsQdFik.o: In function `graph::graphNode::graphNode()':
graph.cpp:(.text._ZN5graph9graphNodeC1Ev[graph::graphNode::graphNode()]+0xd): undefined reference to `route::route()'
collect2: ld returned 1 exit status



[user@server proj2]$ g++ main.cpp -o main.o
/tmp/ccoSUJsn.o: In function `__static_initialization_and_destruction_0(int, int)':
main.cpp:(.text+0x51): undefined reference to `graph::graph()'
/tmp/ccoSUJsn.o: In function `writeKnowledge(std::basic_istream<char, std::char_traits<char> >&)':
main.cpp:(.text+0x503): undefined reference to `graph::writeKnowledgeToFile(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
/tmp/ccoSUJsn.o: In function `listAllRoutes()':
main.cpp:(.text+0x5a2): undefined reference to `graph::printAllRoutes()'
/tmp/ccoSUJsn.o: In function `listAllCities()':
main.cpp:(.text+0x5ea): undefined reference to `graph::printAllCities()'
/tmp/ccoSUJsn.o: In function `listCitiesOfRoute(std::basic_istream<char, std::char_traits<char> >&)':
main.cpp:(.text+0x711): undefined reference to `route::route()'
main.cpp:(.text+0x73d): undefined reference to `graph::getRoute(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, route&)'
main.cpp:(.text+0x77e): undefined reference to `route::getName()'
main.cpp:(.text+0x7fd): undefined reference to `route::printCities()'
/tmp/ccoSUJsn.o: In function `listRoutesOfCity(std::basic_istream<char, std::char_traits<char> >&)':
main.cpp:(.text+0x99c): undefined reference to `graph::printRoutesOfCity(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
/tmp/ccoSUJsn.o: In function `deleteRoute(std::basic_istream<char, std::char_traits<char> >&)':
main.cpp:(.text+0xb3c): undefined reference to `graph::removeRoute(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
/tmp/ccoSUJsn.o: In function `searchRoute(std::basic_istream<char, std::char_traits<char> >&)':
main.cpp:(.text+0xe05): undefined reference to `graph::searchPath(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
/tmp/ccoSUJsn.o: In function `readRoute(std::basic_istream<char, std::char_traits<char> >&)':
main.cpp:(.text+0xfeb): undefined reference to `route::route()'
main.cpp:(.text+0x100f): undefined reference to `route::setName(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
main.cpp:(.text+0x11be): undefined reference to `route::addCity(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
main.cpp:(.text+0x1227): undefined reference to `graph::addRoute(route)'
collect2: ld returned 1 exit status



[user@server proj2]$ g++ main.o city.o graph.o main.o -o proj2
g++: main.o: No such file or directory
g++: city.o: No such file or directory
g++: graph.o: No such file or directory
g++: main.o: No such file or directory
g++: no input files

0
 
LVL 1

Author Comment

by:ubuntuguy
ID: 23692722
this is my makefile

all: proj2

proj2: city.o route.o graph.o main.o
        g++ -o proj2 city.o route.o graph.o main.o


city.o: city.cpp city.h
 g++ -c city.cpp

route.o: route.cpp route.h
 g++ -c route.cpp

graph.o: graph.cpp graph.h
 g++ -c graph.cpp

main.o: main.cpp
 g++ -c main.cpp
all: proj2
0
 
LVL 43

Expert Comment

by:ravenpl
ID: 23694768
Either try my commands (dannykurniawan's are incorrect) or the makefile, it looks fine.

just copy the makefile and four .cpp files into same folder, then simply run
# make
from the directory
0
 
LVL 1

Author Comment

by:ubuntuguy
ID: 23696432
I reworked my makefile so it runs on linux...

all : project2
 
 project2 : city.o route.o graph.o main.o
     g++ city.o route.o graph.o main.o -o project2
 
 main.o: main.cpp
     g++ -c -o main.o main.cpp
 
 graph.o: route.cpp
     g++ -c -o graph.o graph.cpp
 
 
 route.o: route.cpp
     g++ -c -o route.o route.cpp
 
 city.o: city.cpp
     g++ -c -o city.o city.cpp
 
 clean:
     rm -f city.o route.o graph.o main.o project2


********************************************************************
what's this error that I get now?


[user@server proj2]$ make
makefile:4: *** missing separator.  Stop.


0
 
LVL 53

Expert Comment

by:Infinity08
ID: 23696568
>> makefile:4: *** missing separator.  Stop.

You need to make sure that there is a tab character before the commands (and not a few spaces for example).

So, for example, this line :

>>     g++ city.o route.o graph.o main.o -o project2

has to start with a tab character, not a bunch of spaces.
0
 
LVL 2

Expert Comment

by:dannykurniawan
ID: 23697125
sorry... my mistake... I haven't try to compile it, since I don't have GCC...
here's what it should be:

g++ -c route.cpp -o route.o
g++ -c city.cpp -o city.o
g++ -c graph.cpp -o graph.o
g++ -c main.cpp -o main.o
g++ main.o city.o graph.o main.o -o proj2

0
 
LVL 1

Author Comment

by:ubuntuguy
ID: 23697138
Thanks infinity08.  That fixed it.  Now all my classes compile with the makefile.

[user@server proj2]$ make
g++ -c -o city.o city.cpp
g++ -c -o route.o route.cpp
g++ -c -o graph.o graph.cpp
g++ -c -o main.o main.cpp
g++ city.o route.o graph.o main.o -o proj2
 ----------------------------------------------------------------------------------------
[user@server proj2]$ ls
city.cpp  city.o     graph.h  main.cpp  makefile  readme.txt  route.h  test.txt
city.h    graph.cpp  graph.o  main.o    proj2     route.cpp   route.o
----------------------------------------------------------------------------------------------

How do I run the program now?


0
 
LVL 1

Author Comment

by:ubuntuguy
ID: 23697809
thanks all... i got it working.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 23698954
>> How do I run the program now?

I assume you already found out, but just for the sake of completeness :

        ./proj2
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand column That will then direct you to their download page. From that p…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question