You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Contains a syntax reference for CMake. We'll be going through some concepts, the CLI, and scripting with CMake!
5
5
6
6
------
7
7
@@ -34,13 +34,14 @@ Contains a syntax reference for CMake
34
34
3.3 [Comments](#3.3)
35
35
3.4 [Printing](#3.4)
36
36
3.5 [Variables](#3.5)
37
-
3.6 [Variable Scope](#3.6)
38
-
3.7 [Cache Variables](#3.7)
39
-
3.8 [Prefixes](#3.8)
40
-
3.9 [Conditionals](#3.9)
41
-
3.10 [Loops](#3.10)
42
-
3.11 [Functions](#3.11)
43
-
3.12 [Macros](#3.12)
37
+
3.6 [Variable Scope and Directories](#3.6)
38
+
3.7 [User Input: Cache Variables](#3.7)
39
+
3.8 [User Input: Options](#3.8)
40
+
3.9 [Prefixes](#3.9)
41
+
3.10 [Conditionals](#3.10)
42
+
3.11 [Loops](#3.11)
43
+
3.12 [Functions](#3.12)
44
+
3.13 [Macros](#3.13)
44
45
45
46
46
47
@@ -211,6 +212,8 @@ cmake -DCMAKE_BUILD_TYPE=Release # Build optimised code with no debug flags
211
212
cmake -D CMAKE_BUILD_TYPE=Release # This also works!
212
213
```
213
214
215
+
> Additionally, do note that this is actually the **best practice for invoking CMake**
216
+
214
217
215
218
216
219
#### **View Cached Variables**
@@ -322,7 +325,7 @@ The bare minimum CMakeLists that does meaningful work involves three things
322
325
323
326
- CMake requirements
324
327
- Project name
325
-
- Build targets
328
+
- Build **targets**
326
329
327
330
> But actually, if you just want to test the basic programming stuff in CMake, you can have an empty `CMakeLists.txt` file with just the statements to test (in most cases!) It won't build anything, but it's good enough.
328
331
>
@@ -471,6 +474,10 @@ set(EQUIVALENT_LIST "a;b") # These are the same!
Variables are created in the scope they are set in. And scopes are defined by directories and functions. So just think of C++ scopes, and you should be fine.
519
+
Variables are created in the scope they are set in. And scopes are defined by directories and functions.
520
+
521
+
> **Directories!?**
522
+
>
523
+
> Yes! Directories! Since each `CMakeLists.txt` file governs its directory, so treat them modularly like that
524
+
525
+
So just think of C++ scopes, and you should be fine.
513
526
514
527
Luckily, there is a way to state that a variable is set in its parent scope, instead of its current scope.
### 3.7 User Input: Cache Variables <aname="3.7"></a>
524
537
[go to top](#top)
525
538
526
539
527
540
CMake uses a cache, which is really just a text file called `CMakeCache.txt` to remember settings so you don't have to restate them when running CMake.
528
541
529
-
Normal variables aren't placed in the cache unless you explicitly tell CMake to though! **Adding a variable as a cache variable also exposes them in the command line**.
542
+
Normal variables aren't placed in the cache unless you explicitly tell CMake to though! **Adding a variable as a cache variable also exposes them in the command line**. If you don't set them in the command line, they'll use their default value, so it's a good way to create **settable variables with default values**!
543
+
544
+
545
+
546
+
#### **Listing Cache Variables**
547
+
548
+
You can list out all the available settable cache variables from the command line!
549
+
550
+
Just use these commands:
551
+
552
+
```shell
553
+
cmake -L # List all non-advanced cache variables
554
+
cmake -LA # List all cache varialbes (including advanced ones)
555
+
cmake -LAH # List all cache variables, and also display help for them
556
+
```
557
+
558
+
559
+
560
+
#### **Setting Cache Variables in the Command Line**
These are just boolean cache variables that you can set ON or OFF in the command line.
625
+
626
+
627
+
628
+
#### **Listing Options**
629
+
630
+
If you want to use options, you can use the same cache variable interface.
631
+
632
+
> You can list out all the available settable cache variables from the command line!
633
+
>
634
+
> Just use these commands:
635
+
>
636
+
>```shell
637
+
> cmake -L # List all non-advanced cache variables
638
+
> cmake -LA # List all cache varialbes (including advanced ones)
639
+
> cmake -LAH # List all cache variables, and also display help for them
640
+
> ```
641
+
642
+
643
+
644
+
#### **Setting Options in the Command Line**
645
+
646
+
Same as setting variables in the command line.
647
+
648
+
```shell
649
+
cmake -DOPTION_NAME=ON
650
+
```
651
+
652
+
653
+
654
+
#### **Setting Options**
655
+
656
+
```cmake
657
+
option(OPTION_NAME "SOME_RANDOM_DOCSTRINGS" ON) # OPTION_NAME is default ON
658
+
659
+
# You can also set a dependent option!
660
+
# This one defaults to ON
661
+
# If and only if ON_ME_BEING_ON is ON and AND_ON_ME_BEING_OFF is OFF
662
+
CMAKE_DEPENDENT_OPTION(DEP_OPTION "I'm dependent!!" ON
663
+
"ON_ME_BEING_ON;NOT AND_ON_ME_BEING_OFF" OFF)
664
+
```
665
+
666
+
667
+
668
+
### 3.9 Prefixes <a name="3.9"></a>
669
+
577
670
[go to top](#top)
578
671
579
672
@@ -594,7 +687,7 @@ set(${PREFIX}_C 3)
594
687
595
688
596
689
597
-
### 3.9 Conditionals <a name="3.9"></a>
690
+
### 3.10 Conditionals <a name="3.10"></a>
598
691
[go to top](#top)
599
692
600
693
@@ -608,7 +701,9 @@ else()
608
701
endif()
609
702
```
610
703
611
-
Cool! You can control program flow depending on whether stuff evaluates `TRUE` or `FALSE`
704
+
Cool! You can control program flow depending on whether stuff evaluates `TRUE` or `FALSE`.
705
+
706
+
612
707
613
708
#### **Boolean Constants**
614
709
@@ -626,6 +721,8 @@ Additionally, the following are evaluated to false:
626
721
627
722
`IGNORE`, `NOTFOUND`, an empty string, or strings that end with the suffix "-NOTFOUND"
628
723
724
+
725
+
629
726
#### **Example Usage**
630
727
631
728
```cmake
@@ -644,6 +741,8 @@ endif()
644
741
>
645
742
> The `if()` statement is a little weird in this regard, because it takes in both variables and constants! In this case, if we wrapped VAR and passed it in as `if(${VAR})`, the if statement evaluates it as the string "TRUE" as opposed to the boolean constant `TRUE`.
646
743
744
+
745
+
647
746
#### **Logic**
648
747
649
748
You can do logic with the expressions that go into `if()`!
0 commit comments