MathFuncs


==== makefile ====

TARGET=MyExecRefsAssembly
PHONY=all test clean mmm html
OBJS=$(TARGET).obj 
.SUFFIXES: .obj

all: $(TARGET).exe MathFuncsAssembly.dll

$(TARGET).exe: $(OBJS)
	link $(OBJS)

MathFuncsAssembly.dll: MathFuncsAssembly.obj
	link -DLL $<

# -FU :
# Forces the use of a file name as if it had been passed to the #using directive
$(TARGET).obj: $(TARGET).cpp MathFuncsAssembly.h MathFuncsAssembly.dll
	cl -c -clr -FUMathFuncsAssembly.dll $(TARGET).cpp 

# -LD :
#  Creates a dynamic-link library 
MathFuncsAssembly.obj: MathFuncsAssembly.cpp MathFuncsAssembly.h
	cl -c -clr MathFuncsAssembly.cpp 

#.cpp.obj:

test:
	./$(TARGET)


clean:
	-rm *.obj $(TARGET).exe *~ MathFuncsAssembly.dll


TEMPFNAME=000dev0clipboard.tmp
mmm:
	@echo ==== makefile ==== > $(TEMPFNAME)
	@echo >> $(TEMPFNAME)
	cat makefile >> $(TEMPFNAME)
	@echo >> $(TEMPFNAME)
	@echo ==== $(TARGET).cpp ==== >> $(TEMPFNAME)
	@echo >> $(TEMPFNAME)
	cat $(TARGET).cpp >> $(TEMPFNAME)
	@echo >> $(TEMPFNAME)
	@echo ==== MathFuncsAssembly.cpp ==== >> $(TEMPFNAME)
	@echo >> $(TEMPFNAME)
	cat MathFuncsAssembly.cpp >> $(TEMPFNAME)
	@echo >> $(TEMPFNAME)
	@echo ==== MathFuncsAssembly.h ==== >> $(TEMPFNAME)
	@echo >> $(TEMPFNAME)
	cat MathFuncsAssembly.h >> $(TEMPFNAME)
	@echo >> $(TEMPFNAME)
	cat $(TEMPFNAME) > /dev/clipboard
	cat /dev/clipboard
	rm $(TEMPFNAME)

html:
	cat /dev/clipboard | code2html > $(TEMPFNAME)
	cat $(TEMPFNAME) > /dev/clipboard
	rm $(TEMPFNAME)

==== MyExecRefsAssembly.cpp ====

// MyExecRefsAssembly.cpp
// compile with: /clr /FUMathFuncsAssembly.dll

using namespace System;

int main(array<System::String ^> ^args)
{
    double a = 7.4;
    int b = 99;

    Console::WriteLine("a + b = {0}",
        MathFuncs::MyMathFuncs::Add(a, b));
    Console::WriteLine("a - b = {0}",
        MathFuncs::MyMathFuncs::Subtract(a, b));
    Console::WriteLine("a * b = {0}",
        MathFuncs::MyMathFuncs::Multiply(a, b));
    Console::WriteLine("a / b = {0}",
        MathFuncs::MyMathFuncs::Divide(a, b));

    return 0;
}
==== MathFuncsAssembly.cpp ====

// MathFuncsAssembly.cpp
// compile with: /clr /LD

#include "MathFuncsAssembly.h"

namespace MathFuncs
{
    double MyMathFuncs::Add(double a, double b)
    {
        return a + b;
    }

    double MyMathFuncs::Subtract(double a, double b)
    {
        return a - b;
    }

    double MyMathFuncs::Multiply(double a, double b)
    {
        return a * b;
    }

    double MyMathFuncs::Divide(double a, double b)
    {
        if (b == 0)
        {
            throw gcnew DivideByZeroException("b cannot be zero!");
        }

        return a / b;
    }
}
==== MathFuncsAssembly.h ====

// MathFuncsAssembly.h

using namespace System;

namespace MathFuncs
{
    public ref class MyMathFuncs
    {
    public:
        // Returns a + b
        static double Add(double a, double b);

        // Returns a - b
        static double Subtract(double a, double b);

        // Returns a * b
        static double Multiply(double a, double b);

        // Returns a / b
        // Throws DivideByZeroException if b is 0
        static double Divide(double a, double b);
    };
}


$ make test
./MyExecRefsAssembly
a + b = 106.4
a - b = -91.6
a * b = 732.6
a / b = 0.0747474747474748