Compiling mexw32 (mex for windows) files using Eclipse CDT IDE (MinGW GCC compiler)
Considering the trouble I had to go through to figure out how to do this (and in the end, it was embarassingly simple), I thought I'd post here how I managed to compile a mexw32 file using the Eclipse CDT IDE (managed make files). This is both for my own reference as well as to help anyone who may be googling for this information.
So, anyway, firstly I had to setup Eclipse CDT (Eclipse started off as a Java IDE, but the CDT plugin enables C/C++ programming). The IDE itself does not come with a compiler/linker/debugger, so I used the free GCC compiler. Setup instructions can be found here, at a how-to I found while googling, explained much much better than I ever could expect to do myself.
Anyway, I'll skip over the normal stuff about creating a project/importing existing files, and stuff like that, to the important part for this post, how to compile Matlab-compatible MEX files?
In the Matlab help documentation on the subject, there's a whole bunch of instructions for generic compilation, most of which I do not yet understand (having been spoilt on Visual Studio for ages) and sort of guessed at. There's also specific instructions there for Visual Studio, which I have been using so far, but those instructions aren't applicable for Eclipse CDT.
So, anyway, on to the real meat. Assuming you have a .c or .cpp file containing your code (for testing, I copied the timestwoalt.c file from Matlab examples), create a project with whatever name you want (in my case, the name was TestMatlab) and import the file (this creates a copy, not affecting your original). This is done by right-clicking within the Project Explorer and selecting Import->General->File System and browsing to the correct file.
Now, the correct settings need to be input for compilation and linking. These settings are project specific, so right click on your project (for me it was TestMatlab) in the Project Explorer and click properties (right at the bottom). Under the C/C++ Build folder, click on the Settings sub-folder.
Firstly, we have to ensure that we're doing setup for all configurations. At the top there's a drop-down named Configuration, select [All Configurations] there.
To select what type of output we want, go to the Build Artifact tab. The artifact type should be 'Shared Library', the name defaults to your project name (any name is fine, really), the artifact extension should be modified to "mexw32" (without the inverted commas), and I normally set the prefix to blank instead of lib.
Next, compiler settings. Select the 'Tool Settings' tab. There should be 4 folders listed, GCC Assembler, GCC C++ Compiler, GCC C Compiler, and MinGW C++ Linker. If you don't have the linker, ensure that you have selected 'Shared Library' in the previous step.
First, setup a required define for both C++ and C Compilers. For C++, under 'Preprocessor', add MATLAB_MEX_FILE as a defined symbol. For C, this goes under the 'Symbols' selection.
Next, for both C++ and C, select 'Directories', and add the following include path "C:\MATLAB\R2007b\extern\include". Obviously, modify your include path based on your MATLAB install directory.
Next, go to MinGW C++ Linker and select 'Libraries'. Under 'Libraries' (-l) input the following :- 'mex', 'mat', and 'mx'. The Matlab Help says to link against libmex.dll, libmat.dll, and libmx.dll, but the lib prefix is automatically inserted by the MinGW linker. Under 'Library search path (-L) add the following path:- "C:\MATLAB\R2007b\bin\win32" instead of the \extern\lib\win32\microsoft specified for Visual Studio in the Matlab Help (once again, replace according to your MATLAB install folder).
The final step (and my mex files run without this step, but I think it may be important) is to select the 'Shared Library Settings' subfolder under MinGW C++ Linker and adding the DEF file name "timestwoalt.def" or whatever your def file will be named.
Once these settings are done (remember to click Apply just to be sure), you need to create/add 2 files to your project. Right click on the project and click Import. Import "C:\MATLAB\R2007b\extern\include\mexversion.rc" into your project. Also create a new file "right-click on project->New->File" named timestwoalt.def, or whatever you specified previously. The def file should contain the following two lines:-
LIBRARY TestMatlab.mexw32 EXPORTS mexFunction
Obviously, replace TestMatlab.mexw32 with the name of your project.
And that's it. You can try and build your project and test the resulting mexw32 file out in Matlab. Now to try and integrate OpenCV....
edit: which didn't turn out so hard. Followed some online instructions, simple enough that I'll just copy-paste here.
You can setup Eclipse CDT to work with the OpenCV libraries.
Create a new C++ project in Eclipse CDT. Select MinGw as the toolchain.
In the project properties, go to the C/C++ Build->Settings->GCC C++ Compiler, set the directories to:
Julian Chew is a bright young prospect. He's been blessed with a middle-class family and an urban upbringing in PJ. Not exactly a very RICH family, so he didn't go to international schools for his primary and secondary education. But not a very POOR family either, so he was free to skip form 6, head to Taylors for his pre-u, do a twinning course which gave him a year in the UK, and graduate with a "University of London" business degree. He's fluent in English, having spoken it both at home and at school all his life. He's conversant in Malay as the national language, Cantonese as a born-and-bred KL-ian, Mandarin from the soap operas his grandma likes him to watch with her, Hokkien (the southern variety) from family reunions. He's Chinese, and his maths/logic skills are pretty far above average.
Pretty much a solid base, then. The world is his oyster, as they say.
Then he gets his first job. He's got an overseas degree, and the market never has enough of such people, especially when they are conversant in so many languages and dialects. He was a bit worried, having read about 80,000 unemployed graduates, but came to realize most of those were educated in sub-standard environments and not very interested in working hard anyway.
His job pays pretty well. 2.3k starting pay, pretty good benefits. Yet getting his first car means upkeep payments, in addition to the loan repayments. He's filial, gives some of his salary to his parents. He's searching for an apartment in central KL to beat the jam, that's going to cost him. His peers are all working adults, and meal costs have suddenly shot to a level he would have balked at even in London. He's having to consider life insurance and medical insurance, more bills at the end of the month.
And so he needs more money. Yet that's not a problem. He works hard, and he's very capable. Soon he finds his boss giving him a promotion, with accompanying pay benefits. Within 2 years, even though he kinda likes the company he's at, a head-hunter offers him almost double the salary at another company, and he can't really turn down half a dozen K per month. So he jumps company. More work, but the pay is worth it. He's young, and there's lots of time left, so he invests it into something useful. His future, or at least his financial future.
About a decade or so on, and somehow he's found the time to get married to his long-time girlfriend. She doesn't really mind his hours, as she keeps similar hours as well. He's heard of friends who, in spending most of their time at the office, developed clandestine relationships with female colleagues. But he's better than that, and he DOES have a wife to go home to at night anyway. Most nights.
By now he's in a managerial position, pretty high up. Cuts some pretty sharp deals, catches the CEO's eye. Yet he's only one a half a dozen born leaders at his level. He needs to stand out, so he starts his 'campaigning'. He makes sure he's involved in the highest-profile projects, and he does what he has to do to ensure they succeed. There's a lot of backbiting and sabotaging at this level, but he decides to rise above it all. He takes the setbacks as they come and doesn't retaliate. His reason? He has a young son at home, and he wouldn't want the son to find out years later he was playing crooked. But he's not stupid. He makes sure the CEO 'finds out' what's going on behind the scenes. And so the CEO's respect for him increases.
He's a rising star. Business trade magazines have heard of him, he's mentioned by heads of different companies as someone they've heard good things of. And his earnings have increased to the point that his wife has left her job to raise their two sons. When he gets back, he hears how they've been doing, from her. It was difficult for her to give up working, initially, but she's been understanding. He tries his best to keep up-to-date on his sons. Manages to make it to their school events, at least once a year. Once in a while he tries to get home just that bit earlier, to eat dinner with them.
He's out of the business world, now. Has enough money, if there's ever such a thing. He's invested in almost every type of investment there is, and some of his friends say his investments are so diverse, half the planet could blow up and he'd still be able to retire gracefully to an island somewhere. Money is just money, after all. Yet he's still relatively young, just crossing the midpoint of his 50s.
He's into politics, now. There's an opposition party with plenty of potential, and the global economic climate means he understands that power is likely to shift. That most people vote opposition when the going gets tough. And his reputation as a business leader means his name carries weight. Candidates listen attentively to his suggestions. He's almost like a network coordinator, connecting those rising into power with those who need an audience with those in power. His wife is always there beside him as he attends the important functions, the dinner parties. His two sons are off in the US studying. They call home, like all filial children, about once a month. Doing well, as well, they've inherited the sharp minds of both parents.
He's getting old. The sons are well-established. The health problems are starting, but he has the best treatment money can buy. His name is in the newspapers regularly. The unkind label him the 'puppet master' for the new government, but most concede that he's earned his position by merit, unlike those before him. He's built relationships with the powerful at the right time, showed allegience to the right people. His wife is spending most of her time nowadays with a group of like-minded ladies, wives of important and powerful men. And so his days are spent in comfort. Alone.
It was his funeral yesterday. Many spoke well of him. Very well, in fact, especially as his sons were listening. His own sons spoke of how their dad has been an example to them of the value of hard-work, the importance of being driven with a goal. Both are following in his footsteps, one as a businessman, one as a lawyer. Many are those who remember him fondly, who say "Now there was a man who achieved greatness". Political leaders, business leaders, were at the front rows.
Yet consider the fact that no real tears of sadness were shed at his passing.
Consider the fact that the sons did not, could not speak of a dad who loved them.
Consider the fact that many people respected him, feared him, yet none truly loved him.
Consider the fact that his own wife would spend their waning years socializing rather than together with him, enjoying the fruits of a well-built relationship.
Was Julian a successful man? The answer must be a resounding yes! He's made the most of his life.
Or has he really?
Jesus was tempted to turn stones to bread. To use His power to convert what was common to something filling to the body. What can be more common than time, especially when you're young? What fills you up more than money?
Jesus was tempted to throw Himself off a temple rooftop. To show to the whole watching crowd and nation that someone special is here. Who can resist the lure of a good name, of being honoured by all and sundry?
Jesus was tempted with all the kingdoms of the world. Ultimate power. And there is no power greater than political power over a nation. Is there?
Jesus responded to each temptation with words God had spoken.
"Man shall not live by bread alone, but by every word that comes from the mouth of God" "Do not put the Lord your God to the test" "Worship the Lord your God, and serve Him only"
Its been 2000 years, yet we face the same things He did. And we give in, where He did not. We obtain for ourselves every good thing, but something is missing.
Haven't had lots of free time recently. So much for taking Masters providing me with flexi-hours, still have a lot of work to do. Oh well, that's life.
Anyway, this blog-post is quite well-written, by an intelligent young lady who's probably going to take Medicine but in a just world would be some type of writer (and not disadvantaged by the fact). Check it out.
The STAR this week is so much more interesting than during campaigning period. In those times it really looked a bit like a BN newsletter and/or campaign flyer (albeit a thick one). There's still plenty of perceived bias (perceived cos its just my view), such as how the Perak MB situation is making headlines while Terengganu and Perlis are not. Yet post-elections there's been a bit more even coverage, such as the following two articles.
Barisan will act as a watchdog, says Khir
By DHARMENDER SINGH <dharam@thestar.com.my>
SHAH ALAM: The state Barisan Nasional will act as watchdog to ensure that the three parties forming the state government delivered on election promises as stipulated in their manifestoes, said former Selangor Mentri Besar Datuk Seri Dr Mohamad Khir Toyo.
Breaking his silence for the first time since the recently concluded general election, Dr Khir said, “I hope the three parties (PKR, DAP and PAS) will take the best care possible of the people and this beautiful and good state, after receiving the mandate from the people.
“We will work together with the state government if the projects benefit the people and are carried out transparently.”
He was speaking at a press conference yesterday at the Selangor Umno building here.
Dr Khir, who left the building on Saturday without talking to the press after learning of Barisan's defeat in the state, had cancelled several scheduled press conferences before last night.
He said Barisan would register all pre-schoolers, senior citizens, single mothers and students so that they could receive all the benefits such as free education, allowances and healthcare that were promised.
Asked about rumours that the state Barisan had tried to woo PAS assemblymen to gain a simple majority, Dr Khir said this was not true.
Commenting on claims that Barisan had tried to destroy documents kept at the state secretariat building, Dr Khir said these were just rumours and that he had always been transparent and all the papers were in order.
“We have nothing to hide,” he said.
taken from The Star, March 12 (Wednesday)
Call to scrap mega projects
PENANG: The state Umno will “persuade” the Federal Government to scrap all the mega projects in the state in view of the people’s clear rejection of Barisan Nasional.
Since Penangites do not want development, the Federal Government should not force it on them, said state Umno liaison committee secretary Datuk Azhar Ibrahim.
He said that this was not Penang Umno taking revenge “but we are actually giving the people what they want”.
“They voted for change, so let us see what the DAP can do,” he said after the party’s emergency meeting at Menara Umno yesterday.
Azhar, who is Penaga assemblyman, was appointed the state Opposition Leader at the meeting.
Meanwhile, Umno liaison committee deputy chief Datuk Abdul Rashid Abdullah hit out at the new administration for its decision not to practise the New Economic Policy (NEP).
Taken from The Star, March 14 (Friday)
Any kid can identify the sheer childishness in the response of these two men. In the first article, Khir Toyo is simply being malicious, to try and overload the PUBLIC healthcare delivery system by convincing those without need to just show up at the hospitals. And that's the keyword there, the PUBLIC healthcare system. In the second article, Penang Umno's Azhar is advocating the cancellation of all mega-projects (read, road construction, second Penang bridge, development of zoned areas), not because they are wasteful, but because "Penangites do not want develoment". Again, advocating that projects supposedly (and I'm aware there's arguments against this) for the PUBLIC be scrapped, simply because BN has lost in Penang. Even though Penang UMNO was never the main component in Penang's pre-GE government.
If you want to be a people's representative, do it. Fight for things that benefit the people. Otherwise you reveal yourself to be just another power-hungry Little Napoleon, upset at being dislodged from your position of priviledge. The BN and the BA/BR (Barisan Alternatif/Barisan Rakyat) are both supposedly intent on helping Malaysians. Prove it, then. Because Malaysians are not so stupid as to forget that you've called for their public healthcare system to become a joke, for projects in their neighbourhoods to be cancelled, all because you're pissed you've lost an election.
Grow up, please.
P.S. - Though I'm only pulling out the negative comments of a few BN representatives, there are others who say the right thing, speak about reform and listening more closely to the people. Empty words they may be, but they're the RIGHT empty words. Special mention to Wanita UMNO and strong-lady Rafidah Aziz (oddly enough).