Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 433659
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T20:11:46+00:00 2026-05-12T20:11:46+00:00

I’m trying to embed Python into a MATLAB mex function on OS X. I’ve

  • 0

I’m trying to embed Python into a MATLAB mex function on OS X. I’ve seen references that this can be done (eg here) but I can’t find any OS X specific information. So far I can successfully build an embedded Python (so my linker flags must be OK) and I can also build example mex files without any trouble and with the default options:

jm-g26b101:mex robince$ cat pytestnomex.c
#include <Python/Python.h>

int main() {
  Py_Initialize();
  PyRun_SimpleString("print 'hello'"); 
  Py_Finalize();
  return 0;
}
jm-g26b101:mex robince$ gcc -arch i386 pytestnomex.c -I/Library/Frameworks/Python.framework/Versions/2.5/include/python2.5 -L/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/config -ldl -lpython2.5
jm-g26b101:mex robince$ ./a.out
hello

But when I try to build a mex file that embeds Python I run into a problem with undefined symbol main. Here is my mex function:

#include <Python.h>
#include <mex.h>

void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray*prhs[])
{
    mexPrintf("hello1\n");
    Py_Initialize();
    PyRun_SimpleString("print 'hello from python'");
    Py_Finalize();
}

Here are the mex compilation steps:

jm-g26b101:mex robince$ gcc -c  -I/Applications/MATLAB_R2009a.app/extern/include -I/Applications/MATLAB_R2009a.app/simulink/include -DMATLAB_MEX_FILE  -arch i386 -I/Library/Frameworks/Python.framework/Versions/2.5/include/python2.5  -DMX_COMPAT_32 -O2 -DNDEBUG  "pytest.c"
jm-g26b101:mex robince$ gcc -O  -arch i386 -L/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/config -ldl -lpython2.5 -o  "pytest.mexmaci"  pytest.o  -L/Applications/MATLAB_R2009a.app/bin/maci -lmx -lmex -lmat -lstdc++
Undefined symbols:
  "_main", referenced from:
      start in crt1.10.6.o
ld: symbol(s) not found
collect2: ld returned 1 exit status

I’ve tried playing around with arch settings (I added -arch i386 it to try and keep everything 32bit – I am using the python.org 32 bit 2.5 build), and the order of the linker flags, but haven’t been able to get anywhere. Can’t find much online either. Does anyone have any ideas of how I can get this to build?

[EDIT: should probably add I’m on OS X 10.6.1 with MATLAB 7.8 (r2009a), Python 2.5.4 (python.org) – I’ve tried both gcc-4.0 and gcc-4.2 (apple)]

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T20:11:46+00:00Added an answer on May 12, 2026 at 8:11 pm

    I think I found the answer – by including the mysterious apple linker flags:

    -undefined dynamic_lookup -bundle
    

    I was able to get it built and it seems to work OK. I’d be very interested if anyone has any references about these flags or library handling on OS X in general. Now I see them I remember being bitten by the same thing in the past – yet I’m unable to find any documentation on what they actually do and why/when they should be needed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 211k
  • Answers 211k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer There are 2 questions in your question actually: Can I… May 12, 2026 at 10:11 pm
  • Editorial Team
    Editorial Team added an answer Check this article: http://www.fourcc.org/fccyvrgb.php Conversion is quite easy so you… May 12, 2026 at 10:11 pm
  • Editorial Team
    Editorial Team added an answer It turns out the Annotate method was the solution: Post.objects.all().annotate(vote_tally=Sum('vote__value')).order_by('-vote_tally') May 12, 2026 at 10:11 pm

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I want use html5's new tag to play a wav file (currently only supported
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
In order to apply a triggered animation to all ToolTip s in my app,
I have a French site that I want to parse, but am running into

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.