libmuduo compiles finger separately

foreword

When learning the muduo library, I wanted to compile the example of twisted separately, but found that the compilation failed. After trying to confirm that the library has included the calling relationship, I filed it to the article link: Linking sequence problems in dynamic libraries and static libraries .


compile

1. Header file location and muduo library location.

root@ubuntu:/opt/muduo/examples/twisted/finger# ls -l ../../../include/
total 4
drwxr-xr-x 5 root root 4096 May 28 17:00 muduo

root@ubuntu:/opt/muduo/examples/twisted/finger# ls -l ../../../lib/*
-rw-r--r-- 1 root root 3201952 May 28 17:00 ../../../lib/libmuduo_base.a
-rw-r--r-- 1 root root  629644 May 28 17:00 ../../../lib/libmuduo_curl.a
-rw-r--r-- 1 root root 1462478 May 28 17:00 ../../../lib/libmuduo_http.a
-rw-r--r-- 1 root root 3180350 May 28 17:00 ../../../lib/libmuduo_inspect.a
-rw-r--r-- 1 root root 8347124 May 28 17:00 ../../../lib/libmuduo_net.a
-rw-r--r-- 1 root root  439544 May 28 17:00 ../../../lib/libmuduo_protobuf_codec.a
-rw-r--r-- 1 root root 1720140 May 28 17:00 ../../../lib/libmuduo_protorpc.a
-rw-r--r-- 1 root root  694410 May 28 17:00 ../../../lib/libmuduo_protorpc_wire.a

2. The compilation failed, the space is limited, and the part is intercepted.

The current static library link order is: libmuduo_base.abefore, libmuduo_net.aafter

root@ubuntu:/opt/muduo/examples/twisted/finger# g++ -std=c++11 -o twisted_finger01 finger01.cc -I ../../../include/ ../../../lib/libmuduo_base.a ../../../lib/libmuduo_net.a -lpthread
../../../lib/libmuduo_net.a(EventLoop.cc.o): In function `muduo::net::EventLoop::handleRead()':
/opt/muduo-master/muduo/net/EventLoop.cc:250: undefined reference to `muduo::Logger::Logger(muduo::Logger::SourceFile, int, muduo::Logger::LogLevel)'
/opt/muduo-master/muduo/net/EventLoop.cc:250: undefined reference to `muduo::LogStream::operator<<(long)'
/opt/muduo-master/muduo/net/EventLoop.cc:250: undefined reference to `muduo::Logger::~Logger()'

3. Verify that the library contains linking issues.

Confirm that the corresponding library contains the Logger symbol, refer to the link: nm and ldd usage , it is more accurate to replace nm with strings, and confirm that the library contains Loggersymbols

root@ubuntu:/opt/muduo/examples/twisted/finger# nm ../../../lib/libmuduo_base.a |grep Logger
00000000000008c0 T _ZN5muduo6Logger11setLogLevelENS0_8LogLevelE
00000000000008f0 T _ZN5muduo6Logger11setTimeZoneERKNS_8TimeZoneE
0000000000000090 T _ZN5muduo6Logger4Impl10formatTimeEv
00000000000005e0 T _ZN5muduo6Logger4Impl6finishEv
0000000000000360 T _ZN5muduo6Logger4ImplC1ENS0_8LogLevelEiRKNS0_10SourceFileEi
0000000000000360 T _ZN5muduo6Logger4ImplC2ENS0_8LogLevelEiRKNS0_10SourceFileEi
00000000000008e0 T _ZN5muduo6Logger8setFlushEPFvvE
00000000000008d0 T _ZN5muduo6Logger9setOutputEPFvPKciE
00000000000006e0 T _ZN5muduo6LoggerC1ENS0_10SourceFileEi
0000000000000830 T _ZN5muduo6LoggerC1ENS0_10SourceFileEib
0000000000000800 T _ZN5muduo6LoggerC1ENS0_10SourceFileEiNS0_8LogLevelE
0000000000000710 T _ZN5muduo6LoggerC1ENS0_10SourceFileEiNS0_8LogLevelEPKc
00000000000006e0 T _ZN5muduo6LoggerC2ENS0_10SourceFileEi
0000000000000830 T _ZN5muduo6LoggerC2ENS0_10SourceFileEib
0000000000000800 T _ZN5muduo6LoggerC2ENS0_10SourceFileEiNS0_8LogLevelE
0000000000000710 T _ZN5muduo6LoggerC2ENS0_10SourceFileEiNS0_8LogLevelEPKc
0000000000000880 T _ZN5muduo6LoggerD1Ev
0000000000000880 T _ZN5muduo6LoggerD2Ev
                 U _ZN5muduo6LoggerC1ENS0_10SourceFileEib
                 U _ZN5muduo6LoggerD1Ev

solve

1. Replace the position of the static library link sequence.

libmuduo_base.aComing soon libmuduo_net.a. Confirm that the compilation passed.

root@ubuntu:/opt/muduo/examples/twisted/finger# g++ -std=c++11 -o twisted_finger01 finger01.cc -I ../../../include/ ../../../lib/libmuduo_net.a ../../../lib/libmuduo_base.a -lpthread
root@ubuntu:/opt/muduo/examples/twisted/finger#

2. Use Xlinker tool (recommended)

The reason why it is recommended is that there are many libraries used in the actual project. If you try to change the location at this time, you will find that there are too many combinations and the efficiency is low.

g++ -std=c++11 -o twisted_finger01 finger01.cc -I ../../../include/ -Xlinker "-(" ../../../lib/libmuduo_base.a ../../../lib/libmuduo_net.a -Xlinker "-)" -lpthread
root@ubuntu:/opt/muduo/examples/twisted/finger# 

Note: -Xlinker "-("prefix and suffix and library have spaces

thinking and summarizing

  1. The order of linking is to make A depend on B, then put A in the front, and put the dependent B in the back. Obviously libmuduo_base.a is a dependent library, so put it in the back to avoid.
  2. Use 动态库seems to be able to circumvent this link order problem;
  3. Be very familiar with the code requirements, so that 高聚低耦library A does not depend on library B, and library B depends on library A.

Guess you like

Origin blog.csdn.net/nc_linux/article/details/125021248