Recent Changes - Search:

Mcp

edit SideBar

Hello World Example

Staying true to tradition, here's a "hello world" example.
For the full API, see Managed C++ API Documentation.

Example

main.cpp (save as UTF-8 without BOM):

#include <mcp/mcp.h>

using namespace Mcp;

DEFINE_REF_CLASS(HelloWorld); // e.q. typedef gc_ptr<HelloWorld> HelloWorld_P
__ref_class HelloWorld : public virtual Object
{
public:
    HelloWorld(String_P name) : m_Name(name) { }
    // void Finalize() { } // finalizer for cleaning up native handles if necessary
    void Say() { Console::WriteLine(m_Name); }
private:
    String_P m_Name;
};

class Application
{
public:
    static void Main()
    {
        String_P s = u"Hello world © 2013 from MCP™";
        Console::WriteLine(s);

        HelloWorld_P hello = gcnew (HelloWorld)(u"Adam");
        hello->Say();

        Console::WriteLine(u"Press any key to exit.");
        Console::Read();
    }
};

int main()
{
    McpInitDeinit mcp;
    Application::Main();
    return 0;
}

Compiling and Linking

Compile and link in one step.

Linux:

mcp main.cpp -std=c++11 -I../boehm_gc/include -I../include/ -L../boehm_gc/lib -L../dist/Release/ -lgc -lmcprt -o hello_world

Cygwin:

mcp --cc1=g++plugin main.cpp -std=c++11 -I../boehm_gc/include -I../include/ -L../boehm_gc/lib -L../dist/Release/ -lgc -lmcprt -o hello_world.exe

Compile only.
main.o will be available for linking following a successful compile.

Linux:

mcp main.cpp -std=c++11 -I../boehm_gc/include -I../include/ -L../boehm_gc/lib -L../dist/Release/ -lgc -lmcprt -c

Cygwin:

mcp --cc1=g++plugin main.cpp -std=c++11 -I../boehm_gc/include -I../include/ -L../boehm_gc/lib -L../dist/Release/ -lgc -lmcprt -c

Output

Hello world © 2013 from MCP™
Adam
Press any key to exit.
Edit - History - Print - Recent Changes - Search
Page last modified on March 12, 2013, at 04:02 AM