current position:Home>Common problems and solutions in the process of compiling, linking and loading CC + + programs under Linux

Common problems and solutions in the process of compiling, linking and loading CC + + programs under Linux

2022-01-27 01:38:14 Adenialzz

Linux Next C/C++ Common problems and solutions in the process of program compilation, link loading

1 The header file contains problems

Error message

This error usually occurs at compile time , Common error messages are as follows :

run.cpp:2:10: fatal error: dlpack/dlpack.h: No such file or directory
 #include <dlpack/dlpack.h>
          ^~~~~~~~~~~~~~~~~
compilation terminated.

The problems contained in the header file are often encountered by novices , ad locum , We need to understand first gcc About the relevant process of header file search 、 Parameter options and environment variables , After that, the related problems contained in the header file are solved .

Search order of header files

Let's first look at the search order of header files , We know , stay C/C++ In the source file , There are two forms of including header files , That is, angle brackets and double quotation marks are used to contain :#inlcude <xxx> and #include "xxx" .

<>

For header files contained in angle brackets , Usually the search order is :

  1. Search for -I Specified directory
  2. Search for gcc environment variable C_INCLUDE_PATH( If it is C++ Program , Then for CPLUS_INCLUDE_PATH) The directory specified in
  3. Search for gcc Default directory for :
    • /usr/include
    • /usr/local/include
    • /usr/lib/gcc/x86_64-linux-gnu/7.5.0/include

“”

For header files with double quotation marks , We know that , It is mainly to search for the current working directory , The detailed search order in this case is as follows :

  1. Search the current directory
  2. Search for -I The directory specified by the parameter
  3. Search for gcc environment variable C_INCLUDE_PATH( If it is C++ Program , Then for CPLUS_INCLUDE_PATH) The directory specified in
  4. Search for gcc Default directory for :
    • /usr/include
    • /usr/local/include
    • /usr/lib/gcc/x86_64-linux-gnu/7.5.0/include

You can see , be relative to <> How to include ,"" Is to search the current directory first .

in addition , To point out , The compiler searches for header files in the above order , Once you find it, you won't be ready to move on , in other words , If there is a header file with the same name , The header files in the lower search directory will be blocked by the upper search directory .

resolvent : Compile parameter options and environment variables

Through the compilation parameter options and environment variables related to the included header file introduced above, it is not difficult for us to find , When faced with the error that the header file cannot be found , There are two ways to tell the compiler to find the directory of the required header file : namely ** By compiling parameter options or environment variables **.

  • Compile parameter options -I Parameter specifies that the header file that the compiler will search contains the directory .
  • environment variable C_INCLUDE_PATH and CPLUS_INCULDE_PATH Assign separately C Code and C++ The header file that the code needs to search contains the directory .

Note that this is not by PATH Environment variable specified ,PATH There are some executable files in the directory , It is not used to specify the search directory of header files ,

Here's the thing include Default directory for , It's not made up of $ATH Environment variable specified ( That is, in all the behaviors contained in the header file , and PATH Environmental variables have nothing to do with a dime ), But by the g++ Configuration of prefix designated , Can pass gcc -v Check it out. :

Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/7/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 7.5.0-3ubuntu1~18.04' --with-bugurl=file:///usr/share/doc/gcc-7/README.Bugs --enable-languages=c,ada,c++,go,brig,d,fortran,objc,obj-c++ --prefix=/usr --with-gcc-major-version-only --program-suffix=-7 --program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --enable-bootstrap --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-libmpx --enable-plugin --enable-default-pie --with-system-zlib --with-target-system-zlib --enable-objc-gc=auto --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-offload-targets=nvptx-none --without-cuda-driver --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 7.5.0 (Ubuntu 7.5.0-3ubuntu1~18.04)

2 Undefined references when compiling connections

Error message

Common error messages are as follows :

/tmp/ccWYumCZ.o: In function `main':
run.cpp:(.text+0x8f): undefined reference to `tvm::runtime::Module::LoadFromFile(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
collect2: error: ld returned 1 exit status

Library search order when compiling Links

First of all, we should clarify the search order of the library when compiling the connection :

  1. Search for -L + -l Parameter specified
  2. Search for environment variables LIBRARY_PATH + Parameters -l designated
  3. Search the default directory for
    • /lib
    • /usr/lib
    • /usr/local/lib

resolvent : Compile parameter options and environment variables

Similar to the problem that the header file above contains , The solution still depends on adding compilation parameter options and environment variables . What needs to be noted here is :-L Parameter options and LIBRARY_PATH Environment variables can be responsible for specifying the search directory of the library , and -l The parameter option is to specify the name of the specific library file in the search directory . in other words , -L and LIBRARY_PATH A choice , Plus -l Parameters , Talent .

Let's say we have /home/song/tvm/build/ Under the table of contents , There are two library files libtvm.so and libtvm_runtime.so, We're going to use them , There are two ways :

  • Method 1 :

    export LIBRARY_PATH=/home/song/tvm/build/:$LIBRARY_PATH
    gcc main.cpp -ltvm -ltvm_runtime
    
  • Method 2 :

    gcc main.cpp -L/home/song/tvm/build/ -ltvm -ltvm_runtime
    

May refer to :gcc Parameters -i, -L, -l, -include

3 Problems with runtime link libraries

Error message

The problem occurs at runtime , That is, we have obtained the executable a.out 了 , However, an error is reported at runtime, which is similar to :

./a.out: error while loading shared libraries: libtvm.so: cannot open shared object file: No such file or directory

We can also use tools ldd Check it out. a.out Whether all libraries that need to be linked at run time are available :

ldd a.out
#  Output :
	linux-vdso.so.1 (0x00007ffc0ebca000)
	libtvm.so => not found
	libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f1b829b7000)
	libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f1b8279f000)
	libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f1b823ae000)
	libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f1b82010000)
	/lib64/ld-linux-x86-64.so.2 (0x00007f1b82f45000)

You can see , Indeed, as indicated by the error message ,libtvm.so It can't be found .

The search order of the runtime link library

Let's first look at the search order of the runtime link library :

  1. The dynamic library search path specified when compiling the object code

  2. environment variable LD_LIBRARY_PATH Specified dynamic library search path

  3. The configuration file /etc/ld.so.conf The dynamic library search path specified in

  4. Default dynamic library search path

    • /lib

    • /lib64

    • /usr/lib

It should be noted that the dynamic library search path does not include the current folder , So when even the executable and what it needs so The files are in the same folder , It will also appear that it cannot be found so The problem of , It's like #include <xxxx> Do not search the current directory

resolvent : Compile option parameters 、 environment variable 、 The configuration file 、 Soft link

The method of compiling option parameters and environment variables here is similar to the above , I won't go into that here , Talk about the method of configuration file and soft link :

modify /etc/ld.so.conf file

stay /etc/ld.so.conf Add the path of the runtime library to the file . And then execute ldconfig command .

Or in /etc/ld.so.conf.d Add a new... To the directory .conf A new file , Then enter the new path in the file , And then execute ldconfig command :

vim /etc/ld.so.conf.d/MyLibrary.conf		#  Add the search path you need 
sudo ldconfig

Add a soft link to the runtime library

It can be used ln Command to create a soft link to the runtime library , And put the soft link under the system default path , Then, when linking the program, you only need to link the soft link of the dynamic library . The advantage of this is that when the dynamic library is upgraded , Just replace the old soft link , There is no need to modify the compilation command . In fact, the above question 2 You can also consider using this method .

Ref:

http://blog.sina.com.cn/s/blog_7195909a0100zi7i.html

https://blog.csdn.net/Damocles_shi/article/details/104085803

copyright notice
author[Adenialzz],Please bring the original link to reprint, thank you.
https://en.cdmana.com/2022/01/202201270138107816.html

Random recommended