Wiki source for KeyPgExternBlock


Show raw source

{{fbdoc item="title" value="EXTERN...END EXTERN"}}----
Statement block to allow calling of functions compiled for specific languages or platforms.

{{fbdoc item="syntax"}}##
**Extern { """C""" | """C++""" | """Windows""" | """Windows-MS""" | """rtlib""" }** [ **[[KeyPgLib|Lib]]** "//libname//" ]
//declarative statements//
**End Extern**
##
{{fbdoc item="desc"}}
##**Extern**## blocks provide default calling conventions for procedures and mandate a certain name decoration.

##**Extern """C"""**## blocks provide a default ##[[KeyPgCdecl|cdecl]]## calling convention to procedures, and also preserve the case of all names declared within them. The same effect can be achieved without the EXTERN block by using ##[[KeyPgCdecl|cdecl]]## together with an ##[[KeyPgAlias|alias]]## string containing the exact procedure name.

##**Extern """C++"""**## blocks are exactly like ##**Extern """C"""**## blocks but they also mangle the names declared within them in a way compatible to that of //g""++""-4.x//.

##**Extern """Windows"""**## blocks provide a default ##[[KeyPgStdcall|Stdcall]]## calling convention to procedures, preserve the case of all names declared within them, and on the Windows platform, append an ##"@//N//"## suffix to procedure names, where ##//N//## is the total size in bytes of any procedure parameters. Similar to the ##**Extern """C"""**## block, the same effect can be achieved by using ##[[KeyPgStdcall|stdcall]]## and ##[[KeyPgAlias|alias]]##.

##**Extern """Windows-MS"""**## blocks are exactly like ##**Extern """Windows"""**## blocks but do not append the ##"@//N//"## suffix to procedure names on Windows.

##**Extern """rtlib"""**## blocks combine the name decoration of ##**Extern """c"""**## and the default calling convention of fbc. When used in a namespace, the symbol respects the scope of the namespace, however, the name decoration (name mangling) links the symbol to a regular C library. This behaviour may be desired when declaring procedures that exist in the fb run-time library; where calling convention is based on the target, but we would like to have the compile time name respect the namespace.

##[[KeyPgLib|Lib]] "//libname//"## can be used to specify a library which will be linked in as if ##[[KeyPgInclib|#inclib "libname"]]## or ##[[CompilerOptl|-l libname]]## had been used. Additionally, all procedure declarations inside the **Extern** block will use the specified ##[[KeyPgLib|Lib]] "//libname//"## as if it was specified as part of their declarations (but it can still be overridden with an explicit ##[[KeyPgLib|Lib]] "//libname//"##).

{{fbdoc item="ex"}}
{{fbdoc item="filename" value="examples/manual/module/extern-block.bas"}}%%(freebasic)
'' This procedure uses the default calling convention for the system, which is
'' STDCALL on Win32 and CDECL on Linux/DOS/*BSD, and is seen externally as
'' "MYTEST1@4" on Win32 and "MYTEST1" on Linux/DOS/*BSD (following FB's default
'' ALL-UPPER-CASE name mangling).
Sub MyTest1 ( ByVal i As Integer )
End Sub

Extern "C"
'' This procedure uses the CDECL convention and is seen externally
'' as "MyTest2".
Sub MyTest2 ( ByVal i As Integer )
End Sub
End Extern

Extern "C++"
'' This procedure uses the CDECL convention and its name is mangled
'' compatible to g++-4.x, specifically: "_Z7MyTest3i"
Sub MyTest3 ( ByVal i As Integer )
End Sub
End Extern

Extern "Windows"
'' This procedure uses the STDCALL convention and is seen externally
'' as "MyTest4@4" on Windows, and "MyTest4" on Linux, *BSD and DOS.
Sub MyTest4 ( ByVal i As Integer )
End Sub
End Extern

Extern "Windows-MS"
'' This procedure uses the STDCALL convention and is seen externally
'' as "MyTest5".
Sub MyTest5 ( ByVal i As Integer )
End Sub
End Extern

MyTest1( 0 )
MyTest2( 0 )
MyTest3( 0 )
MyTest4( 0 )
%%

{{fbdoc item="lang"}}
- ##**Extern**## blocks are only available in the //[[CompilerOptlang|-lang fb]]// dialect.

{{fbdoc item="diff"}}
- New to ""FreeBASIC""

{{fbdoc item="target"}}
- On Linux, *BSD and DOS platforms, ##**Extern "Windows"**## blocks never append a ##"@//N//"## suffix to procedure names, and thus are equal to ##**Extern "Windows-MS"**##.

{{fbdoc item="see"}}
- ##[[KeyPgCdecl|Cdecl]]##
- ##[[KeyPgStdcall|Stdcall]]##
- ##[[KeyPgExtern|Extern]]##

{{fbdoc item="back" value="CatPgModularizing|Modularizing"}}
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki



sf.net phatcode