Oh, Al Fin Existen Package Managers Para C++?

Yo se que ya he hablado de esto demasiado en otros artículos, per después de trabajar en idiomas como Python, es muy fácil hacerse cómodo usando algo como pip para instalar todo los modulos. Necesitas Pytorch? Solomente hay que iniciar pip install torch and you can use it as soon as it is finished installing (usually).  When you step into the world of C++ though, it is really not as easy as just grabbing one of the major c++ package managers.

C++ has a few ways to install packages and generally none of them are as simple as pip. On top of that, there is a lot of information out there on the “proper” way to install packages.  Some developers swear by using git submodule add to include a project directly from github or gitlab into their projects.  Others will use a system package manager like apt or yum (and now chocolatey on Windows too), which is almost as simple as using something like pip.

Pues, la verdad es que existe otro candidato: Packager Managers por C++. Ya existen varios, pero los dos más conocidos son Conan y vcpkg. Package Managers por C++ funcionan en varios sistemas y resuelven conflicts de versiones entre paquetes, y hacen actualizaciones más fácil cuando paquetes tienen versiones nuevos. Supongo que los lectores Perception ML ya saben idiomas que tiene Package Managers como pip o npm, así que no voy a hablar sobre los detalles.

Empezando Con Vckpg

En este punto, estoy seguro que estás listo aprender como usar un Package Manager, y por eso probamos a vckpgEsto es completamente mi preferencia, entonces si preferés usar Conan no hay problema, pero esta vez solo voy a introducir uno. Las preparaciones para usar vckpg son bastante fácil y lo podés ver en su pagina Getting Started.  

Es bastante simple, pero yo prefiero que todos mis proyectos tienen un ambiente Docker, y por eso te muestro como lo hago. Esto puede ser cualquier imagen de Docker hecho para C++. Podés clone vcpkg y hacerlo to mismo en tu propio proyecto en la misma manera que yo lo hice.

Aquí tenemos un ejemplo de mi Dockerfile:

Dockerfile
FROM gcc:13.2.0

# Only set this if you are working on an ARM device
ENV VCPKG_FORCE_SYSTEM_BINARIES=1

RUN apt update
RUN apt -y install git ninja-build cmake build-essential tar curl zip unzip bison libdbus-1-dev libxi-dev libxtst-dev

RUN ln -s /usr/bin/ninja /usr/bin/ninja-build

RUN git clone https://github.com/Microsoft/vcpkg.git

RUN ./vcpkg/bootstrap-vcpkg.sh

ENV PATH=/vcpkg/:${PATH} 

WORKDIR /code

Version Control con Vckpg

When you make a project that uses vcpkg, there are two ways to use vcpkg.  Classic mode lets you just install packages the same way you would with a system package manager.  That’s not great though when you are trying to follow good development practices.  Instead, the recommended mode is manifest mode.  To use manifest mode all you need to do is add a manifest file, which can be as simple as this:

JSON
{
    "dependencies": [
      "eigen3"
    ]
}

My example here is just a super simple manifest mode that only contains Eigen3 in it.  However, if you check out their documentation on manifests, you can get much more detailed with it, including adding versions, etc.

Usando CMake Juntos con Vckpg

This is optional if you are using something else to build with, but vcpkg works pretty seamlessly together with cmake.  To add packages from vcpkg you just need to set your toolchain file to the vcpkg.cmake file inside wherever you installed vckpg.  Then you can add your packages using find_package and link them to your executable with target_link_libraries.

Aquí tenemos un ejemplo de eso.

CMake
cmake_minimum_required(VERSION 3.8)

set(CMAKE_TOOLCHAIN_FILE vcpkg/scripts/buildsystems/vcpkg.cmake)

project(main)

add_executable(main main.cpp)

find_package(Eigen3 3.3 REQUIRED NO_MODULE)
target_link_libraries(main PRIVATE Eigen3::Eigen)

Cheat Code

If you don’t have any requirements for which docker image to use, you could always use a Microsoft C++ image.  They already have vcpkg installed on them so setup is super simple. There’s a copy of an example dockerfile and docker-compose.yaml in this repository.  The only thing you have to be careful of is where you set your toolchain file, which you will need to confirm the location of before running cmake, but that’s easy right!

Dockerfile
FROM mcr.microsoft.com/devcontainers/cpp:ubuntu

Source Code

Para el source code usado en este articulo, incluyendo a un archivo docker-compose.yaml para hacer el proceso de construcción más fácil, podés ver este folder en el git repo de este sitio. Ademas, hay un directorio con el ejemplo del Cheat Code también.

Conclusión

Yo creo que vckpg es bastante fácil y directo para usar y integrar con cmake. Intento usarlo en la mayoridad de mis proyectos de C++ porque creo que es una mejor manera de mantener dependencias que instalar manualmente o añadir proyectos como git submodules.

La unica desventaja que yo veo de usar vcpkg es que paquetes no están siempre mantenidos y tenía problemas tratando de instalar libtorch, que al fin no podía instalar correctamente. Pero eso es algo que puedo soportar. Si hubiera un paquete que no se puede instalar con vckpg, yo lo usaría para el resto de los paquetes y instalarlo con el paquete con el gestor de paquetes sistema o con un submodule. Aunque, espero que no es algo común.

For more content on C++ check out some of my other articles!


Publicado

en

por

Etiquetas:

Comentarios

Deja un comentario

es_ARES