1
0
Fork 0
mirror of https://github.com/TheLartians/ModernCppStarter.git synced 2025-09-01 06:30:52 +02:00

add standalone executable

This commit is contained in:
Lars Melchior 2020-04-14 13:25:41 +02:00
parent ad1f006762
commit 500d028c8f
3 changed files with 97 additions and 7 deletions

View file

@ -33,16 +33,27 @@ This template is a collection from learnings of previous projects and should all
- Add your project's codecov token to your project's github secrets under `CODECOV_TOKEN` - Add your project's codecov token to your project's github secrets under `CODECOV_TOKEN`
- Happy coding! - Happy coding!
### Build and run the standalone target
Use the following command to build and run the executable target.
```bash
cmake -Hstandalone -Bbuild/standalone
cmake --build build/standalone
./build/standalone/Greeter --help
```
### Build and run test suite ### Build and run test suite
Use the following commands from the project's root directory to run the test suite. Use the following commands from the project's root directory to run the test suite.
```bash ```bash
cmake -Htest -Bbuild cmake -Htest -Bbuild/test
cmake --build build cmake --build build/test
CTEST_OUTPUT_ON_FAILURE=1 cmake --build build --target test CTEST_OUTPUT_ON_FAILURE=1 cmake --build build/test --target test
# or simply call the executable: # or simply call the executable:
./build/GreeterTests ./build/test/GreeterTests
``` ```
To collect code coverage information, run CMake with the `-DENABLE_TEST_COVERAGE=1` option. To collect code coverage information, run CMake with the `-DENABLE_TEST_COVERAGE=1` option.
@ -52,11 +63,13 @@ To collect code coverage information, run CMake with the `-DENABLE_TEST_COVERAGE
Use the following commands from the project's root directory to run clang-format (must be installed on the host system). Use the following commands from the project's root directory to run clang-format (must be installed on the host system).
```bash ```bash
cmake -Htest -Bbuild cmake -Htest -Bbuild/test
# view changes # view changes
cmake --build build --target format cmake --build build/test --target format
# apply changes # apply changes
cmake --build build --target fix-format cmake --build build/test --target fix-format
``` ```
See [Format.cmake](https://github.com/TheLartians/Format.cmake) for more options. See [Format.cmake](https://github.com/TheLartians/Format.cmake) for more options.

31
standalone/CMakeLists.txt Normal file
View file

@ -0,0 +1,31 @@
cmake_minimum_required(VERSION 3.5 FATAL_ERROR)
project(GreeterStandalone
LANGUAGES CXX
)
# ---- Dependencies ----
include(../cmake/CPM.cmake)
CPMAddPackage(
NAME cxxopts
GITHUB_REPOSITORY jarro2783/cxxopts
VERSION 2.2.0
OPTIONS
"CXXOPTS_BUILD_EXAMPLES Off"
"CXXOPTS_BUILD_TESTS Off"
)
CPMAddPackage(
NAME Greeter
SOURCE_DIR ${CMAKE_CURRENT_LIST_DIR}/..
)
# ---- Create standalone executable ----
file(GLOB sources ${CMAKE_CURRENT_SOURCE_DIR}/source/*.cpp)
add_executable(GreeterStandalone ${sources})
set_target_properties(GreeterStandalone PROPERTIES CXX_STANDARD 17 COMPILE_FLAGS "-Wall -pedantic -Wextra")
set_target_properties(GreeterStandalone PROPERTIES OUTPUT_NAME "Greeter")
target_link_libraries(GreeterStandalone Greeter cxxopts)

View file

@ -0,0 +1,46 @@
#include <greeter.h>
#include <cxxopts.hpp>
#include <iostream>
#include <string>
#include <unordered_map>
const std::unordered_map<std::string, greeter::LanguageCode> languages{
{"en", greeter::LanguageCode::EN},
{"de", greeter::LanguageCode::DE},
{"es", greeter::LanguageCode::ES},
{"fr", greeter::LanguageCode::FR},
};
int main(int argc, char** argv) {
cxxopts::Options options(argv[0], "A program to welcome the world!");
std::string language;
std::string name;
// clang-format off
options.add_options()
("h,help", "Show help")
("n,name", "Name to greet", cxxopts::value(name)->default_value("World"))
("l,lang", "Language code to use", cxxopts::value(language)->default_value("en"))
;
// clang-format on
auto result = options.parse(argc, argv);
if (result["help"].as<bool>()) {
std::cout << options.help() << std::endl;
return 0;
}
auto langIt = languages.find(language);
if (langIt == languages.end()) {
std::cout << "unknown language code: " << language << std::endl;
return 1;
}
greeter::Greeter greeter(name);
std::cout << greeter.greet(langIt->second) << std::endl;
return 0;
}