C++ how to manage dependencies (use libraries from

2020-06-08 03:11发布

问题:

I'm very new to C++ world, so please, sorry me for such a dummy question. I google a little, but wasn't able to find proper answer.

My question is fairly simple - how should I use lib's in C++ world. For example in Java - there is maven and gradle for this task. In Python - I use pip. In javascript npm and bower do all the stuff. In C# you use nuget or just adding DLL lib to your project. But looks like in C++ things isn't such easy.

I found a tool, called conan but ammount of libs they have is pretty small and don't include any what i'm loking for.

So, for example - I want to use nlp lib meta. Seem's like they don't provide any installer file. So I assume I need to get sources from github. Should I compile them and then trying to add compiled files to my project or I need to have lib folder in my project, and put meta's sources in those folder and after operate with meta's sources as they are in my project?


My question isn't about how to install specific meta lib, but more from the source management point of view. If I use Visual Studio on Windows for example, but my colegue will be coding Clion under Linux. And I wan't to realize which is a proper way to managing dependencies in C++ world.

回答1:

C++ doesn't have anything like pip or npm/bower. I don't know if maven or gradle can be persuaded to handle C++ libraries.

In general, you are going to have to end up with

  • Header files in a directory somewhere
  • library files (either static libraries, or DLLs/shared objects). If the library is a header-only library like some of the boost libraries, then you won't need this.

You get hold of the library files, either by building them on your machine (typical for open source projects, and projects aimed at Linux platforms), or by downloading the pre-compiled binaries (typical for Windows libraries, particularly paid-for).

Hopefully, the instructions for building the library will be included on the library website. As noted in the comments, 'meta' seems to be quite good at that.

When you try to compile with the library, you may need a command line option (eg -I) to specify the directory containing the header files, and you may need a linker option (eg -l) to tell the linker to link against your library.



回答2:

Cget will install any package that uses standard cmake, and works for linux and windows. It has shorten syntax for getting packages directly from github(such as cget install google/googletest).

In addition, dependencies can be automatically downloaded as well by listing them in a requirements.txt file.

There is also recipes for installing non-cmake packages and the repository here has over 300 libraries(and growing). So you can install curl with just cget install pfultz2/cget-recipes curl.



回答3:

C++ sadly has no package manager for libraries. Some are out there and try to be one which are still small and scattered though (like conan).

In linux you have some "-dev" packages you can install but they are also not "all".

You most likely end up downloading them yourself. Next though is you have the problem of integrating those libraries. You have different build systems per operating system so you have to see how you build c++ files.

Like in windows with Visual studio you have to get a visual studio project or a nmake compatible makefile to build the libraries and then add them to your project. Same with linux makefiles.

There are several build frameworks who are higher level like cmake. The example you have in your post also works with CMake. So integrating that one into a cmake build environment would be easier but this only applies for other libraries also trying to use/integrate cmake build environments to it (e.g. boost / qt is doing this).

Yeah these are some thoughts to this. Sadly there won't be an easy/definitive answer to this because there is no real central c++ packet repository which is also integrated into a build system.



回答4:

It appears to me that the Crascit/DownloadProject could be of help in your situation. It provides CMake plugins for downloading projects from a git repository by specifying tags, etc. Then you can use add_custom_target to run commands you need to have the project built.



回答5:

There are a number of popular C++ released via nuget packages. You can search on the gallery for them, usually using the native or c++ tags. Obviously you need a nuget manager for your OS, and I'm pretty sure that the C++ nuget packages rely on MSBuild for a lot of the grunt work, so you may have trouble getting a non-Visual Studio oriented setup to work nicely.

Also Gradle actually does have some support for native dependencies as well. I had a look at little while ago but the work on it was curtailed because the support for VS 2015 was lacking.



回答6:

I recommend vcpkg for cross platform development. It has a number of IDE integrations. GitHub project is here.

I do cross platform development using tools like CMake, Visual Studio, WSL. vcpkg was incredibly helpful.



回答7:

I started new project... in cureent time it's just "source package manager" you can provide some source code on github and then it will be just copy to you project (based on cmake + auto generating cmake files)

So links here: https://github.com/wsjcpp/wsjcpp