[ubuntu] The solution to the error reported when linking the dynamic library

Problem Description

linkRegarding the solution to create a dynamic library by yourself in the ubuntu environment, an error occurs during the running phase, and it cannot be loaded into this library

dargon@dd:~/桌面/CSAPP/TestCode$ ./snooze 
./snooze: error while loading shared libraries: libcsapp.so: cannot open shared object file: No such file or directory
  • Method 1: The current location of the library under export

    At this time, because it will libcsapp.sobe placed /usr/local/libunder the directory, but it is added for the first time, linkwhen the program is in the stage, an error will occur.

    1. # 查看下变量 LD_LIBRARY_PATH 
      dargon@dd:~/桌面/CSAPP/TestCode$ echo $LD_LIBRARY_PATH
      
      # 设置 LD_LIBRARY_PATH 
      # Note: 第一次用,应该直接export,否则会报错:No command exist 
      dargon@dd:~/桌面/CSAPP/TestCode$ export LD_LIBRARY_PATH=/usr/local/lib
      
      # 设置后,在进行查看一下 
      dargon@dd:~/桌面/CSAPP/TestCode$ echo $LD_LIBRARY_PATH
      /usr/local/lib
      
      # ./it can run my prog
      dargon@dd:~/桌面/CSAPP/TestCode$ ./snooze  10
      ^CSlept for 2 of 10 escs. 
      
      
    2. Disadvantage, it is only valid for the current shell window, when restarting a window, it needs to be reconfigured

  • Method 2: Direct settingldconfig

    1. # 直接进行配置一下 link dynamic library
      sudo ldconfig
      
    2. Description of the function of this commandldconfig creates the necessary links and cache to the most recent shared libraries found in the directories specified on the command line, in the file /etc/ld.so.conf, and in the trusted directories (/lib and /usr/lib).

Guess you like

Origin blog.csdn.net/Dallas01/article/details/123030632