1

(I've just edited the question to be more specific)

I know that this question might be too general to answer, but I'm just can not find good tutorial on this, so trying to seek for help here.

I'm new to C++, previously my main programing language is Java and Python. The way that C++ manage third-party lib is somehow confusing for me......While Python can easily install things with pip, Java can import the JAR, how C++ organize those things?

I would like to split my question into few parts: Here is some understanding and question of mine:

  1. As long as the compiler, or IDE, know the path of the lib, then everything is fine. So when saying install, we just add the path of the lib to some system path. And for IDE, we just config the setting so that it can resolve the lib in given path. Correct me if anything is wrong.

  2. Some C++ lib are all source code, and some contains sth. like .so or .dll, what is that? And what's the difference? I saw some lib saying that it can be used with simply include a few headers, but some require static linking, what does it mean?

  3. What's is a general good approach to manage all those lib? (For example, in python, pip will simply install to some global scope, or we use vitrual env to manage that. Then anything similar to pip in C++?

  4. More specifically, I'm using CLion, and Clion use CMake, so maybe all I suppose to do is config the CMakeList.text correctly and then the IDE will resolve all lib and compile correctly?

Again sorry for such general and somehow opaque question, but I'm totally lost as a newb for C++, which is much more complicated than Python and Java I used before.....

Any good tutorials might be of great help, thanks!

Ziqi Liu
  • 2,931
  • 5
  • 31
  • 64

2 Answers2

2

C++ doesn't. C++ is a language not a specific compiler or implementation.

With that said, for most compilers, building a C++ application is done in multiple steps:

  1. Edit
  2. Compile to object file
  3. Link to executable.

The C++ compiler is technically only involved in step 2 (and really only part of step 2).

Most compilers and linkers since long ago allow you to put header- and library-file anywhere, and then there are flags passed to the compiler and linker on the command-line that tell the compiler and linker where to find the files.

For header files the (common) command-line option -I (upper-case i) is used to add a path to be searched for header files. For libraries the option -L similarly adds a path to be searched by the linker for libraries. There are of course default paths built into the compiler and linker, and the -I and -L options adds to those defaults.

Then to link with an actual library, the linker-option -l (lower-case L) is the common option to use. Each -l options list a single library that needs to be linked into the executable.


In regards to CMake and CLion, the CLion IDE doesn't really link anything at all. Instead it uses CMake to create a set of makefiles which contains the information used to build the targets.


Lastly there are some C and C++ alternatives to PIP or other languages package managers, but generally you use the standard way to install programs and libraries on your system.

Like on Windows you find an installer, and then modify your project settings (using CMake CMakeLists.txt, raw Makefile, or IDE settings) to add the directories needed.

For Linux systems you use the standard package manager (like apt on Debian-based systems, or yum on Fedora-based systems, etc.) to find and install libraries. Then the libraries and their header files will be installed in the default locations. You still need to set up the build-environment to actually link to the libraries.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • 1
    Nitpicking, but the standard library is specified in the standard along with the ability of having multiple compilation units. So the *compiler* in only involved in step 2, but the language specifies a (very small) part of step 3. – Serge Ballesta Jul 24 '18 at 05:55
  • Thanks for the answer. So for any IDE, as long as I config it right(that means adding path of any lib so that the IDE will be able to resolve it), then the IDE will work correctly? (BTW I just edit my question to be more clear) – Ziqi Liu Jul 24 '18 at 05:56
  • @ZiqiLiu That's right. If you just put the right things in the right places (like libraries in `target_link_libraries` in your `CMakeLists.txt` file) then it should "just work" :) – Some programmer dude Jul 24 '18 at 05:58
  • 1
    @SergeBallesta isn't the standard library *specified* only as a bunch of headers which introduce symbols into relevant namespaces? The actual mechanism is left to the implementation. – Caleth Jul 24 '18 at 08:54
  • @Caleth: the standard fully specifies the behaviour of classes and functions of the standard library. And a conformant implementation shall respect that. That being said, little is said about the link phase besides *it shall produce an executable file from well formed compilation units and library modules*. – Serge Ballesta Jul 24 '18 at 09:03
  • @SergeBallesta what I mean is that `` need not exist a file, but as a static AST fragment included in the compiler binary (or whatever). I don't know of any implementations that do that, but it would be possible – Caleth Jul 24 '18 at 09:05
1

The common way is, that you include the thirdparty stuff as a .dll or you can include it direct as code (as example boost ... you have to load it and to make it work you only have to include the parts you want, and for some parts from boost you have to build it with your compiler settings and include the .dlls)

The thing with the manager like you want I only now from VisualStudio with NuGet. I have no idea if is there such a thing for CLion.

As example you can look to the example from opencv:

https://docs.opencv.org/master/d3/d52/tutorial_windows_install.html

For your questions:

  1. Correct. But in case the lib have to match also the settings (32/64 bit, release/debug)

  2. If you only have to include some headers, then the code is direct included to your project and compiled with your code. If you have to link it as a binary (.dll windows, .so Unix (i think please correct me if wrong)) than the code is compiled and you link the compiled functions to your code.

Here a answer to .so: What are .a and .so files?

And here for static and dynamic libs: When to use dynamic vs. static libraries

HEvTH
  • 126
  • 1
  • 8