gcc makefile生成静态库和动态库?

发布于 2022-09-02 11:53:39 字数 2071 浏览 17 评论 0

用makefile生成.a静态库和.so动态库,其中静态库总是出错。
平台为Fedora-23-x86_64 gcc-5.3.x

exampleMain.o: exampleMain.cpp
    g++ -std=c++11 -I./ -Wall -g -c exampleMain.cpp

################################
# Basic
################################

example.o: example.cpp
    g++ -std=c++11 -I./ -Wall -g -c example.cpp

basic: example.o exampleMain.o
    g++ -std=c++11 -Wall -g -pthread example.o exampleMain.o -o basic.exe


################################
# Static Library
################################

static_example.o: example.cpp
    g++ -std=c++11 -I./ -Wall -g -c example.cpp -o static_example.o

libexample.a: example.o
    ar rcs libexample.a     example.o

static: libexample.a exampleMain.o
    g++ -static exampleMain.o -L./ -lexample -L/usr/lib64/ -pthread -o static.exe


################################
# Shared Library
################################

shared_example.o: example.cpp
    g++ -std=c++11 -I./ -Wall -g -c -fPIC example.cpp -o shared_example.o

libexample.so: example.o
    g++ -shared -Wl,soname,libexample.so.1 -o libexample.so.1.0.1 shared_example.o

shared: example.so exampleMain.o
    g++ exampleMain.o -o shared.exe -L./ -lexample -pthread


################################
# Clean
################################

clean:
    rm *.o *.so *.a *.exe
  • (1) make basic 正确

  • (2) make static:
    $make static
    g++ -std=c++11 -I./ -Wall -g -c example.cpp
    ar rcs libexample.a example.o
    g++ -std=c++11 -I./ -Wall -g -c exampleMain.cpp
    g++ -static exampleMain.o -L./ -lexample -L/usr/lib64/ -pthread -o static.exe
    /usr/bin/ld: cannot find -lstdc++
    /usr/bin/ld: cannot find -lm
    /usr/bin/ld: cannot find -lpthread
    /usr/bin/ld: cannot find -lc
    collect2: error: ld returned 1 exit status
    makefile:26: recipe for target 'static' failed
    make: * [static] Error 1

  • (3) make shared:
    $make shared
    make: * No rule to make target 'example.so', needed by 'shared'. Stop.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

若沐 2022-09-09 11:53:39

静态库用
ar rv libxxx.a a.o b.o c.o
应该就可以了,就是打包。具体的选项是不是rv我忘了,可以搜一下。

g++下的用法我就不清楚了

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文