linux環境で静的ライブラリ、動的ライブラリコンパイラを同時に使用

6108 ワード

1.シーンの適用


いくつかの理由で、静的ライブラリと動的ライブラリを同時に使用してコードをコンパイルする必要があります.ここで静的リンクが必要なのはzbarlibで、動的リンクはopencvライブラリです.午後の苦しい奮闘を経て、ネット上の解決方法と最終的に成功しなかった原因を分かち合います.

2.Makfileインスタンス

CXX=g++

CFLAGS += -I${PWD}/../zbar/include
CFLAGS += -I${PWD}/../opencv/include

LDFLAGS += -Wl,-Bstatic -lzbar -L${PWD}/../zbar/lib 
LDFLAGS += -Wl,-Bdynamic -lpthread -lrt -lopencv_core  -lopencv_highgui  -lopencv_imgproc  -lopencv_ml  -lopencv_video -L${PWD}/../opencv/lib
LDFLAGS += -Wl,--as-needed

objects = main.o
target = main

all:${target}

${target}:$(objects)
    $(CXX) $^ -o $@ ${LDFLAGS}

%.o:%.cpp
    $(CXX) -c ${CFLAGS} $^ -o $@

.PHONY:clean
clean:
    @rm -f  ${target}
    @rm -f  *.o

3.実例分析


makefileでコンパイルされたパラメータは、-Wl、-BStatic-Wl、-Bdynamic-Wl、-as-needed以上の3つのパラメータを使用していることがわかります.パラメータ定義を確認すると、
 -Wl,option
      Pass option as an option to the linker.  If option contains commas, it is split into multiple options at the commas.  You can use this syntax to pass an argument to the option.  For example, -Wl,-Map,output.map passes -Map output.map to the linker.  When using the GNU linker, you can also get the same effect with -Wl,-Map=output.map.  

NOTE: In Ubuntu 8.10 and later versions, for LDFLAGS, the option -Wl,-z,relro is used. To disable, use -Wl,-z,norelro.
主な機能はリンクにパラメータを渡すことで、これを加えなくてもコンパイルできるようです.
   -Bdynamic
   -dy
   -call_shared
       Link against dynamic libraries.  This is only meaningful on platforms for which shared libraries are supported.  This option is normally the default on such platforms.  The different variants of this option are for compatibility with various systems.  You may use this option multiple times on the command line: it affects library searching for -l options which follow it.

   -Bstatic
   -dn
   -non_shared
   -static
       Do not link against shared libraries.  This is only meaningful on platforms for which shared libraries are supported.  The different variants of this option are for compatibility with various systems.  You may use this option multiple times on the command line:
        it affects library searching for -l options which follow it.  This option also implies --unresolved-symbols=report-all.  This option can be used with -shared.  Doing so means that a shared library is being created but that all of the library's external references must be   resolved by pulling in entries from static libraries.

-Bstatic告知リンク、リンク静的ライブラリ-Bdynamic告知リンク、リンク動的ライブラリ
 --as-needed
   --no-as-needed
       This option affects ELF DT_NEEDED tags for dynamic libraries mentioned on the command line after the --as-needed option.   Normally the linker will add a DT_NEEDED tag for each dynamic library mentioned on the command line, regardless of whether the library is actually needed or not.  --as-needed causes a DT_NEEDED tag to only be emitted for a library that at that point in the link satisfies a non-weak undefined symbol reference from a regular object file or, if the library is not found in the DT_NEEDED lists of other libraries, a non-weak undefined symbol reference from another dynamic library.  Object files or libraries appearing on the command line after the library in question do not affect whether the library is seen as needed.  This is similar to the rules for extraction of object files from archives.  --no-as-needed restores the default behaviour.

-as-needed使用したダイナミックライブラリのみにDT_を設定NEEDED.

4.エラー


午後の振り回されを経て、コンパイル文に問題があるだけで、本当に冤罪ですね.
コンパイルエラー:
${target}:$(objects)
    $(CXX) ${LDFLAGS} $^ -o $@

このうち${LDFLAGS}パラメータを$^ -o $@の前に置くと、静的ライブラリ関数が見つからないエラーが発生します.ここでは具体的な分析時間がなく、後で研究する暇があります.

5.参考サイト


http://blog.csdn.net/wangxvfeng101/article/details/15336955 http://blog.csdn.net/nodeathphoenix/article/details/9058531 http://www.cnblogs.com/little-ant/p/3398885.html