When working with C++, the choice of compiler is very important. Unlike other languages, C++ programs are ultimately composed of header files (.h) and library files (.dll or.so). Different applications (libraries) also need to support the files and libraries, and the libraries on different platforms are not consistent, so in cross-platform development, we prefer to use CMake to compile and link.

So cmake is not a package management tool, it doesn’t solve the package dependency problem, it just solves the cross-platform problem.

The compiler

Turning source code into an executable application requires something called a compiler, if you’re using established third-party libraries in your application, or if your own application is large and requires multiple modules. You also need to wire these simulations together to form a usable application.

For example, ina standard Spring Boot project, you can build a xx.jar containing your own and other third party libraries that can be run directly, and a xx.jar. Original file containing only your own libraries that can be used as third party libraries.

The process of turning the source code into executable bytecode is called compilation, and the process of linking the libraries generated by the source code itself with other libraries to form an executable application is called chaining.

The tools that do this are called compilers.

In Java development, we need to download different JDKs for different operating systems. Part of the role of the JDK is to be the Java compiler. In C++ development, a compiler called Clang was installed by default in MacOS, so we didn’t need to install it separately.

You need to have different C ++ compilers on different systems. For example, MacOS has Clang installed by default. In addition, the compiler has GNU(GCC, g++). Different compilers have their own characteristics, and we incorporate these characteristics into our source code as we develop our applications. The compiler then compiles the source code into a byte file. Some features are only supported by certain compilers, which is why some applications will only work if compiled with certain compilers.

Dependency management

Java has a more famous Maven, Gradle package manager, it can automatically download the dependency for us, then call the specified JDK for compilation, can complete the Java application packaging work.

Unfortunately, there are no such powerful package management tools in C ++ applications. But for a project with multiple modules and dependencies, the idea of implementation is still the same.

Like the packages managed under NPM, the required package and version number are declared in package.json, and then installed using the NPM install command. C++ applications can declare version numbers in cMakelists.txt. Then use other methods to install.

Load depends on

We mentioned earlier that cmake is a cross-platform compiler, and its corresponding configuration file is cmakelists.txt, which explains the dependencies on other third-party packages.

find_package()

CMakelists.txt can be used to declare dependencies in two general ways. The first is using find_package. For example: find_package(JsonCpp REQUIRED). At this point, CMake will first look for its internal package. If not found in the internal package, it will look for the file findjsoncpp.cmake in the current directory. If the file findjsoncpp.cmake is found, the package will be loaded according to the Settings of the file. If it is not found, an exception is thrown because the second parameter is set to Required.

So if some third-party packages provide the findXXXX. Cmake file, you can easily use the find_package(name, REQUIRED) function to load third-party packages.

In general, if a third-party package is successfully found, the following variables will be defined:

# When a package is found, This value is true: Xxxx_FOUND # The location of the header file is Xxxx_INCLUDE_DIRS or Xxxx_INCLUDES # The location of the package library is Xxxx_LIBRARY or Xxxx_LIBS # You can use message(${Xxxx_LIBRARIES}) to print the value of the variable

pkg_check_modules()

The second way is with the help of PkgConfig:

find_package(PkgConfig REQUIRED)

A PkgConfig error is not found when PkgConfig is not installed on the system, and PkgConfig needs to be installed on the system.

With PkgConfig, you can use the pkg_check_modules package detection method provided by PkgConfig to get information about third-party packages:

pkg_check_modules(JSONCPP jsoncpp)

JSONCPP is the alias name of the module.

The system variable jsoncpp __found is true if JSONcpp exists. Otherwise, it is false. If the current project must depend on a third party package, then you can add REQUIRED, such as pkg_check_modules(XXX XXX REQUIRED). An exception will occur if XXX does not exist.

loading

Fetching third-party dependent packages through either find_package() or pkg_check_modules essentially checks to see if a package exists, returns information about the package if it does, and sets the system variable Xxxx_FOUND to false if it does not. Beyond that, it has no other effect.

In other words, the above two methods do not load dependencies on their third parties. This is why you need to use commands to find information about a package (package library, header location), also for cross-platform reasons. In the cross-platform case, we cannot use any form such as C: XXX \ XXX in Windows or ~/ XXX/XXX in XUNIX. So before loading a dependency, you need to dynamically retrieve the package information using the above method, either in the form of c: XXX \xx if you are using Windows, or in the form of /usr/local/xxx if you are using XX.

Loading third-party libraries requires that you include the header directories that use include_directories at your location, and link the library in with the target_link_libraries link.

include_directories(project-name ${Xxxxx_INCLUDE_DIRS})
target_link_libraries(project-name ${Xxxxx_LIBRARIES})

A package manager

Microsoft provides VCPKG to install C ++ application dependencies. Use shell to install as follows:

# clone git clone https://github.com/Microsoft/vcpkg.git - the depth = 1 # warehouse into the warehouse CD VCPKG # run the setup script. / the bootstrap - VCPKG. Sh # VCPKG integrate install: integrate integrate install VCPKG integrate install: integrate install VCPKG integrate install

After performing the full bureau installation, you will get a prompt:

panjie@panjies-iMac vcpkg % ./vcpkg integrate install
Applied user-wide integration for this vcpkg root.

CMake projects should use: "-DCMAKE_TOOLCHAIN_FILE=/Users/panjie/github/Microsoft/vcpkg/scripts/buildsystems/vcpkg.cmake"

It says that if we use the CMake tool again, we need to append the above parameter to the CMake command. If we are using the CLion editor, we need to add it to CMake options:



Cmake in Clion will automatically add the above parameters at execution time. Since then, CLion has been able to work happily with CMake and the VCPKG package managers.

Install specific packages

/ VCPKG install jsoncpp: / VCPKG install jsoncpp: / VCPKG install

panjie@panjies-iMac vcpkg % ./vcpkg install jsoncpp Computing installation plan... . The package jsoncpp:x64-osx provides CMake targets: find_package(jsoncpp CONFIG REQUIRED) target_link_libraries(main PRIVATE jsoncpp_object jsoncpp_static)

Finally, follow the instructions and add the above code to your own project, cmMakelists.txt, and you’ll be happy to use the third party JSONCPP package in your project:

    add_executable(yz-main yz-main.cpp)
    find_package(jsoncpp CONFIG REQUIRED)
    target_link_libraries(yz-main PRIVATE jsoncpp_object jsoncpp_static)    

JSONcpp can then be used freely in the current project to complete the serialization and deserialization of JSON.

conclusion

Finally, Java and C ++ projects are compared and summarized:

  1. Both are compiled languages and need to be compiled first.
  2. Java source files are compiled using JDK, and different operating systems need to install different JDK installers. C++ needs to be compiled using the C++ compiler, and different operating systems need to download different compilers.
  3. There are several versions of the Java compiler, such as Oracle’s own JDK or third-party OpenJDK. There are also several versions of the C++ compiler, such as Clang and GNU. In the actual development process, you need to download and install the configuration according to your own needs.
  4. Java has a unified package management, installation toolsmavenorgradle; C++ has a unified package management toolvcpkg, there is a unified installation toolscmake.
  5. vcpkgWould like tocmakeHappy work, need to configure some parameters. This parameter is different for different computers, so it needs to be installedvcpkgThe situation is configured.