ใช้งาน gcc ที่มากับ Rtools ใน Mathematica

ใน Rtools จะมี compiler ของ gcc (mingw) มาด้วยแล้วทั้งที่เป็นแบบ 32 bits และ 64 bits หากอยากจะเอาไปใช้ใน Mathematica ก็ต้องเรียกผ่าน CCompilerDriver`GenericCCompiler` ครับ แล้วเพียงเซ็ต Path ของ gcc จาก Rtools นี้ให้ถูก เช่น

Needs["CCompilerDriver`GenericCCompiler`"]

ทดลองเรียกใช้งาน

greeter = CreateExecutable[StringJoin[
 "#include <stdio.h>\n",
 "int main(){\n",
 " printf(\"Hello MinGW-w64 world.\\n\");\n",
 "}\n"],
 "helloworld", "Compiler" -> GenericCCompiler, 
 "CompilerInstallation" -> "C:/Rtools/mingw_64", 
 "CompilerName" -> "x86_64-w64-mingw32-gcc.exe"]

Import["!\""<>greeter<>"\"","Text"]
Hello MinGW-w64 world.

ที่นี้ถ้าอยากจะให้ Mathematica มันเรียกใช้เจ้า mingw 64 จาก Rtools นี้ตลอด อย่างเช่นคำสั่ง Compile ก็สามารถทำได้โดยเซ็ตค่าที่มันเกี่ยวข้องอย่างเช่น Path กับตัวแปร $CCompiler ได้เลยครับ

$CCompiler = {"Compiler" -> GenericCCompiler, 
 "CompilerInstallation" -> "C:/Rtools/mingw_64", 
 "CompilerName" -> "x86_64-w64-mingw32-gcc.exe"};

f = Compile[{x, y}, Sqrt[x^2 + y^2], CompilationTarget -> "C"]

Table[{x, f[x, 5/4 x]}, {x, 0, 6, 0.5}]

 

เขียน C/C++ ใน Wolfram Workbench

Wolfram Workbench (2.0) นี่ถูกพัฒนามาจาก Eclipse ซึ่งโปรแกรมเสริมหรือ plugin อะไรต่างๆที่ถูกพัฒนามาสำหรับ Eclipse สามารถใช้ได้กับ Wolfram Workbench ครับ ซึ่งเจ้า Wolfram Workbench 2.0 นี้สามารถใช้ได้กับโปรแกรมเสริมของ Eclipse ตั้งแต่ 3.4 จนถึง 3.8 เลยครับ (http://www.wolfram.com/products/workbench/qa/)

ถ้าจะอยากจะใช้ Wolfram Workbench  เป็น IDE สำหรับเขียน C/C++ ก็สามารถทำได้โดยเพิ่ม C/C++ development tools (CDT) ของ Eclipse เข้าไปใน Workbench ตามนี้ครับ

cdt1

1. ไปที่ Help->Install New Software

cdt2

2. คลิ๊ก Add แล้วพิมพ์ข้อความตามภาพด้านบนครับ

cdt3

3. จากนั้นก็เลือก CDT Main Features แล้วก็ Next ไปเรื่อยๆ  เป็นอันเสร็จพิธี 🙂

เจ้า CDT นี้ไม่ได้ติดตั้ง C/C++ compiler มาด้วยนะครับ เราต้องลงเอง ซึ่งผมแนะนำ MinGW  ครับ สามารถโหลดตัวติดตั้งได้ที่ https://sourceforge.net/projects/mingw/files/Installer/mingw-get-inst/

การติดตั้ง MinGW นี้ก็เพียง double clicks ที่ตัวติดตั้งแล้วก็ตอบคำถามไปเรื่อยๆ ซึ่งค่า default มันจะทำการติดตั้งตัว compiler ไว้ที่ C:\MingGW ครับ  เมื่อลงเสร็จเราก็เพิ่ม C:\MinGW\bin เข้าไปใน Path ครับ ดูรายละเอียดเพิ่มเติมได้ที่ http://mingw.org/wiki/Getting_Started

cdt4

เป็นอันเสร็จครับ 🙂

 

%d bloggers like this: