Commit 0d3e19fb authored by Mark Malone's avatar Mark Malone
Browse files

initial cpp project

parent 30cb7114
# CMake
cmake-build-*/
build/*
!build/.gitkeep
cbuild/
image: ubuntu:hirsute
before_script:
- rm -Rf build/
- mkdir build
- cd build
- cmake ..
- make
run tests:
script:
- ./bin/helloworld_tests
- cd CMakeFiles/helloworld_tests.dir/
- lcov -d . -c -o coverage.info
- lcov -r coverage.info */build/* */tests/* */c++/* -o coverageFiltered.info
- lcov --list coverageFiltered.info
coverage: '/Total:\|\s*([0-9.]+%)\s.*$/'
\ No newline at end of file
# Changelog
All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [1.0.0] - 2021-07-30
### Added
- first version of c++ project
[1.0.0]: https://code.80px.com/80px/80px.com/cpp/hello_world/-/compare/30cb7114...main
cmake_minimum_required (VERSION 3.5.1)
set(CMAKE_C_COMPILER "gcc")
set(CMAKE_CXX_COMPILER "g++")
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --coverage")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --coverage")
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "lib")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "lib")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "bin")
enable_testing()
########################################################################
# Download a local copy of GoogleTest in build/
configure_file(CMakeLists.txt.in googletest-download/CMakeLists.txt)
execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" .
RESULT_VARIABLE result
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download
)
if(result)
message(FATAL_ERROR "CMake step for googletest failed: ${result}")
endif()
execute_process(COMMAND ${CMAKE_COMMAND} --build .
RESULT_VARIABLE result
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download
)
if(result)
message(FATAL_ERROR "Build step for googletest failed: ${result}")
endif()
add_subdirectory(${CMAKE_CURRENT_BINARY_DIR}/googletest-src
${CMAKE_CURRENT_BINARY_DIR}/googletest-build
EXCLUDE_FROM_ALL)
########################################################################
set(APP
"helloworld"
)
set(APP_HEADERS
"include"
)
set(APP_SOURCES
"src/helloworld.cpp"
)
set(APP_TESTER
"helloworld_tests"
)
set(APP_TESTS
"tests/helloworld_test.cpp"
)
project(${APP}
VERSION 1.0.0
LANGUAGES "CXX"
)
include_directories(${APP_HEADERS})
add_executable(${APP}
"${APP_SOURCES}"
"src/main.cpp"
)
add_executable(${APP_TESTER}
"${APP_SOURCES}"
"${APP_TESTS}"
)
target_link_libraries(${APP_TESTER}
gtest_main
pthread
)
cmake_minimum_required(VERSION 3.5.1)
project(googletest-download NONE)
include(ExternalProject)
ExternalProject_Add(googletest
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG release-1.11.0
SOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}/googletest-src"
BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/googletest-build"
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
INSTALL_COMMAND ""
TEST_COMMAND ""
)
\ No newline at end of file
# Contributing
See the existing examples for how to create a test for your funtionality first to drive the development.
Feel free to make a merge request once you have a working piece of functionality.
MIT License
Copyright (c) 2021 80px
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
# hello_world
[![pipeline status](https://code.80px.com/80px/80px.com/cpp/hello_world/badges/main/pipeline.svg)](https://code.80px.com/80px/80px.com/cpp/hello_world/-/commits/main) [![coverage report](https://code.80px.com/80px/80px.com/cpp/hello_world/badges/main/coverage.svg)](https://code.80px.com/80px/80px.com/cpp/hello_world/-/commits/main)
hello_world is a c++ project to return "Hello world".
## Prerequisites
You need `g++`, `cmake`, `git`, and `make` to run hello_world.
## Usage
### Building application and tests
```shell script
rm -rf build/
mkdir build
cd build
cmake ..
make
```
This will create two executables: `helloworld` and `helloworld_tests` in the `./build/bin` directory.
### Running the application
```bash
./bin/helloworld
```
```bash
Hello World!
```
### Running the tests
```bash
./bin/helloworld_tests
```
```bash
Running main() from .../hello_world/build/googletest-src/googletest/src/gtest_main.cc
[==========] Running 1 test from 1 test suite.
[----------] Global test environment set-up.
[----------] 1 test from HelloWorldTest
[ RUN ] HelloWorldTest.PositiveResponse
[ OK ] HelloWorldTest.PositiveResponse (0 ms)
[----------] 1 test from HelloWorldTest (0 ms total)
[----------] Global test environment tear-down
[==========] 1 test from 1 test suite ran. (0 ms total)
[ PASSED ] 1 test.```
```
### Getting code coverage
After building and testing your changes (see above)
```bash
./bin/helloworld_tests # Run the tests at least once to determine coverage
cd CMakeFiles/helloworld_tests.dir/
lcov -d . -c -o coverage.info
lcov -r coverage.info */build/* */tests/* */c++/* -o coverage.info # Remove coverage from includes
lcov --list coverage.info
```
```bash
Reading tracefile coverage.info
|Lines |Functions |Branches
Filename |Rate Num|Rate Num|Rate Num
===========================================================
[.../code/hello_world/src/]
helloworld.cpp | 100% 2| 100% 1| - 0
===========================================================
Total:| 100% 2| 100% 1| - 0
```
## Contributing
See [CONTRIBUTING.md](https://code.80px.com/80px/80px.com/cpp/hello_world/-/blob/main/CONTRIBUTING.md)
## License
[MIT](https://choosealicense.com/licenses/mit/)
#ifndef HELLOWORLD_H
#define HELLOWORLD_H
#include <string>
class HelloWorld
{
public:
static std::string greet();
};
#endif // HELLOWORLD_H
#include <string>
#include "helloworld.h"
std::string HelloWorld::greet() {
return "Hello World!";
}
#include <iostream>
#include "helloworld.h"
int main() {
std::cout << HelloWorld::greet() << std::endl;
return 0;
}
#include <gtest/gtest.h>
#include "helloworld.h"
TEST(HelloWorldTest, PositiveResponse) {
ASSERT_EQ(HelloWorld::greet(), "Hello World!");
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment