0% found this document useful (0 votes)
32 views27 pages

CMake Documentation - Dark

cmake tutorial dark

Uploaded by

suwemegy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
32 views27 pages

CMake Documentation - Dark

cmake tutorial dark

Uploaded by

suwemegy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 27
Step 1: A Basic Starting Point Where do | start with CMake? This step will provide an introduction to some of CMake's basic Oa Ue R WOR oe eee ea ee eR ae exercises and create a simple CMake project. Each exercise in this step will start with some background information. Then, a goal and list of helpful resources are provided. Each file in the Fiies to cait section is in the step directory and contains one or more too comments. Each Toso represents a line or two of code to change or Ce Ree aC kre COR em eRe UR or) PCR Roeser tne Oa eRe NC en Oe eee ee Uh he ce heen Rose ercise. Finally, at the end of each exercise the intended solution is discussed. Also note that each step in the tutorial builds on the next. So, for example, the starting code for eee Re croton eee Exercise 1 - Building a Basic Project The most basic CMake project is an executable built from a single source code file. For simple oe caed ced Reena eee ec Peete sae ee Re ee ee ke Momo ‘commands are preferred and will be used throughout the tutorial. Any project's top most CMakeLists.txt must start by specifying @ minimum CMake version using eee eee Reco oe geo eu acne lowing CMake functions are run with a compatible version of CMake. Secret) em CR eke RO ei EO acme TT ee eet ee eee cme nen eee Ck esr this command can also be used to specify other project level information such as the language Oe ns AU eet SCO MCU IC RC Ren CRORE UR cetic lo Mile Raa oc) etek ae Goal Ue Onto Ry ke Lace Helpful Resources Seems soto) Pee aCe 6) © project() Files to Edit Caer ee eed Getting Started Bie Me Res Se eRe 0 RR Ste eo oe Ong Ike) Pome R Rcd MCR et Ca Se ROR Cea com RC Ee ae ee eee RU Reet (eel ea Leh ete es ETC RTO) een ee eR ie ce eam Ce meme aot Gn Roce ee ee eee Ree ae oe ue Ree Reco chosen build tool. eee eC Re ee Ret eee cond the CMake source code tree and create a build directory: eer med Next, navigate to that build directory and run cnake to configure the project and generate a na- Nn ca erat) Eon See UROL etc NRC oT ae to ne eee gu mea tee OLN cme me ESI eM aI ee Ce Ke ccna cars ee ADC Sao eee Se Cee et Td ccecsmtd Tutorial Dee Si Re tee a ee ee er eed SD ea cc on TC Rone a ete ots Solution eee ee ee eee ee On a ee first line is to USE cmake_nininun_required() to set the CMake version as follows. [meee SRC ae Soe RRC Od oe eR eee CRO am On u Lamle) OP sae ee ORO ee & Re eR ee cd er eee OR eee > TODO 3: Glick to show/hide answer Exercise 2 - Specifying the C++ Standard ene) A eeu eae RU RULE eae Ue me Cee en MU eect ROMA LIL Pee aca ya cee Me AOR RCC tec e mea RUC UI) Cee RU amet RU Om ns ecm AU eo te mono ables are cnakc_cxx_stawparo and cHAKE_cKx STANDARD_REQUIRED. These may be used together to Pee RU Reema currence ce Cl at het asc Goal PNET CMR UCC era Helpful Resources Peonmerms ota) Se ee moe © set() Files to Edit Oeen en scd Ceeteecd Getting Started eee RS eee eC Le ee eRe Cae Rs First, edit cutorial.cxx by adding a feature that requires C+#11. Then update aekeLists. ext to ire Nk Omea Build and Run ROO Ree oc eS ee oe Re eee Une ns See meee CRC ee Rss ee reet] etree ie Nena eRe on Sa ae eee eS ea eee Seceee erate cceesmed Tutorial, Solution We start by adding some C++11 features to our project by replacing atof with ctd::stod in eee ea oe nee ee) > TODO 4: Click to show/hide answer Sede CRE ACU ets cesta Reo carne te a ae eno a Reece mega Peer eat ee Cae Rs ee] keree eC Re oe cee ORC Pee em ee Cee ere eR Re eee eee eee Ree Ce nn ecm eC re nee fee ee ee sree > TODO 6: Glick to show/hide answer Exercise 3 - Adding a Version Number and Configured Header aI Pe aa ae OR Ce RUC CRC RU ee see eee od available in your source code. In this case, we would like to print the project version. One way to accomplish this is by using a configured header file. We create an input file with one CRE ees CRs CoM eR aE es cra Cee enc a cee Pe ee eR A a Rem tae ager ted TRCN era Rn Sec seme Maen Ree eR Re Ce NURS eee me RUSS CCR ese it creates a single source of truth and avoids duplication. Goal Define and report the project's version number. Helpful Resources Sean mime Seen mS Comey Cecaset etc) Ceca et ace) Files to Edit Caer ee acd Pericsteers Getting Started eeu SS ee rescue ee Re CRU ace ce Pee Romeo Cum ee ee configure_File() 0 copy a given input file to an output file and substitute some variable values in ae Reon Next, create an input header file Tutorielcontig.h.in defining version numbers which will accept Ree ee ee Re eo Finally, update tutoriai.cxx to print out its version number. LTCC Tae) Let's build our project again. As before, we already created a build directory and ran CMake so Rete en nett csr Se reeretl er crc NUE eee ic CU ee ae Ere mecs Solution RUS eC ee cence Mee ema a een os) this exclusively in the source code, using cueketists.cxt lets Us maintain a single source of data feu ead CR a eee Re eRe ca me cd name and version number. When the project() command is called, CMake defines ome Sune eI Reo moc meek E ILS > TODO 7: Glick to show/hide answer Then we used configure_file() to copy the input file with the specified CMake variables eH >» TODO &: Glick to show/hide answer Since the configured file will be written into the project binary directory, we must add that direc- tory to the list of paths to search for include files. Note: Throughout this tutorial, we will refer to the project bulld and the project binary directory Te a oe a Ue Uae Td coco ete Rett oe etek eR Cenc Rue asin a age reece > TODO 9: Glick to show/hide answer Seo ee a a ae cee ee Renee ene Cree Se OR cee i teem ec mn Rae placed with the corresponding version numbers from the project in rutorsaiconfig.n. >» TODO 10: Click to show/hide answer a A ROR ees cmeee CMON RU LeOMOU IR -- a arec ee > TODO 11: Click to show/hide answer Te ce oa ee Soo) Coos > TODO 12: Click to show/hide answer Step 2: Adding a Library eS eR Cee ccm ke Cm Ce TO nS Ca coe nO Ca aoe eee ee Exercise 1 - Creating a Library POEM CMU Re Cen a Meme Ee omecn ea Re cme Com Tennant tac Puen eCeat eROg ec N Ree eC mR uote eee ce cos ey eR ee rem Tee eee ee cee eA ee CR Re eee Ree ee oe cree eR ee er ee eee Ree aR CR eet scl cece CRS co Cole a Wc CO Cee coe ee SCO URC See me) Goal Add and use a library. Helpful Resources CeCe ZO} © add_subdirectory() CeCe ot eric cSt) CeCe E eG) eam ea Files to Edit Caer ees scd Pecans Seca CR emcd Getting Started Soe Re eM OR) eee eon aM eon en ee aes ee ee eet ce eer nee VR oc a Rene Retreats ean ccc Rukia For this tutorial we will put the library into a subdirectory called mathFunctions. This directory al- ee eee eee eee ere end See ee eee ee ee Cn ee eee ees ee eRe SCOR Coc Ree ca ecas Ce Scene eee ea RO RUM ac) Coe eras Gun Re eae ce ce eC Ue Re ee Or as ES RUA Ren eee RLM usc Lem eee (Ok Next, edit the top level ciskeLists txt. Gur Ae CU ecce ce Ree eet Le Rec escees Build and Run Run the cnake executable or the cmake-gui to configure the project and then build it with your Cec hos CE oi mee aia Nena ees noe Seemed ce yecese) Eee eee SOAR Re eee ee RU ae ee ee cet Kaod Sees Solution ee Re en eee ca er eC Cea) Pee eee Une eee UR ec Rm eee eee) reece ones > TODO 1: Glick to show/hide answer SOME Cm Me eee Cress Resa ea ear > TODO 2: Click to show/hide answer Next, the new library target is linked to the executable target using target link Libraries(). > TODO 3: Click to show/hide answer cr ee ee eo) pees eae eet ep RCC ree esteeen ne Une ae eae) Cs eee Rena » TODO 4: Click to show/hide answer NOM eae tel ae Wa ee Omen hee ace >» TODO 5: Glick to show/hide answer es eet a ce eee ee » TODO 6: Click to show/hide answer Exercise 2 - Adding an Option Now let us add an option in the MathFunctions library to allow developers to select either the custom square root implementation or the built in standard implementation. While for the tutorial there really isn't any need to do so, for larger projects this is a common occurrence. ‘CMake can do this using the option() command. This gives users a variable which they can fe ile Rene OY RUC US eee ee ee user does not need to set the value each time they run CMake on a build directory. Goal CCR rae ont Re ae mesa Helpful Resources oer Parastrre) Cerra SCR TsCrscLio) Files to Edit Cease oRtienesd eu act Le acceand (eT au nom) ecta care cane eR oR cn ccc ee con Wer GSoC Ce RCO ae n este cu uence ce eeeeae Ce ee eee nn ae eee ea UU eae see OL eee Cn cee Tee eee Re Rae a ene ea Reena ce Cees ecco Build and Run Since we have our build directory already configured from Exercise 1, we can rebuild by simply eRe tae eee cr ec

You might also like