gcc makefile生成静态库和动态库?
用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
静态库用
ar rv libxxx.a a.o b.o c.o
应该就可以了,就是打包。具体的选项是不是rv我忘了,可以搜一下。
g++下的用法我就不清楚了