1.TEMPLATE = lib
2.静态库CONFIG += staticlib
创建XSocket.cpp XSocket.h
testlib.pro
SOURCES += XSocket.cpp
HEADERS += XSocket.h
TARGET = testlib
TEMPLATE = lib
xz@xiaqiu:~/study/csdn/day0/testlib$ ls
testlib.pro XSocket.cpp XSocket.h
xz@xiaqiu:~/study/csdn/day0/testlib$ mkdir build
xz@xiaqiu:~/study/csdn/day0/testlib$ cd build/
xz@xiaqiu:~/study/csdn/day0/testlib/build$ qmake ..
xz@xiaqiu:~/study/csdn/day0/testlib/build$ ls
Makefile
xz@xiaqiu:~/study/csdn/day0/testlib/build$ make
g++ -c -pipe -O2 -std=gnu++1z -Wall -Wextra -D_REENTRANT -fPIC -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -I../../testlib -I. -I/opt/Qt/Qt6/6.2.0/gcc_64/include -I/opt/Qt/Qt6/6.2.0/gcc_64/include/QtGui -I/opt/Qt/Qt6/6.2.0/gcc_64/include/QtCore -I. -I/opt/Qt/Qt6/6.2.0/gcc_64/mkspecs/linux-g++ -o XSocket.o ../XSocket.cpp
rm -f libtestlib.so.1.0.0 libtestlib.so libtestlib.so.1 libtestlib.so.1.0
g++ -Wl,-O1 -Wl,-rpath,/opt/Qt/Qt6/6.2.0/gcc_64/lib -shared -Wl,-soname,libtestlib.so.1 -o libtestlib.so.1.0.0 XSocket.o /opt/Qt/Qt6/6.2.0/gcc_64/lib/libQt6Gui.so /opt/Qt/Qt6/6.2.0/gcc_64/lib/libQt6Core.so -lpthread -lGL
ln -s libtestlib.so.1.0.0 libtestlib.so
ln -s libtestlib.so.1.0.0 libtestlib.so.1
ln -s libtestlib.so.1.0.0 libtestlib.so.1.0
xz@xiaqiu:~/study/csdn/day0/testlib/build$ ls
libtestlib.so libtestlib.so.1.0 Makefile
libtestlib.so.1 libtestlib.so.1.0.0 XSocket.o
xz@xiaqiu:~/study/csdn/day0/testlib/build$
xz@xiaqiu:~/study/csdn/day0/testlib/build$ nm -D libtestlib.so
w __cxa_finalize
w __gmon_start__
w _ITM_deregisterTMCloneTable
w _ITM_registerTMCloneTable
xz@xiaqiu:~/study/csdn/day0/testlib/build$
testlib.pro
SOURCES += XSocket.cpp
HEADERS += XSocket.h
TARGET = testlib
TEMPLATE = lib
CONFIG += staticlib
xz@xiaqiu:~/study/csdn/day0/testlib$ rm -rf build/
xz@xiaqiu:~/study/csdn/day0/testlib$ mkdir build
xz@xiaqiu:~/study/csdn/day0/testlib$ cd build/
xz@xiaqiu:~/study/csdn/day0/testlib/build$ qmake ..
xz@xiaqiu:~/study/csdn/day0/testlib/build$ make
g++ -c -pipe -O2 -fPIC -std=gnu++1z -Wall -Wextra -D_REENTRANT -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -I../../testlib -I. -I/opt/Qt/Qt6/6.2.0/gcc_64/include -I/opt/Qt/Qt6/6.2.0/gcc_64/include/QtGui -I/opt/Qt/Qt6/6.2.0/gcc_64/include/QtCore -I. -I/opt/Qt/Qt6/6.2.0/gcc_64/mkspecs/linux-g++ -o XSocket.o ../XSocket.cpp
rm -f libtestlib.a
ar cqs libtestlib.a XSocket.o
xz@xiaqiu:~/study/csdn/day0/testlib/build$ ls
libtestlib.a Makefile XSocket.o
xz@xiaqiu:~/study/csdn/day0/testlib/build$
新建xglobal.h
xglobal.h
#pragma once
#include <QtCore/qglobal.h>
#ifdef LINUXLIB_LIB
#define XEXPORT Q_DECL_EXPORT
#else
#define XEXPORT Q_DECL_IMPORT
#endif
XSocket.h
#pragma once
#include "xglobal.h"
class XEXPORT XSocket
{
public:
XSocket();
};
XSocket.cpp
#include "XSocket.h"
#include <iostream>
using namespace std;
XSocket::XSocket()
{
cout<<"create XSocket"<<endl;
}
testlib.pro
SOURCES += XSocket.cpp
HEADERS += XSocket.h xglobal.h
TARGET = testlib
TEMPLATE = lib
#CONFIG += staticlib
CONFIG += console
DEFINES += LINUXLIB_LIB
xz@xiaqiu:~/study/csdn/day0/testlib/build$ rm -rf *
xz@xiaqiu:~/study/csdn/day0/testlib/build$ qmake ..
xz@xiaqiu:~/study/csdn/day0/testlib/build$ make
g++ -c -pipe -O2 -std=gnu++1z -Wall -Wextra -D_REENTRANT -fPIC -DLINUXLIB_LIB -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -I../../testlib -I. -I/opt/Qt/Qt6/6.2.0/gcc_64/include -I/opt/Qt/Qt6/6.2.0/gcc_64/include/QtGui -I/opt/Qt/Qt6/6.2.0/gcc_64/include/QtCore -I. -I/opt/Qt/Qt6/6.2.0/gcc_64/mkspecs/linux-g++ -o XSocket.o ../XSocket.cpp
rm -f libtestlib.so.1.0.0 libtestlib.so libtestlib.so.1 libtestlib.so.1.0
g++ -Wl,-O1 -Wl,-rpath,/opt/Qt/Qt6/6.2.0/gcc_64/lib -shared -Wl,-soname,libtestlib.so.1 -o libtestlib.so.1.0.0 XSocket.o /opt/Qt/Qt6/6.2.0/gcc_64/lib/libQt6Gui.so /opt/Qt/Qt6/6.2.0/gcc_64/lib/libQt6Core.so -lpthread -lGL
ln -s libtestlib.so.1.0.0 libtestlib.so
ln -s libtestlib.so.1.0.0 libtestlib.so.1
ln -s libtestlib.so.1.0.0 libtestlib.so.1.0
xz@xiaqiu:~/study/csdn/day0/testlib/build$ ls
libtestlib.so libtestlib.so.1.0 Makefile
libtestlib.so.1 libtestlib.so.1.0.0 XSocket.o
xz@xiaqiu:~/study/csdn/day0/testlib/build$ nm -D libtestlib.so
0000000000004058 B __bss_start
U __cxa_atexit
w __cxa_finalize
0000000000004058 D _edata
0000000000004060 B _end
w __gmon_start__
w _ITM_deregisterTMCloneTable
w _ITM_registerTMCloneTable
U qt_version_tag
0000000000001220 T _ZN7XSocketC1Ev
0000000000001220 T _ZN7XSocketC2Ev
U _ZNKSt5ctypeIcE13_M_widen_initEv
00000000000012b0 W _ZNKSt5ctypeIcE8do_widenEc
U _ZNSo3putEc
U _ZNSo5flushEv
U _ZNSt8ios_base4InitC1Ev
U _ZNSt8ios_base4InitD1Ev
U _ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l
U _ZSt16__throw_bad_castv
U _ZSt4cout
xz@xiaqiu:~/study/csdn/day0/testlib/build$
创建 bin build lib src 目录 移动源码到src
xz@xiaqiu:~/study/csdn/day0/testlib/build$ rm -rf *
xz@xiaqiu:~/study/csdn/day0/testlib/build$ qmake ../src/
xz@xiaqiu:~/study/csdn/day0/testlib/build$ ls
Makefile
xz@xiaqiu:~/study/csdn/day0/testlib/build$ make
g++ -c -pipe -O2 -std=gnu++1z -Wall -Wextra -D_REENTRANT -fPIC -DLINUXLIB_LIB -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -I../src -I. -I/opt/Qt/Qt6/6.2.0/gcc_64/include -I/opt/Qt/Qt6/6.2.0/gcc_64/include/QtGui -I/opt/Qt/Qt6/6.2.0/gcc_64/include/QtCore -I. -I/opt/Qt/Qt6/6.2.0/gcc_64/mkspecs/linux-g++ -o XSocket.o ../src/XSocket.cpp
rm -f libtestlib.so.1.0.0 libtestlib.so libtestlib.so.1 libtestlib.so.1.0
g++ -Wl,-O1 -Wl,-rpath,/opt/Qt/Qt6/6.2.0/gcc_64/lib -shared -Wl,-soname,libtestlib.so.1 -o libtestlib.so.1.0.0 XSocket.o /opt/Qt/Qt6/6.2.0/gcc_64/lib/libQt6Gui.so /opt/Qt/Qt6/6.2.0/gcc_64/lib/libQt6Core.so -lpthread -lGL
ln -s libtestlib.so.1.0.0 libtestlib.so
ln -s libtestlib.so.1.0.0 libtestlib.so.1
ln -s libtestlib.so.1.0.0 libtestlib.so.1.0
xz@xiaqiu:~/study/csdn/day0/testlib/build$ ls
libtestlib.so libtestlib.so.1.0 Makefile
libtestlib.so.1 libtestlib.so.1.0.0 XSocket.o
xz@xiaqiu:~/study/csdn/day0/testlib/build$
把so 放到lib目录
testlib.pro
SOURCES += XSocket.cpp
HEADERS += XSocket.h xglobal.h
TARGET = testlib
TEMPLATE = lib
#CONFIG += staticlib
CONFIG += console
DEFINES += LINUXLIB_LIB
DESTDIR = "$$PWD/../lib"
xz@xiaqiu:~/study/csdn/day0/testlib/build$ rm -rf *
xz@xiaqiu:~/study/csdn/day0/testlib/build$ qmake ../src/
xz@xiaqiu:~/study/csdn/day0/testlib/build$ make
g++ -c -pipe -O2 -std=gnu++1z -Wall -Wextra -D_REENTRANT -fPIC -DLINUXLIB_LIB -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -I../src -I. -I/opt/Qt/Qt6/6.2.0/gcc_64/include -I/opt/Qt/Qt6/6.2.0/gcc_64/include/QtGui -I/opt/Qt/Qt6/6.2.0/gcc_64/include/QtCore -I. -I/opt/Qt/Qt6/6.2.0/gcc_64/mkspecs/linux-g++ -o XSocket.o ../src/XSocket.cpp
rm -f libtestlib.so.1.0.0 libtestlib.so libtestlib.so.1 libtestlib.so.1.0
g++ -Wl,-O1 -Wl,-rpath,/opt/Qt/Qt6/6.2.0/gcc_64/lib -shared -Wl,-soname,libtestlib.so.1 -o libtestlib.so.1.0.0 XSocket.o /opt/Qt/Qt6/6.2.0/gcc_64/lib/libQt6Gui.so /opt/Qt/Qt6/6.2.0/gcc_64/lib/libQt6Core.so -lpthread -lGL
ln -s libtestlib.so.1.0.0 libtestlib.so
ln -s libtestlib.so.1.0.0 libtestlib.so.1
ln -s libtestlib.so.1.0.0 libtestlib.so.1.0
rm -f ../lib/libtestlib.so.1.0.0
mv -f libtestlib.so.1.0.0 ../lib/libtestlib.so.1.0.0
rm -f ../lib/libtestlib.so
rm -f ../lib/libtestlib.so.1
rm -f ../lib/libtestlib.so.1.0
mv -f libtestlib.so ../lib/libtestlib.so
mv -f libtestlib.so.1 ../lib/libtestlib.so.1
mv -f libtestlib.so.1.0 ../lib/libtestlib.so.1.0
xz@xiaqiu:~/study/csdn/day0/testlib/build$ ls ../lib/
libtestlib.so libtestlib.so.1 libtestlib.so.1.0 libtestlib.so.1.0.0
xz@xiaqiu:~/study/csdn/day0/testlib/build$
测试库
xz@xiaqiu:~/study/csdn/day0$ mkdir test
xz@xiaqiu:~/study/csdn/day0$ cd test
xz@xiaqiu:~/study/csdn/day0/test$ subl test.pro
test.pro
QT += core
INCLUDEPATH += "$$PWD/../testlib/src/"
LIBS += -L$$PWD/../testlib/lib -ltestlib
CONFIG += c++11
TARGET = testlib
CONFIG += console
TEMPLATE = app
SOURCES += main.cpp
xz@xiaqiu:~/study/csdn/day0/test$ subl main.cpp
xz@xiaqiu:~/study/csdn/day0/test$
main.cpp
#include <QCoreApplication>
#include "XSocket.h"
#include <iostream>
using namespace std;
int main(int argc,char *argv[])
{
XSocket xsocket;
QCoreApplication a(argc,argv);
std::cout<<"test"<<std::endl;
return a.exec();
}
xz@xiaqiu:~/study/csdn/day0/test$ mkdir build
xz@xiaqiu:~/study/csdn/day0/test$ cd build/
xz@xiaqiu:~/study/csdn/day0/test/build$ qmake ..
xz@xiaqiu:~/study/csdn/day0/test/build$ make
g++ -c -pipe -O2 -std=gnu++1z -Wall -Wextra -D_REENTRANT -fPIC -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -I../../test -I. -I../../testlib/src -I/opt/Qt/Qt6/6.2.0/gcc_64/include -I/opt/Qt/Qt6/6.2.0/gcc_64/include/QtGui -I/opt/Qt/Qt6/6.2.0/gcc_64/include/QtCore -I. -I/opt/Qt/Qt6/6.2.0/gcc_64/mkspecs/linux-g++ -o main.o ../main.cpp
g++ -Wl,-O1 -Wl,-rpath,/opt/Qt/Qt6/6.2.0/gcc_64/lib -o testlib main.o -L/home/xz/study/csdn/day0/test/../testlib/lib -ltestlib /opt/Qt/Qt6/6.2.0/gcc_64/lib/libQt6Gui.so /opt/Qt/Qt6/6.2.0/gcc_64/lib/libQt6Core.so -lpthread -lGL
xz@xiaqiu:~/study/csdn/day0/test/build$ ls
main.o Makefile testlib
xz@xiaqiu:~/study/csdn/day0/test/build$
xz@xiaqiu:~/study/csdn/day0/test/build$ ./testlib
./testlib: error while loading shared libraries: libtestlib.so.1: cannot open shared object file: No such file or directory
xz@xiaqiu:~/study/csdn/day0/test/build$
xz@xiaqiu:~/study/csdn/day0/test/build$ cp ../../testlib/lib/libtestlib.so* .
xz@xiaqiu:~/study/csdn/day0/test/build$ ls
libtestlib.so libtestlib.so.1.0 main.o testlib
libtestlib.so.1 libtestlib.so.1.0.0 Makefile
xz@xiaqiu:~/study/csdn/day0/test/build$ ./testlib
create XSocket
test

2497

被折叠的 条评论
为什么被折叠?



