How can I get my program to compile on Unix?

I am working on a school project in C++ and am using our schools unix server to write it as instructed by the professor. I am using vim and g++ to compile.

This project is built off another project, Lab0 which I had no trouble getting to build after some help from the TA, but had to do some weird stuff to get it to build (#include LinkedSortedList.cpp file at the bottom of LinkedSortedList.h). Everyone in the class did this weird stuff with #include .cpp file.

First, here are the files and what #includes they have for Lab0 which is compiling fine:


(Tabs in my make file are not showing up in my post....!)

makefile:

main: *.cpp *.h

g++ -o LSL main.cpp

clean:

rm -f *.o LSL*



Lab0 (The one that builds), is like this:

Files:

main.cpp (NOT Templated):
#include "LinkedSortedList.h"
#include <iostream>
using namespace std;

SortedList.h (Templated):
Nothing

LinkedNode.h (Templated):
#include <iostream>
using namespace std;

LinkedSortedList.h (Templated):
#include "SortedList.h"
#include "LinkedNode.h"
#include <iostream>
using namespace std;

#include "LinkedSortedList.cpp" - At the bottom fo this file above the #endif to get the program to compile from what the TA told me to do for lab0 due to the templated class.

LinkedSortedList.cpp (Templated):
Nothing

No problems building and running this project.


Below is lab1 the one I am having trouble with and Lab1 uses all the files from Lab0 just adds Employee.h and Employee.cpp.

Lab1 (The one that won't build) is like this:

Files:

lab1.cpp:
#include <iomanip>
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <stdexcept>
#include "Employee.h"
#include "LinkedSortedList.h"

SortedList.h (Templated):
Nothing

LinkedNode.h (Templated):
#include <iostream>
using namespace std;

LinkedSortedList.h (Templated):
#include "SortedList.h"
#include "LinkedNode.h"
#include "Employee.h"
#include <iostream>
using namespace std;

#include "LinkedSortedList.cpp" - At the bottom fo this file above the #endif to get the program to compile from what the TA told me to do for lab0 due to the templated class.

LinkedSortedList.cpp (Templated):
Nothing

Employee.h (NOT templated):
#include <iostream>
#include <sstream>
using namespace std;

Employee.cpp (NOT templated):
#include <stdio.h>
#include <string.h>


(Tabs in my make file are not showing up in my post....!)

makefile:

main: *.cpp *.h

g++ -o LSL lab1.cpp

clean:

rm -f *.o LSL*



Errors I get:

Here are the errors I am getting. It seems like the Employee.cpp/Employee.h files are not being seen. Any ideas??

unixserver:Lab1> make
g++ -o LSL lab1.cpp
/tmp/ccamnaqx.o: In function `createRecord()':
lab1.cpp:(.text+0x3fb): undefined reference to `Employee::Employee()'
lab1.cpp:(.text+0x477): undefined reference to `Employee::setLastName(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
lab1.cpp:(.text+0x4f2): undefined reference to `Employee::setFirstName(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
lab1.cpp:(.text+0x589): undefined reference to `Employee::setId(int)'
lab1.cpp:(.text+0x5ce): undefined reference to `Employee::setSalary(int)'
lab1.cpp:(.text+0x60e): undefined reference to `Employee::setDepartment(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
lab1.cpp:(.text+0x67c): undefined reference to `Employee::setPhoneNumber(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
lab1.cpp:(.text+0x6ea): undefined reference to `Employee::setAddress(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
lab1.cpp:(.text+0x758): undefined reference to `Employee::setHireDate(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
lab1.cpp:(.text+0x7c6): undefined reference to `Employee::setEmailAddress(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
collect2: ld returned 1 exit status
make: *** [main] Error 1

Any help would be greatly appreciated!
Last edited on
That TA told you to do what? O_o

The fix is actually quite simple. g++ didn't know that it needed to also compile your LinkedSortedList.cpp and Employee.cpp files and link with them. Thus, when it gets around to linking everything, it complains.

How you can tell it that you need to compile it at the same time as main.cpp is by adding LinkedSortedList.cpp and Employee.cpp to the end of your g++ call. Good luck!

Fine print: this isn't a kosher practice for larger projects. For those, you might want to compile each of your files into unlinked object files using separate g++ commands, after which you'd then link them together with one command. For a projects with fewer than five files, though, it's probably still okay.

Also, could you please use [code]/*Your code here!*/[/code] tags for your code? This way you won't lose your tabs. Also, for your program output, you can use [output]Output here.[/output] tags. Thanks!

-Albatross
Last edited on
Thank you thank you thank you! Sorry about the poor formatting, thanks also for the tips on the formatting tags.

For some reason I thought it would pick up all my .cpp files automatically and I just needed to list the main in the makefile.
Topic archived. No new replies allowed.