Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I really need help with my makefile. Every time I attempt to use the command \"r

ID: 669901 • Letter: I

Question

I really need help with my makefile. Every time I attempt to use the command "run test1.x" I receive this error:

(CC) -otest1.cpp product.o
/bin/sh: -c: line 0: syntax error near unexpected token `-otest1.cpp'
/bin/sh: -c: line 0: `(CC) -otest1.cpp product.o'
makefile:13: recipe for target 'test1.x' failed
make: *** [test1.x] Error 1

These are the files I am working with:

File: makefile

CPP = $(HOME)/cpp


#CC = g++ -std=c++11 -Wall -Wextra -I. -I$(CPP)
CC = clang++ -std=c++11 -I. -I$(CPP) -Weverything -Wno-old-style-cast
CCC = clang++ -std=c++11 -I. -I$(CPP) -Weverything -Wno-old-style-cast -Wno-sign-conversion

# -Weverything -Wno-old-style-cast

all: product.x test1.x test2.x

test1.x: product.o
$ (CC) -otest1.cpp product.o

test2.x: product.o
$(CC) -otest2.cpp product.o

product.x: product.o
$(CC) -oproduct.x product.o

product.o: product.h product.cpp
$(CC) -c product.cpp

File: product.cpp

/*
product.cpp
*/

#include"product.h"
#include <cstring>

void Product::SetName    ( const char* name )
{
if (name_ != NULL)
  delete [] name_;
size_t size = strlen(name);
name_ = new char [1+size];
name_[size] = '';
strcpy(name_, name);
}

void Product::SetBarCode ( uint32_t code )
{
code_ = code;
}

void Product::SetCost    ( float cost )
{
cost_ = cost;
}

const char* Product::GetName    () const
{
return name_;
}

uint32_t Product::GetBarCode () const
{
return code_;
}

float Product::GetCost    () const
{
return cost_;
}

Product::Product() : name_(NULL), code_(0), cost_(0.0)
{
name_ = new char [2];
name_[0] = '#';
name_[1] = '';
}

Product::Product(const char* name, uint32_t code, float cost) : name_(NULL), code_(code), cost_(cost)
{

size_t size = strlen(name);
name_ = new char [1+size];
name_[size] = '';
strcpy(name_ , name);

}

Product::~Product()
{
if (name_ != NULL)
  delete[] name_;
}

Product::Product (const Product& p) : name_(NULL), code_(p.code_), cost_(p.cost_)
{


size_t size = strlen(p.name_);
name_ = new char [1+size];
name_[size] = '';
strcpy(name_ , p.name_);

}

Product& Product::operator= (const Product& p)
{
if (this != &p)
{
  if (name_ != NULL)
   delete [] name_;
  size_t size = strlen(p.name_);
  name_ = new char [1+size];
  name_[size] = '';
  strcpy(name_ , p.name_);

}
return *this;
}

std::ostream& operator<< (std::ostream& os, const Product& p)
{
os << p.GetName() << ' '
  << p.GetBarCode() << ' '
  << p.GetCost ();

return os;
}


File: product.h

/* product.h */
#include <iostream>
#include <cstdlib>
#include <cstdint>

#ifndef _PRODUCT_H
#define _PRODUCT_H

class Product
{
public:
void        SetName    ( const char* ); // sets the name field
void        SetBarCode ( uint32_t );    // sets the bar code field
void        SetCost    ( float );       // sets the cost field
const char* GetName    () const;        // returns a const pointer to the name field
uint32_t    GetBarCode () const;        // returns the bar code by value
float       GetCost    () const;        // returns cost by value

Product(); //name "#" code = 0 cost =0
Product(const char* name, uint32_t code, float cost);
~Product();
Product (const Product& p);
Product& operator= (const Product& p);

private:
char *   name_; // the product name
uint32_t code_; // the product bar code
float    cost_; // the product cost

};

std::ostream& operator<< (std::ostream& os, const Product& p);

#endif

File: test1.cpp THIS FILE CANNOT BE MODIFIED!

/**
test.cpp

test harness for class Product
*/

#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <product.h>
// #include <product.cpp> // in lieu of makefile

const size_t arraySize = 10;
const size_t numDigits = 2;

Product CopyCheck (Product p);
void AssignCheck (const Product& pIn, Product& pOut);

Product CopyCheck (Product p) // pass in by value calls CC
{
Product x(p); // initialization calls CC (NOT assignment!)
return x; // return by value calls CC
}

void AssignCheck (const Product& pIn, Product& pOut) // pass in by reference - no copies made
{
pOut = pIn; // calls assignment (not CC)
}

int main()
{
Product p1("hammer", 0xFFFFFFFF, 15.00), p2;
std::cout << " Products after declaration: ";
std::cout << " p1 = " << p1 << ' ';
std::cout << " p2 = " << p2 << ' ';

p1.SetName("Copy Checker");
p1.SetCost(10.0);
p2.SetName("Assign Checker");
p2.SetCost(20.0);
std::cout << " Products after Set: ";
std::cout << " p1 = " << p1 << ' ';
std::cout << " p2 = " << p2 << ' ';

Product p3 = CopyCheck(p1);
std::cout << " Products after p3 = CopyCheck(p1): ";
std::cout << " p1 = " << p1 << ' ';
std::cout << " p3 = " << p3 << ' ';

AssignCheck(p2, p3);
std::cout << " Products after AssignCheck(p2,p3): ";
std::cout << " p2 = " << p2 << ' ';
std::cout << " p3 = " << p3 << ' ';

Product p4 ("Transitive Assignment Check", 50, 25.0);
p1 = p2 = p3 = p4;
std::cout << " Products after p1 = p2 = p3 = p4: ";
std::cout << " p1 = " << p1 << ' ';
std::cout << " p2 = " << p2 << ' ';
std::cout << " p3 = " << p3 << ' ';
std::cout << " p4 = " << p4 << ' ';

Product * parray = new Product [arraySize];
std::cout << " Product Array after declaration: ";
for (size_t i = 0; i < arraySize; ++i)
{
std::cout << " p[" << std::setw(numDigits) << i << "] = " << parray[i] << ' ';
}

for (size_t i = 0; i < arraySize; ++i)
{
parray[i].SetName("Titanium Hammer");
parray[i].SetBarCode(static_cast<uint32_t>(17 + i));
parray[i].SetCost(static_cast<float>((2*17 + i))/2);
}
std::cout << " Product Array after Set: ";
for (size_t i = 0; i < arraySize; ++i)
{
std::cout << " p[" << std::setw(numDigits) << i << "] = " << parray[i] << ' ';
}

// */
}

I think the error lies with my makefile and I feel like it is just a minor tweak. Any idea what is going wrong? Any help is really appreciated.

Explanation / Answer

test1.x: product.o
$ (CC) -test1.cpp product.o

test2.x: product.o
$(CC) -test2.cpp product.o

product.x: product.o
$(CC) -oproduct.x product.o

product.o: product.h product.cpp
$(CC) -c product.cpp