jueves, 2 de septiembre de 2021

Gradle: creando un proyecto C++

Gradle ha evolucionado de ser una alternativa a Maven a ser algo más. Ahora se puede crear y administrar proyectos Kotlin, Swift y hasta C++. en este post veremos:
  1. Crear una carpeta para un proyecto C++
  2. Generar un proyecto C++ con Gradle
  3. Editar archivo principal
  4. Construir y ejecutar
1. Crear carpeta del proyecto:
$ mkdir pruebasCpp && cd pruebasCpp
2. Generar proyecto C++ con Gradle:
$ gradle init


Select type of project to generate:
  1: basic
  2: application
  3: library
  4: Gradle plugin
Enter selection (default: basic) [1..4] 2
Elegimos la opción 2, porque queremos crear una aplicación.
Select implementation language:
  1: C++
  2: Groovy
  3: Java
  4: Kotlin
  5: Scala
  6: Swift
Enter selection (default: Java) [1..6] 1
Elegimos la opción 1, porque queremos usar C++.
Select build script DSL:
  1: Groovy
  2: Kotlin
Enter selection (default: Groovy) [1..2] 1
Elegimos la opción 2, porque... solo para variar un poco.
Listemos el el contenido del directorio:
$ tree

.
├── app
 ├── build
  ├── exe
   └── main
       └── debug
           └── app
  ├── install
   └── main
       └── debug
           ├── app
           └── lib
               └── app
  ├── obj
   ├── for-test
    └── main
        └── debug
            └── app.o
   └── main
       └── debug
           └── c0rn65lkroj8p4fvz0qgmzzjr
               └── app.o
  ├── test-results
   └── test
  └── tmp
      ├── compileDebugCpp
       ├── options.txt
       └── output.txt
      ├── compileTestCpp
       ├── options.txt
       └── output.txt
      ├── linkDebug
       ├── options.txt
       └── output.txt
      └── linkTest
          ├── options.txt
          └── output.txt
 ├── build.gradle.kts
 └── src
     └── main
         ├── cpp
          └── app.cpp
         └── headers
             └── app.h
├── gradle
 └── wrapper
     ├── gradle-wrapper.jar
     └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
└── settings.gradle.kts

Observemos nuestro archivo "build" generado.
build.gradle.kts
/*
 * This file was generated by the Gradle 'init' task.
 *
 * This generated file contains a sample C++ project to get you started.
 * For more details take a look at the Building C++ applications and libraries chapter in the Gradle
 * User Manual available at https://docs.gradle.org/7.2/userguide/building_cpp_projects.html
 */

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

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

// Set the target operating system and architecture for this application
application {
    targetMachines.add(machines.linux.x86_64)
}
3. Vamos a la carpeta donde esta el archivo principal y lo editamos:
app.cpp
/*
 * This C++ source file was generated by the Gradle 'init' task.
 */

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

std::string demo::Greeter::greeting() {
    return std::string("Hola mundo desde Codemonkey Junior!!");
}

int main () {
    demo::Greeter greeter;
    std::cout << greeter.greeting() << std::endl;
    return 0;
}
4. Contruimos y ejecutamos:
$ ./gradlew build

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

Hola mundo desde Codemonkey Junior!!
En próximos post veremos más ejemplos.

Enlaces:
https://gradle.org/

No hay comentarios:

Publicar un comentario