C++外文翻译Word文档格式.docx
《C++外文翻译Word文档格式.docx》由会员分享,可在线阅读,更多相关《C++外文翻译Word文档格式.docx(12页珍藏版)》请在冰豆网上搜索。
,andMicrosoftWindows95operatingsystems,andApple®
Macintosh®
UNIX,andRISC(reducedinstructionsetcomputer)machines.Untilrecently,developerswantingtobuildversionsoftheirapplicationformorethanoneplatformhadfewchoices:
∙Maintainseparatecodebasesforeachplatform,writtentotheplatform'
snativeapplicationprogramminginterface(API).
∙Writetoa"
virtualAPI"
suchasthoseprovidedbycross-platformtoolsets.
∙Buildtheirownmultiple-platformlayerandsupportit.
Today,however,anewchoiceexists.DeveloperscanusetheirexistingcodewrittentotheWindowsAPIand,usingtoolsavailablefromMicrosoftandthirdparties,recompileforalloftheplatformslistedabove.Thispaperlooksatthemethodsandsomeoftheissuesinvolvedindoingso.
Currentlythemostlucrativemarketforgraphicaluserinterface(GUI)applications,afterMicrosoftWindows,istheAppleMacintosh.However,vastdifferencesseparatethesewhollydifferentoperatingsystems,requiringdeveloperstolearnnewAPIs,programmingparadigms,andtools.Generally,MacintoshdevelopmentrequiresaseparatecodebasefromtheWindowssources,increasingthecomplexityofmaintenanceandenhancement.
BecauseportingcodefromWindowstotheMacintoshcanbethemostdifficultportingcase,thispaperconcentratesinthisarea.Ingeneral,ifyourcodebaseissufficientlyportabletoenablestraightforwardrecompilingfortheMacintosh(excludinganyplatform-specific,or"
edge"
code,youmayelecttoinclude),you'
llfindthatitwillcomeuponotherplatformseasilyaswell.
MicrosoftVisualC++®
Cross-DevelopmentEditionforMacintosh(VisualC++forMac™)providesasetofWindowsNT–orWindows95–hostedtoolsforrecompilingyourWindowscodefortheMotorola680x0andPowerPCprocessors,andaportabilitylibrarythatimplementsWindowsontheMacintosh.ThisallowsyoutodevelopGUIapplicationswithasinglesourcecodebase(writtentotheWin32®
API)andimplementitonMicrosoftWindowsorAppleMacintoshplatforms.
Figure1,below,illustrateshowVisualC++forMacworks.Yoursourcecodeisedited,compiled,andlinkedonaWindowsNT–orWindows95–based(Intel)hostmachine.Thetoolscreate68000andPowerPCnativecodeandMacintoshresources.AnEthernet-basedorserialtransportlayer(TL)movestheresultingbinariestoaMacintoshtargetmachinerunningremotely.TheMacintoshapplicationisstartedontheMacintoshanddebuggedremotelyfromtheWindows-basedmachine.
NowthatApplehastwodifferentMacintosharchitecturestocontendwith(Motorola680x0andPowerPC)portabilityisparticularlyimportant.
Portingcaninvolveseveralsteps,dependingonwhetheryouareworkingwithold16-bitapplicationsorwithnew32-bitsources.Ingeneral,thestepstoaMacintoshportareasfollows:
1.Makeyourapplicationmoreportablebyfollowingsomegeneralportabilityguidelines.Thiswillhelpinsurenotonlyportabilitytothe680x0-basedMacintoshmachines,butalsotothenewer,morepowerfulPowerPCmachinesthatarebasedonaRISCchip.
2.PortyourapplicationfromWindows16-bitcodeto32-bitcode.Thismaybethemostcomplexandtime-consumingpartofthejob.
3.SegregatethosepartsofyourapplicationthatareuniquetoWindowsfromsimilarimplementationsthatarespecifictotheMacintosh.Thismayinvolveusingconditionalcompilationoritmayinvolvechangingthesourcetreeforyourproject.
4.PortyourWin32APIcodetotheMacintoshbyusingtheportabilitylibraryfortheMacintoshandVisualC++forcompiling,linking,anddebugging.
5.UsetheMicrosoftFoundationClassLibrary(MFC)version4.0toimplementnewfunctionalitysuchasOLE2.0containers,servers,andclientsordatabasesupportusingopendatabaseconnectivity(ODBC).CodewrittenusingMFCishighlyportabletotheMacintosh.
6.WriteMacintosh-specificcodetotakeadvantageofuniqueMacintoshfeatures,suchasAppleEventsorPublishandSubscribe.
ThechiefchallengeamongthefamiliesofWindowsoperatingsystemsisthebreakfrom16bits(Windows3.11andWindowsforWorkgroups3.11operatingsystemwithintegratednetworking)to32bits(WindowsNTandWindows95).Ingeneral,16-bitand32-bitcodebasesaresomewhatincompatible,unlesstheyarewrittenusingMFC.Developershavethechoiceofbranchingtheirsourcesintotwotrees,ormigratingeverythingto32bits.OncetheWin32choicehasbeenmade,howarelegacyplatformstoberun(thatis,machinesstillrunningWindows3.11)?
TheobviouschoiceistousetheWin32s®
APIlibraries,whichthunk32-bitcallsdowntotheir16-bitcounterparts.
DeveloperswhowanttheirapplicationstobeabletotakeadvantageofthehotnewRISChardware,suchasDECAlphaAXPmachines,canusethespecialmultipleplatformeditionsofVisualC++.TheseincludeversionsfortheMIPSR4000seriesofprocessorsaswellastheaforementionedDECAlphaAXPchipandtheMotorolaPowerPC.ThesetoolsetsrununderWindowsNT3.51andcreatehighlyoptimizednativeWin32applicationsforDECAlphaandMotorolaPowerPCplatforms.
DeveloperswhohaverecompiledtheirWin32sourcesusingthesetoolsetsareamazedathowsimpleitis.Sincetheoperatingsystemisidenticalonallplatforms,andthetoolsareidentical,littleworkhastobedoneinordertoachieveaport.ThekeydifferenceintheRISCmachinesfromIntelistheexistenceofanative64-bitinteger,whichisfarmoreefficientthanon32-bit(thatis,Intel)processors.
Microsoftworkscloselywithtwothird-partyUNIXtoolsproviders,BristolTechnologyandMainsoftCorporation,toallowdeveloperstorecompiletheirWin32-basedorMFC-basedapplicationsforUNIX.Developersseekingadditionalinformationshouldcontactthosecompaniesdirectly.
You'
llhavetodecideearlyonwhethertowritetothenativeAPI(Win32)ortoMFC.Ingeneralyou'
llfindMFCapplicationswillportmorequicklythanWin32applications.Thisisbecauseoneoftheintrinsicbenefitsofanapplicationframeworkisanabstractionofthecodeawayfromthenativeoperatingsystemtosomeextent.Thisabstractionislikeaninsurancepolicyforyou.However,developersfrequentlyhavequestionsaboutMFC,suchas:
∙WhatifIneedanoperatingsystemservicethatisn'
tpartoftheframework?
CalltheWin32APIdirectly.MFCneverpreventsyoufromcallinganyfunctionintheWin32APIdirectly.Justprecedeyourfunctioncallwiththeglobalscopeoperator(:
:
).
∙Idon'
tknowC++.CanIstilluseMFC?
Sure.MFCisbasedonC++,butyoucanmixCandC++codeseamlessly.
∙HowcanIgetstartedusingMFC?
Startbytakingsomeclassesand/orreadingsomebooks.VisualC++shipswithafinetutorialonMFC(Scribble).Then,checkouttheMFCMigrationKit(availableonCompuServeor,foramodestshippingandhandlingfee,fromMicrosoft).ItwillhelpyoumigrateyourC-basedapplicationcodetoMFCandC++.
Allportingwillbeeasierifyoubegintodaywritingmoreportableprograms.Followingsomebasicportabilityguidelineswillmakeyourcodelessplatform-specific.
Neverassumeanything.Particularly,don'
tmakeassumptionsaboutthesizesoftypes,thestateofthemachineatanytime,byteordering,oralignment.
Don'
tassumethesizeofprimitivetypes,becausethesehavedifferentsizesondifferentprocessors.Forexample,anintistwobytesinWin16andfourbytesinWin32.Atallcosts,avoidcodethatreliesonthesizeofatype.Usesizeof()instead.Todeterminetheoffsetofafieldinastructure,usetheoffsetof()macro.Don'
ttrytocomputethismanually.
Useprogrammaticinterfacestoaccessallsystemorhidden"
objects,"
forexample,thestackorheap.
ParsingdatatypestoextractindividualbytesorevenbitscancauseproblemswhenportingfromWindowstotheMacintoshunlessyouarecarefultowritecodethatdoesn'
tassumeanyparticularbyteorder.LIMITS.Hcontainsconstantsthatcanbeusedtohelpwriteplatform-independentmacrostoaccessindividualbytesinaword.
Thismayseemobvious,becausenothingcouldbelessportablethanassemblylanguage.Compilers,suchasMicrosoftVisualC++,thatprovideinlineassemblersmakeiteasytoslipinalittleassemblercodetospeedthingsup.Ifyouwantportablecode,however,avoidthistemptation.Itmaynotbenecessary.Moderncompilerscanoftengeneratecodeasgoodashand-tunednativeassemblercode.OurownresearchatMicrosoftindicatesthatperformanceproblemsaremoreoftentheresultofpooralgorithmsthantheyareofpoorcodegeneration.Indeed,withRISCmachines,hand-turnednativeassemblercodemayactuallybeworsethanmachine-generatedcode,duetothecomplexityofinstructionschedulingandpickingregisterusage.
WriteallroutinesinCfirst;
then,ifyouabsolutelyneedtorewriteoneinassembler,besuretoleavebothimplementationsinyoursources,controlledbyconditionalcompiles,andkeepbothuptodate.
AmajorgoalofAmericanNationalStandardsInstitute(ANSI)C/C++istoprovideaportableimplementationofthelanguage.Theoretically,codewrittentostrictANSICcomplianceiscompletelyportabletoanycompilerthatimplementsthestandardcorrectly.MicrosoftVisualC++providesacompileroption(/Za)toenablestrictANSIcompatibilitychecking.
MicrosoftVisualC++providessomelanguagefeaturesthatareinadditiontoANSIC,suchasfour-characterconstantsandsingle-linecomments.ProgramsthatusetheMicrosoftCextensionsshouldbeportabletoallotherimplementationsofMicrosoftVisualC++.Thus,