generated from ENCCS/sphinx-lesson-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cpp code example for exercise in "producing libraries" session
- Loading branch information
1 parent
3d817ae
commit 1def96b
Showing
8 changed files
with
117 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# FIXME set minimum cmake version | ||
cmake_minimum_required(...) | ||
|
||
# FIXME declare project giving project name and language | ||
project(... LANGUAGES ...) | ||
|
||
# FIXME create the message library from the source files | ||
add_library(...) | ||
|
||
# FIXME create hello-world executable from hello-world.cpp | ||
add_executable(hello-world ...) | ||
|
||
# FIXME link hello-world to the message library | ||
target_link_libraries(hello-world ...) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#include "Message.hpp" | ||
|
||
#include <iostream> | ||
#include <string> | ||
|
||
std::ostream &Message::printObject(std::ostream &os) { | ||
os << "This is my very nice message: " << std::endl; | ||
os << message_; | ||
|
||
return os; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#pragma once | ||
|
||
#include <iosfwd> | ||
#include <string> | ||
|
||
class Message { | ||
public: | ||
Message(const std::string &m) : message_(m) {} | ||
|
||
friend std::ostream &operator<<(std::ostream &os, Message &obj) { | ||
return obj.printObject(os); | ||
} | ||
|
||
private: | ||
std::string message_; | ||
std::ostream &printObject(std::ostream &os); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#include "Message.hpp" | ||
|
||
#include <cstdlib> | ||
#include <iostream> | ||
|
||
int main() { | ||
Message say_hello("Hello, CMake World!"); | ||
|
||
std::cout << say_hello << std::endl; | ||
|
||
Message say_goodbye("Goodbye, CMake World"); | ||
|
||
std::cout << say_goodbye << std::endl; | ||
|
||
return EXIT_SUCCESS; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# set minimum cmake version | ||
cmake_minimum_required(VERSION 3.18) | ||
|
||
# project name and language | ||
project(libraries LANGUAGES CXX) | ||
|
||
# generate a library from sources | ||
add_library(message | ||
Message.hpp | ||
Message.cpp | ||
) | ||
|
||
add_executable(hello-world hello-world.cpp) | ||
|
||
target_link_libraries(hello-world PRIVATE message) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#include "Message.hpp" | ||
|
||
#include <iostream> | ||
#include <string> | ||
|
||
std::ostream &Message::printObject(std::ostream &os) { | ||
os << "This is my very nice message: " << std::endl; | ||
os << message_; | ||
|
||
return os; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#pragma once | ||
|
||
#include <iosfwd> | ||
#include <string> | ||
|
||
class Message { | ||
public: | ||
Message(const std::string &m) : message_(m) {} | ||
|
||
friend std::ostream &operator<<(std::ostream &os, Message &obj) { | ||
return obj.printObject(os); | ||
} | ||
|
||
private: | ||
std::string message_; | ||
std::ostream &printObject(std::ostream &os); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#include "Message.hpp" | ||
|
||
#include <cstdlib> | ||
#include <iostream> | ||
|
||
int main() { | ||
Message say_hello("Hello, CMake World!"); | ||
|
||
std::cout << say_hello << std::endl; | ||
|
||
Message say_goodbye("Goodbye, CMake World"); | ||
|
||
std::cout << say_goodbye << std::endl; | ||
|
||
return EXIT_SUCCESS; | ||
} |