DemiSheep
DemiSheep

Reputation: 698

How can I get my C++ 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:

(The post is not showing my tabs in my makefile but they're there!)

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.

Here is my issue:

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>

(The post is not showing my tabs in my makefile but they're there!)

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 toEmployee::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 toEmployee::setFirstName(std::basic_string, std::allocator >)' lab1.cpp:(.text+0x589): undefined reference to Employee::setId(int)' lab1.cpp:(.text+0x5ce): undefined reference toEmployee::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 toEmployee::setPhoneNumber(std::basic_string, std::allocator >)' 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 toEmployee::setHireDate(std::basic_string, std::allocator >)' lab1.cpp:(.text+0x7c6): undefined reference to `Employee::setEmailAddress(std::basic_string, std::allocator >)' collect2: ld returned 1 exit status make: * [main] Error 1

Any help would be greatly appreciated!

Upvotes: 0

Views: 1882

Answers (2)

Joe
Joe

Reputation: 42666

Your makefile for your second project only compiles lab1.cpp and not your other cpp files. You have two others (Employee.cpp and LinkSortedList.cpp) that you need to account for.

Also, please learn how to properly format code here on this site. https://stackoverflow.com/editing-help

Upvotes: 3

NPE
NPE

Reputation: 500873

You have to compile Employee.cpp and link it into the executable:

g++ -o LSL lab1.cpp Employee.cpp

It could be that the same goes for LinkedSortedList.cpp (its purpose is not entirely clear to me).

Upvotes: 4

Related Questions