Clang failing to compile code containing throw

I've been going through the book "Programming: Principles and Practice Using C++" by Bjarne Stroustrup, and in Chapter 9 he has code for a Date class throwing errors for invalid dates in the Date constructor:

1
2
3
4
5
  Date::Date(int yy, Month mm, int dd)
      :y(yy),m(mm),d(dd)
    {
      if (dd < 0) throw Invalid();
    }


Where Invalid is a class for exceptions. When I try to compile this file, I get the error:

1
2
3
4
5
6
Undefined symbols for architecture x86_64:
  "_main", referenced from:
     implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [main] Error 1


I've tried using the <stdexception> library and throwing an exception from that library, I get the same error. The code compiles fine when I comment out any code using the keyword "throw".

I'm using the Mac OS Command Line Dev Tools, and using make to compile the file. I haven't downloaded any new tools, it's just the basic Clang compiler that comes with the Dev Tools package. When I compile the code on a different computer, using the compiler in VS2017, it compiles fine. It also compiled fine using the G++ compiler in Mingw64, using Cmake in Windows. I don't think it's a compatibility issues because as long as I don't use the keyword "throw", I can compile the code and it works as intended on my Mac.

I'm a novice when it comes to configuring compilers and using Makefiles, so any guidance in the right direction would be very helpful, thank you.
Last edited on
http://www.eelis.net/iso-c++/testcase.xhtml
A testcase is said to reproduce the problem if, when we independently try to compile and run the testcase locally, we get the same compiler error (or our compiler's version of it) or erroneous program output specified in the testcase

A testcase consisting of randomly copy&paste'd bits of code that you thought were relevant can obviously not reproduce the problem.

The error says that you don't have a `main()' function.
I don't see `main()' in the code that you've posted. Quite simple, it seems.

Post a complete example.
Also, show the commands used to build.
I do have a main function, I thought that was enough code because if I comment out the if statement that is in line with the throw the code compiles normally.

This is the code I have for my date.h header file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <vector>
#include <cmath>
#include <string>
#include <iostream>
#include <stdexcept>
#include <limits>

namespace Chrono {
	enum class Month {
		jan=1, feb,mar,apr,may,jun,jul,aug,sep,oct,nov,dec
	};
	
	class Date {
	public:
	  class Invalid{};
		Date(int y, Month m, int d);
		int day() const { return d; }
		Month month() const { return m; }
		int year() const { return y; }
		
		void add_day(int n);
		void add_month(int n);
		void add_year(int n);

	private:
		int y;
		Month m;
		int d;
	};

	bool is_date(int y, Month m, int d);
	bool leapyear(int y);

	bool operator==(const Date& a, const Date& b);
	bool operator!=(const Date& a, const Date& b);

	std::ostream& operator<<(std::ostream& os, const Date& d);
	std::istream& operator>>(std::istream& is, Date& dd);

	int day_of_week(const Date& d);
	Date next_Sunday(const Date d);
	Date next_weekday(const Date& d);

}


And this is the code for my main.cpp file(I started defining the declared functions from the header file here, it's how it was explained in the exercise).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <vector>
#include <string>
#include <iostream>
#include "date.h"

namespace Chrono {
    Date::Date(int yy, Month mm, int dd)
      :y(yy),m(mm),d(dd)
    {
      if (yy < -1) throw Invalid();
    }
    
    int main() {
        return 0;
    }

}



The code is compiled with the command "make main" in the terminal. It's always worked before, but now I'm getting this linker error and it seems like everything is valid.
Place main() outside the namespace Chrono.
Wow, I feel dumb. Thank you, it's compiling now.
Topic archived. No new replies allowed.