Even as a Javaer, if you want to use some new version of the framework or software under the * Nix platform, you will inevitably have to download the source code from the official source and compile it into an executable. For example, compile Hadoop, Redis, or JDK source code (JVM: start manually compiling JDK – nuggets (juejin. Cn)). Even if we don’t specialize in C/C++ development, we should be familiar with the following commands:

./configue
cmake ..
make && make install
Copy the code

In addition, the official website usually requires that the compilation environment include but not limited to such tools as g++, GCC, make, cmake, etc. This article briefly covers the basics of these tools, including how to DEBUG across environments or write C/C++ code, using the CLion IDE as an example.

Tools needed to compile source code

If you only use the official framework, you can generally follow the official instructions to install the relevant version of the tool. If you are a C/C++ developer, the tools you choose will depend on your team’s needs.

gcc / g++

Simply put, GCC (GNU C Complier) handles C programs, while g++ (GNU C++ Complier) handles C++ programs. In fact, both can compile *.c and *.cpp files, the main difference being:

  1. GCC handles it separately*.c*.cppFile;
  2. G++ treats them both*.cppProcess.

GCC /g++ is not a compiler per se, but rather a driver: used to drive cc1, cc1plus, etc. When compiling files with g++, g++ automatically links to the standard library STL, whereas GCC does not automatically link to the STL.

make

The make tool solves compilation problems for a large C/C++ project. First, it needs a Makefile to indicate how it should work: which source files in the project need to be compiled and how to compile them, which intermediate files need to be created and how to create them, how to eventually produce the executable we want, and so on. However, since writing makefiles is a pain in its own right, it makes sense to introduce two more tools to make:./configure, Cmake. Both can help simplify writing makefiles.

We often use a combination of these two commands: make && make install. Make compiles the project and make install installs it.

./configure

./configure is essentially a build file automatically generated by a build tool called autoconf, which is stored as shell script and was the dominant C/C++ build tool before Cmake. Generate makefiles on behalf of the developer based on the configure rule file.

Cmake

The full name of Cmake is Cross Plaform Make. As the name suggests, it is a cross-platform compilation tool that can describe the installation/compilation process for all platforms in a simple statement.

For example, a Windows C/C++ developer might use Visual Studio’s built-in tools to build projects, while a Linux developer might use QT Creater to build projects. However, projects on two different platforms cannot be ported to each other. This is where CMake comes in. Developers can start by writing a cmakelists. TXT file, including the required.h and.cpp files, and then use the CMake tool to call their respective compilers on different platforms to generate their own platform-compatible projects (this is basically the same as Java cross-platform thinking).

Cmake generates makefiles on Linux/Unix platforms, Xcode on Mac, and MSVC engineering files on Windows.

In addition, when compiling the same project across platforms, environment mismatches are a major cause of runtime failures. If the default g++/ GCC in the OS is not an officially specified (or team-constrained) version, or if there are multiple versions of GCC /g++ in the OS, there are two ways to resolve this:

The first method is to enter the cmakelists.txt file for modification:

SET(CMAKE_C_COMPILER "/home/public/local/bin/gcc")
SET(CMAKE_CXX_COMPILER "/home/public/local/bin/g++")
Copy the code

The second method is to actively declare environment variables before calling the cmake tool (this is temporary and will be invalidated after exiting the command line) :

export CC=/usr/local/bin/gcc
export CXX=/usr/local/bin/g++
cmake /path/to/your/project
make
Copy the code

Make lacks a dependency library solution

In a few cases, the make source process can go wrong because of the lack of dependent libraries:

canning dependencies of target bitmap_test [ 91%] Building CXX object unitest/CMakeFiles/bitmap_test.dir/bitmap_test.cpp.o [ 92%] Linking CXX executable .. /bin/bitmap_test /home/lijunhu/Documents/miniob_X/build/bin/bitmap_test: error while loading shared libraries: Libevent-2.2.so.1: Cannot open shared object file: No such file or directoryCopy the code

For example, I have encountered this error while compiling the googleTest framework using the make tool. The error appears to be due to the lack of the dependent libevent-2.2.so. There are two possibilities:

  1. This library is missing.
  2. This library is not in the system’s search path.

You can use the find command to check whether the library exists in the environment:

find / -name xxx.so
Copy the code

If the library file does exist, then the only possibility is that it is not on the system’s library file (so) search path. The first method is to move the library file to /usr/lib:

sudo cp xxx.so /usr/lib/
Copy the code

Another good solution is to use LDConfig to add the system’s search path.

Step 1 edit the link configuration file. Edit vim /etc/ld.so.conf and check whether the content is as follows. If not, change it to the following:

include /etc/ld.so.conf.d/*.conf
Copy the code

Step 2 Go to /etc/ld.so.conf.d and create the *. The file name is optional, and the extension must be.conf.

cd /etc/ld.so.conf/
vim mylib.conf 
Copy the code

Step 4 Add the. So path to the file, save the file, and exit.

Step 5. The command takes effect during execution.

sudo ldconfig
Copy the code

When the program looks for the.so library at runtime, it looks in the added directory.

Remotely deploy/debug C/C++ code with CLion

This remote DEBUG approach can be extended to other JET BRAINS compilers such as IntelliJ IDEA, Goland, etc.

For secondary development purposes, or for debugging purposes, we may sometimes need to compile C/C++ across platforms. For example, my local environment is Windows, but my work environment is Linux. Therefore, to install make on Windows, you must first install a MinGW tool and then select the packages to download.

If you already have an existing C/C++ environment in the cloud server/virtual environment, you can simply write the source code and upload the source code to the existing environment for compilation in Run/Debug.

  1. Encode locally.
  2. Before compiling, send the local code to the remote end.
  3. Use remote Cmake, make and other tools to compile, DEBUG.

This idea of remote work can be extended to the case of connecting to a cloud server, referred to simply as remote.

Compile and DEBUG using a remote environment

The native is only responsible for writing code, so there is no need for additional Cmake, make tools, etc. Create a new Remote Host in the ToolChains option and establish an SFTP connection with the Remote end.

First, you need to configure the GCC, g++ environment to ensure the running of the CMake tool. Note that the path of the C Compiler and C++ Compiler is actively configured by the author, so the path of GCC /g++ is manually changed here. By default, the IDE automatically probes CMake, make, GCC, g++ paths after a successful connection. After the configuration is complete, select OK.

In the Cmake TAB, select the Toolchains you just configured and select OK.

After that, subsequent Run/DEBUG of the program takes place remotely. Then you just need to program locally, send the code remotely, and DEBUG it. At every DEBUG, the code is automatically deployed to the remote/TMP /… Save and run in a temporary folder.

Commit only the project source code to the remote end

If you simply want to upload code to a remote end, open Tools -> Development in the upper window of CLion to set:

On the Configuration screen, create a connection and select SFTP as the connection mode.

After Connection is filled in, do not close the window, go to Mappings and configure the path where the local code is uploaded to the remote end.

When all of these are configured, select OK to save the configuration. Now right-click Development in the project root (or go to Tools -> Development) and you can see the Upload option:

You can set CLion to upload code updates in real time.

The resources

CLion Remote development and debugging of C/C++ code – IUNI_JM – Blogpark (cnblogs.com)

CLion implements remote debugging _Leo’s blog -CSDN blog _Clion remote debugging

The difference between cmakelists.txt and Makefile _Jay463261929’s blog -CSDN Blog

C++ programming basics (ii) — Makefile and cmake introduction _potxxx blog -CSDN blog _cmake Makefile

What is the relationship between GCC and g++? – zhihu (zhihu.com)

Configure, make, make install

A Brief introduction to make Tools – Galaxy – Blogpark (CNblogs.com)

CLion Remote development and debugging of C/C++ code – IUNI_JM – Blogpark (cnblogs.com)

CLion implements remote debugging _Leo’s blog -CSDN blog _Clion remote debugging