Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding lab resources for lessons 6 and 6b #2

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions cmake-docker-environment/demos/target-compile/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
cmake_minimum_required(VERSION 3.9.1)
project(mainTest)

add_executable(main main.cpp)
target_compile_definitions(main PRIVATE GREETING="Hello!")
target_compile_features(main PRIVATE cxx_std_11)
target_compile_options(main PRIVATE -Wall)
2 changes: 2 additions & 0 deletions cmake-docker-environment/demos/target-compile/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# target-compile
This is the code demo included at the beginning of the 'Targets' lesson. It is not included with the Docker enviornment's labs.
6 changes: 6 additions & 0 deletions cmake-docker-environment/demos/target-compile/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include <iostream>

int main() {
std::cout << GREETING << "\n";
return 0;
}
4 changes: 4 additions & 0 deletions cmake-docker-environment/labs/ninja-example/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
cmake_minimum_required(VERSION 3.9.1)
project(greetingTest)

add_executable(greeting main.cpp greeting.cpp)
6 changes: 6 additions & 0 deletions cmake-docker-environment/labs/ninja-example/greeting.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include "greeting.h"
#include <iostream>

void sayHello() {
std::cout << "Hello World!\n";
}
3 changes: 3 additions & 0 deletions cmake-docker-environment/labs/ninja-example/greeting.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#pragma once

void sayHello();
6 changes: 6 additions & 0 deletions cmake-docker-environment/labs/ninja-example/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include "greeting.h"

int main() {
sayHello();
return 0;
}
17 changes: 17 additions & 0 deletions cmake-docker-environment/labs/ninja-example/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# ninja-example
The project for this lesson contains the same code as cmake-example. However, instead of creating an executable with Make, this lesson will do the same work using the Ninja generator.

## Exercise 1: Examine the generators found in the lab environment
```
$ cmake --help
NickR2600 marked this conversation as resolved.
Show resolved Hide resolved
```

## Exercise 2: Create an executable using Ninja as the generator. Examine the resulting build.ninja file.
Make a slight change to greeting.cpp and build again.
```
$ cd ninja-example
$ mkdir build && cd build
$ cmake .. -G Ninja
$ cmake --build .
$ ./greeting
```
12 changes: 12 additions & 0 deletions cmake-docker-environment/labs/subdirectory-example/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
cmake_minimum_required(VERSION 3.9.1)
project(songTest)

add_subdirectory(firstDay)
add_subdirectory(secondDay)

add_library(thirdDay thirdDay/thirdDay.cpp)
NickR2600 marked this conversation as resolved.
Show resolved Hide resolved
target_include_directories(thirdDay PUBLIC ${CMAKE_CURRENT_LIST_DIR}/thirdDay/include)
target_link_libraries(thirdDay PUBLIC secondDay)

add_executable(song main.cpp)
target_link_libraries(song thirdDay)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
cmake_minimum_required(VERSION 3.9.1)
project(songTest VERSION 1.0.0)

add_library(firstDay firstDay.cpp)
target_include_directories(firstDay PUBLIC ${CMAKE_CURRENT_LIST_DIR}/include)
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include <iostream>
#include "firstDay.h"

void firstDay() {
std::cout << "1 Partridge in Pear Tree\n";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#pragma once

void firstDay();
10 changes: 10 additions & 0 deletions cmake-docker-environment/labs/subdirectory-example/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include "firstDay.h"
#include "secondDay.h"
#include "thirdDay.h"

int main() {
firstDay();
secondDay();
thirdDay();
return 0;
}
53 changes: 53 additions & 0 deletions cmake-docker-environment/labs/subdirectory-example/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# subdirectory-example
This project asks you to manage targets that are created in several CMakeLists.txt files across subdirectories. Before you start, change to the proper directory and make the build directory.
```
$ cd subdirectory-example
$ mkdir build && cd build
```

## Exercise 1: Move the thirdDay target functionality into thirdDay/CMakeLists.txt

Reverse engineer what you need from other files.

**CMakeLists.txt**

```
cmake_minimum_required(VERSION 3.9.1)
project(songTest)

add_subdirectory(firstDay)
add_subdirectory(secondDay)

add_library(thirdDay thirdDay/thirdDay.cpp)
target_include_directories(thirdDay PUBLIC ${CMAKE_CURRENT_LIST_DIR}/thirdDay/include)
target_link_libraries(thirdDay PUBLIC secondDay)

add_executable(song main.cpp)
target_link_libraries(song thirdDay)
```

**secondDay/CMakeLists.txt**

```
cmake_minimum_required(VERSION 3.9.1)
project(songTest)

add_library(secondDay secondDay.cpp)
NickR2600 marked this conversation as resolved.
Show resolved Hide resolved
target_include_directories(secondDay PUBLIC ${CMAKE_CURRENT_LIST_DIR}/include)
target_link_libraries(secondDay PUBLIC firstDay)
```

**thirdDay/CMakeLists.txt**

```
//Exercise 1
```

**Command Line**

Compile and run the executable.
```
$ cmake ..
$ make
$ ./song
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
cmake_minimum_required(VERSION 3.9.1)
project(songTest)

add_library(secondDay secondDay.cpp)
NickR2600 marked this conversation as resolved.
Show resolved Hide resolved
target_include_directories(secondDay PUBLIC ${CMAKE_CURRENT_LIST_DIR}/include)
target_link_libraries(secondDay PUBLIC firstDay)
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#pragma once

void secondDay();
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include <iostream>
#include "secondDay.h"
#include "firstDay.h"

void secondDay() {
std::cout << "2 Turtle Doves, ";
firstDay();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#pragma once

void thirdDay();
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include <iostream>
#include "thirdDay.h"
#include "secondDay.h"

void thirdDay() {
std::cout << "3 French Hens, ";
secondDay();
}
14 changes: 14 additions & 0 deletions cmake-docker-environment/labs/targets-example/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
cmake_minimum_required(VERSION 3.9.1)
project(songTest)

add_library(firstDay firstDay/firstDay.cpp)
target_include_directories(firstDay PUBLIC ${CMAKE_CURRENT_LIST_DIR}/firstDay/include)

add_library(secondDay secondDay/secondDay.cpp)
target_include_directories(secondDay PUBLIC ${CMAKE_CURRENT_LIST_DIR}/secondDay/include)
target_link_libraries(secondDay PUBLIC firstDay)

# thirdDay target

add_executable(song main.cpp)
target_link_libraries(song secondDay)
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include <iostream>
#include "firstDay.h"

void firstDay() {
std::cout << "1 Partridge in Pear Tree\n";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#pragma once

void firstDay();
10 changes: 10 additions & 0 deletions cmake-docker-environment/labs/targets-example/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include "firstDay.h"
#include "secondDay.h"
//#include "thirdDay.h" //Uncomment out this line

int main() {
firstDay();
secondDay();
//thirdDay(); //Uncomment out this line
return 0;
}
83 changes: 83 additions & 0 deletions cmake-docker-environment/labs/targets-example/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# targets-example
This project asks you to create targets that have varying visibility of each other. Before you start, change to the proper directory and make the build directory.
```
$ cd targets-example
$ mkdir build && cd build
```

## Exercise 1: Create a song executable that uses the thirdDay

**main.cpp**

Remove the comments so that the thirdDay header is included and the thirdDay function is called.
```
#include "firstDay.h"
#include "secondDay.h"
#include "thirdDay.h" //Exercise 1, comment back in

int main() {
firstDay();
secondDay();
thirdDay() //Exercise 1, comment back in
return 0;
}
```

**CMakeLists.txt**

Modify CMakeLists.txt so that the executable utilizes thirdDay functionality. Reverse-engineer what you need for the thirdDay target and make any other tweaks.
```
cmake_minimum_required(VERSION 3.9.1)
project(songTest)

add_library(firstDay firstDay/firstDay.cpp)
target_include_directories(firstDay PUBLIC ${CMAKE_CURRENT_LIST_DIR}/firstDay/include)

add_library(secondDay secondDay/secondDay.cpp)
target_include_directories(secondDay PUBLIC ${CMAKE_CURRENT_LIST_DIR}/secondDay/include)
target_link_libraries(secondDay PUBLIC firstDay)

#setup the thirdDay target

add_executable(song main.cpp)
target_link_libraries(song secondDay)
```

**Command Line**

Run the executable.
```
$ cmake ..
$ make
$ ./song
```

## Exercise 2: Change the target_include_directory for the thirdDay target to have PRIVATE and INTERFACE visibility

**CMakeLists.txt**

```
...
target_include_directories(thirdDay PRIVATE ${CMAKE_CURRENT_LIST_DIR}/thirdDay/include)
...
```
```
...
target_include_directories(thirdDay INTERFACE ${CMAKE_CURRENT_LIST_DIR}/thirdDay/include)
...
```

**Command Line**

Try compiling and running.
```
$ cmake ..
$ make
$ ./song
```

## Exercise 3: Create a dependency graph and convert it to a PNG
```
$ cmake .. --graphviz=song.dot
$ dot -Tpng -o song.png song.dot
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#pragma once

void secondDay();
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include <iostream>
#include "secondDay.h"
#include "firstDay.h"

void secondDay() {
std::cout << "2 Turtle Doves, ";
firstDay();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#pragma once

void thirdDay();
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include <iostream>
#include "thirdDay.h"
#include "secondDay.h"

void thirdDay() {
std::cout << "3 French Hens, ";
secondDay();
}