Home>

I want to compile multiple c ++ files

I have three files Date.h, Date.cpp and Datetest01.cpp, and when I try to compile them together using makefile, I get the following error. Can you tell me what caused it?

Undefined symbols for architecture x86_64:
ld: symbol (s) not found for architecture x86_64
collect2: error: ld returned 1 exit status
make: *** [Date1] Error 1


The object file can be created with makefile, but it was not possible to create the executable file Date1 that bundles them.

code

Date.h

#include<iostream>
class Date
{
    int y;
    int m;
    int d;
public:
    Date ();
    Date (int yy, int mm = 1, int dd = 1);
    int year () const {return y;}
    int month () const {return m;}
    int day () const {return d;}
    Date preceding_day () const;
    std :: string to_string () const;
};
std :: ostream&operator<<(std :: ostream&s, const Date&x);
std :: istream&operator >>(std :: istream&s, Date&x);


Date.cpp

#include<ctime>
#include<sstream>
#include<iostream>
#include "Date.h"
using namespace std;
Date :: Date ()
{
    time_t current = time (NULL);
    struct tm * local = localtime (&current);
    y = local->tm_year + 1900;
    m = local->tm_mon + 1;
    d = local->tm_mday;
}
Date :: Date (int yy, int mm, int dd) {
    y = yy;
    m = mm;
    d = dd;
}
Date Date :: preceding_day () const
{
    int dmax [] = {31, 28, 31, 30, 31, 31, 30, 31, 30, 31};
    Date tmp = * this;
    if (tmp.d>1) tmp.d-;
    else {
        if (--tmp.d<1) {
            tmp.y-;
            tmp.m = 12;
        }
        tmp.d = dmax [tmp.m --1];
    }
    return tmp;
}
string Date :: to_string () const
{
    ostringstream s;
    s<<y<<"year"<<m<<"month"<<d<<"day";
    return s.str ();
}


Datetest01.cpp

#include<iostream>
#include "Date.h"
using namespace std;
int main ()
{
    Date today;
    cout<<"Today is"<<today<<"\ n";
    cout<<"Yesterday is"<<today.preceding_day ()<<"is \ n";
}


makefile

TARGET = Date1
SRCS = Date.cpp Datetest01.cpp
OBJS = $(SRCS: .cpp = .o)
CC = g ++
CFLAG = -Wall
$(TARGET): $(OBJS)
    $(CC) -o [email protected] $^
$(OBJS): $(SRCS)
    $(CC) -c $^
all: clean $(OBJS) $(TARGET)
clean: clean:
    -rm -f $(OBJS) $(TARGET) * .d
What I tried

I got the same error message when I typed g ++ -o Date1 Date.o Datetest01.o directly into the terminal without using the makefile.