sábado, 22 de junio de 2024

Gradle: creando un proyecto C++ (2)

Hace tiempo vimos como crear un proyecto en C++ usando Gradle.

Creamos un directorio:

mkdir holamundo

Nos ubicamos en el directorio creado:

cd holamundo

Creamos el proyecto:

gradle init

Elegimos estas opciones:

PS C:\Users\HP\Documents\pruebasGradle\holamundo> gradle init

Select type of build to generate:
  1: Application
  2: Library
  3: Gradle plugin
  4: Basic (build structure only)
Enter selection (default: Application) [1..4] 1

Select implementation language:
  1: Java
  2: Kotlin
  3: Groovy
  4: Scala
  5: C++
  6: Swift
Enter selection (default: Java) [1..6] 5

Project name (default: holamundo):

Select build script DSL:
  1: Kotlin
  2: Groovy
Enter selection (default: Kotlin) [1..2] 2

Generate build using new APIs and behavior (some features may change in the next minor release)? (default: no) [yes, no] no


> Task :init
To learn more about Gradle by exploring our Samples at https://docs.gradle.org/8.8/samples/sample_building_cpp_applications.html

Nos ubicamps en la carpeta app:

cd app

Este será el contenido del archivo gradle.build:

plugins {
    // Apply the cpp-application plugin to add support for building C++ executables
    id 'cpp-application'

    // Apply the cpp-unit-test plugin to add support for building and running C++ test executables
    id 'cpp-unit-test'
}

// Set the target operating system and architecture for this application
application {
    targetMachines.add(machines.windows.x86_64)
}

Editamos el programa:

app.cpp

#include <iostream>
#include <stdlib.h>
#include "app.h"

std::string holamundo::Greeter::greeting() {
    return std::string("Hola, mundo desde C++ y Gradle!");
}

int main () {
    holamundo::Greeter greeter;
    std::cout << greeter.greeting() << std::endl;
    return 0;
}

Construimos:

gradle build

Ejecutamos:

./app/build/exe/main/debug/app.exe

Salida:

Hola, mundo desde C++ y Gradle!

Editamos los archivos:

  • app.h 
  • app.cpp

Crearemos una nueva función que calcule el número de pulsaciones de una persona. Las variables de entrada de la función serán: edad y peso de la persona.

app.h

/*
 * This source file was generated by the Gradle 'init' task
 */

#ifndef APP_H
#define APP_H

#include <string>

namespace holamundo {
    class Greeter {
        public:
        std::string greeting();
        double pulsations(double weigth, int age);
    };
}

#endif

app.cpp

/*
 * This source file was generated by the Gradle 'init' task
 */

#include <iostream>
#include <stdlib.h>
#include "app.h"

std::string holamundo::Greeter::greeting() {
    return std::string("Hola, mundo desde C++ y Gradle!");
}

double holamundo::Greeter::pulsations(double weight, int age) {
    return 210 - (0.5 * age) - (0.11 * weight);
}

int main () {
    holamundo::Greeter greeter;
    std::cout << greeter.greeting() << std::endl;
    std::cout <<"Pulsaciones: "<< greeter.pulsations(56.0,42) << std::endl;
    return 0;
}

Construimos y ejecutamos:

gradle build
./app/build/exe/main/debug/app.exe

Salida:

Hola, mundo desde C++ y Gradle!
Pulsaciones: 182.84

Como se puede observar han habido algunos cambios a la hora de crear el proyecto. Además que ya no podemos usar la GUI de Gradle.

Enlaces:

https://codemonkeyjunior.blogspot.com/2021/09/gradle-creando-un-proyecto-c.html

No hay comentarios:

Publicar un comentario