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 :
- Search for
-I
Specified directory - Search for gcc environment variable
C_INCLUDE_PATH
( If it is C++ Program , Then forCPLUS_INCLUDE_PATH
) The directory specified in - 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 :
- Search the current directory
- Search for
-I
The directory specified by the parameter - Search for gcc environment variable
C_INCLUDE_PATH
( If it is C++ Program , Then forCPLUS_INCLUDE_PATH
) The directory specified in - 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
andCPLUS_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 :
- Search for
-L
+-l
Parameter specified - Search for environment variables
LIBRARY_PATH
+ Parameters-l
designated - 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 :
-
The dynamic library search path specified when compiling the object code
-
environment variable
LD_LIBRARY_PATH
Specified dynamic library search path -
The configuration file
/etc/ld.so.conf
The dynamic library search path specified in -
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
The sidebar is recommended
- Spring IOC container loading process
- [thinking] the difference between singleton mode and static method - object-oriented programming
- Hadoop environment setup (MySQL environment configuration)
- 10 minutes, using node JS creates a real-time early warning system for bad weather!
- Git tool
- Force deduction algorithm - 92 Reverse linked list II
- What is the sub problem of dynamic programming?
- C / C + +: static keyword summary
- Idea does not have the artifacts option when configuring Tomcat
- Anaconda can't open it
guess what you like
-
I don't know how to start this
-
Matlab simulation of transportation optimization algorithm based on PSO
-
MySQL slow log optimization
-
[Vue] as the window is stretched (larger, smaller, wider and higher), the text will not be displayed
-
Popular Linux distributions for embedded computing
-
Suzhou computer research
-
After installing SSL Certificate in Windows + tomcat, the domain name request is not successful. Please answer!!
-
Implementation time output and greetings of jQuery instance
-
The 72 year old uncle became popular. Wu Jing and Guo fan made his story into a film, which made countless dreamers blush
-
How to save computer research
Random recommended
- Springboot implements excel import and export, which is easy to use, and poi can be thrown away
- The final examination subjects of a class are mathematical programming, and the scores are sorted and output from high to low
- Two pronged approach, Tsinghua Professor Pro code JDK and hotspot source code notes, one-time learning to understand
- C + + recursive knapsack problem
- The use of GIT and GitHub and the latest git tutorial are easy to understand -- Video notes of crazy God speaking
- PostgreSQL statement query
- Ignition database test
- Context didn't understand why he got a high salary?, Nginxfair principle
- Bootstrap switch switch control user's guide, springcloud actual combat video
- A list that contains only strings. What other search methods can be used except sequential search
- [matlab path planning] multi ant colony algorithm grid map path planning [including GUI source code 650]
- [matlab path planning] improved genetic algorithm grid map path planning [including source code phase 525]
- Iinternet network path management system
- Appium settings app is not running after 5000ms
- Reactnative foundation - 07 (background image, status bar, statusbar)
- Reactnative foundation - 04 (custom rpx)
- If you want an embedded database (H2, hsql or Derby), please put it on the classpath
- When using stm32g070 Hal library, if you want to write to flash, you must perform an erase. If you don't let it, you can't write continuously.
- Linux checks where the software is installed and what files are installed
- SQL statement fuzzy query and time interval filtering
- 69. Sqrt (x) (c + + problem solving version with vs runnable source program)
- Fresh students are about to graduate. Do you choose Java development or big data?
- Java project: OA management system (java + SSM + bootstrap + MySQL + JSP)
- Titanic passenger survival prediction
- Vectorization of deep learning formula
- Configuration and use of private image warehouse of microservice architect docker
- Relearn JavaScript events
- For someone, delete return 1 and return 0
- How does Java dynamically obtain what type of data is passed? It is used to judge whether the data is the same, dynamic data type
- How does the database cow optimize SQL?
- [data structure] chain structure of binary tree (pre order traversal) (middle order traversal) (post order traversal) (sequence traversal)
- Webpack packaging optimization solution
- 5. Operation element
- Detailed explanation of red and black trees
- redhat7. 9 install database 19C
- Blue Bridge Cup notes: (the given elements are not repeated) complete arrangement (arrangement cannot be repeated, arrangement can be repeated)
- Detailed explanation of springboot default package scanning mechanism and @ componentscan specified scanning path
- How to solve the run-time exception of test times
- Detailed explanation of k8s management tool kubectl
- Android system view memory command