C++代码使用.cpp源编译,但不是.c

Jac*_*rry 0 c++ file-extension visual-studio-2008

在Visual Studio 2008中,使用C++,我尝试使用http://msdn.microsoft.com/en-us/library/ms235636.aspx上的说明构建DLL ,除了我命名了一个扩展名为"."的源文件. c"而不是建议的".cpp".

扩展名为.c时,编译器会抛出37个错误.扩展名为.cpp,DLL构建成功.

源文件的扩展有何不同?

这是完整的代码:

// MathFuncsDll.cpp
// compile with: /EHsc /LD

#include "MathFuncsDll.h"

#include <stdexcept>

using namespace std;

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 new invalid_argument("b cannot be zero!");
        }

        return a / b;
    }
}




// MathFuncsDll.h

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

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

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

        // Returns a / b
        // Throws DivideByZeroException if b is 0
        static __declspec(dllexport) double Divide(double a, double b);
    };
}
Run Code Online (Sandbox Code Playgroud)

Ry-*_*Ry- 8

当扩展名为时.c,编译器将其编译为C.如果是.cpp,则将其编译为C++.