1"use strict";(self.webpackChunkmodular_fe=self.webpackChunkmodular_fe||[]).push([["15315"],{99855(e,n,o){o.r(n),o.d(n,{metadata:()=>t,default:()=>p,frontMatter:()=>a,contentTitle:()=>c,toc:()=>l,assets:()=>s});var t=JSON.parse('{"id":"docs/reference/decorators/export","title":"@export","description":"Marks a function for export.","source":"@site/versioned_docs/version-1.0.0/docs/reference/decorators/export.mdx","sourceDirName":"docs/reference/decorators","slug":"/docs/reference/decorators/export","permalink":"/1.0.0/docs/reference/decorators/export","draft":false,"unlisted":false,"editUrl":"https://github.com/modular/modular/edit/main/Mojo/docs/site/reference/decorators/export.mdx","tags":[],"version":"1.0.0","frontMatter":{"title":"@export","description":"Marks a function for export.","codeTitle":true},"sidebar":"referenceSidebar","previous":{"title":"@explicit_destroy","permalink":"/1.0.0/docs/reference/decorators/explicit-destroy"},"next":{"title":"@fieldwise_init","permalink":"/1.0.0/docs/reference/decorators/fieldwise-init"}}'),r=o(74848),i=o(28453);let a={title:"@export",description:"Marks a function for export.",codeTitle:!0},c,s={},l=[];function d(e){let n={a:"a",admonition:"admonition",code:"code",li:"li",p:"p",pre:"pre",ul:"ul",...(0,i.R)(),...e.components};return(0,r.jsxs)(r.Fragment,{children:[(0,r.jsxs)(n.p,{children:["You can add the ",(0,r.jsx)(n.code,{children:"@export"})," decorator on any function to make it publicly\navailable as an exported symbol in the compiled artifact, allowing it to be\ncalled from external code. An ",(0,r.jsx)(n.code,{children:"@export"})," function must declare its calling\nconvention with an explicit ",(0,r.jsx)(n.a,{href:"../../../../docs/reference/function-declarations#abi-c",children:(0,r.jsx)(n.code,{children:"abi"})}),"\neffect."]}),"\n",(0,r.jsx)(n.pre,{children:(0,r.jsx)(n.code,{className:"language-mojo",children:'# This function is internal - not an exported symbol\ndef internal_helper():\n print("Internal")\n\n# This function is exported under its own name, "my_exported_function"\n@export\ndef my_exported_function() abi("Mojo"):\n print("Exported!")\n internal_helper()\n\n# This function is exported under the name "my_renamed_function"\n@export("my_renamed_function")\ndef my_other_function() abi("Mojo"):\n print("Another function.")\n'})}),"\n",(0,r.jsxs)(n.p,{children:["The ",(0,r.jsx)(n.code,{children:"@export"})," decorator can take an optional argument:"]}),"\n",(0,r.jsxs)(n.ul,{children:["\n",(0,r.jsx)(n.li,{children:"An alternate name to export the function under, as shown above."}),"\n"]}),"\n",(0,r.jsxs)(n.p,{children:["Use the name specifier and ",(0,r.jsx)(n.code,{children:'abi("C")'})," effect to export a function that complies\nwith the C calling conventions. You must also supply a function name that is a\nvalid C identifier. For example:"]}),"\n",(0,r.jsx)(n.pre,{children:(0,r.jsx)(n.code,{className:"language-mojo",children:'@export("my_func")\ndef my_function(\n name: StaticString,\n ptr: OpaquePointer[MutUntrackedOrigin],\n) abi("C") -> None:\n pass\n'})}),"\n",(0,r.jsx)(n.admonition,{title:"Initialize the runtime in shared libraries",type:"note",children:(0,r.jsxs)(n.p,{children:["If you compile an exported function into a shared library\n(",(0,r.jsx)(n.code,{children:"mojo build --emit shared-lib"}),") and call it from a non-Mojo host program\nsuch as C or C++, no Mojo ",(0,r.jsx)(n.code,{children:"main()"})," function runs, so the Mojo runtime is\nnever initialized. Call\n",(0,r.jsx)(n.a,{href:"../../../../docs/std/runtime/asyncrt/initialize_runtime/",children:(0,r.jsx)(n.code,{children:"initialize_runtime()"})}),"\nbefore calling any other standard library functions. See\n",(0,r.jsx)(n.a,{href:"../../../../docs/tools/compilation/#call-a-mojo-shared-library-from-c-or-c",children:"Call a Mojo shared library from C or\nC++"}),"\nfor details."]})}),"\n",(0,r.jsxs)(n.p,{children:["To call Mojo from Python, register functions with a module builder. See\n",(0,r.jsx)(n.a,{href:"../../../../docs/manual/python/mojo-from-python/",children:"Calling Mojo from Python"})," for details."]})]})}function p(e={}){let{wrapper:n}={...(0,i.R)(),...e.components};return n?(0,r.jsx)(n,{...e,children:(0,r.jsx)(d,{...e})}):d(e)}},28453(e,n,o){o.d(n,{R:()=>a,x:()=>c});var t=o(96540);let r={},i=t.createContext(r);function a(e){let n=t.useContext(i);return t.useMemo(function(){return"function"==typeof e?e(n):{...n,...e}},[n,e])}function c(e){let n;return n=e.disableParentContext?"function"==typeof e.components?e.components(r):e.components||r:a(e.components),t.createElement(i.Provider,{value:n},e.children)}}}]);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.