编译分支
当前编译的是主分支
https://github.com/zhiyong0804/brks/tree/master
重点:
(1)理解src/CMakeList.txt的意义再去考虑编译;
(2)如果自己在下面的各个编译中,没有找到相关的目录,则自行创建。
(3)不要盲目一遍一遍地去试,遇到编译错误,先思考下哪里可能出问题再百度查找,如果百度不能解决则在群里提问。
对于CMakeList.txt不理解的可以参考 https://www.jianshu.com/p/89f0836d9f33
0. 先理解cmake
先理解src目录下的CMakeList.txt,只有理解了其意义,去编译工程才有意义,否则就是浪费时间。
(1)INCLUDE_DIRECTORIES指定包含的目录;
(2)LINK_DIRECTORIES库文件的目录
(3)TARGET_LINK_LIBRARIES需要链接哪一些库
(4)ADD_SUBDIRECTORY包含的子目录
(5)AUX_SOURCE_DIRECTORY查找在某个路径下的所有源文件
#NodeServer cmake config file for src
#cmake version request
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
#project name工程名
PROJECT(brks)
#Head file path 头文件
# ../third/include是第三方库文件的头文件所在路径
INCLUDE_DIRECTORIES(../third/include)
INCLUDE_DIRECTORIES(./service)
INCLUDE_DIRECTORIES(./intf)
INCLUDE_DIRECTORIES(./common)
INCLUDE_DIRECTORIES(./bus)
#library path 库文件所在目录,curl和event是我编译的时候新加的
# 编译出错是可以对比下我这个文件
LINK_DIRECTORIES(../third/lib/curl)
LINK_DIRECTORIES(../third/lib/event)
LINK_DIRECTORIES(../third/lib/jsoncpp)
LINK_DIRECTORIES(../third/lib/tinyxml)
LINK_DIRECTORIES(../third/lib/log4cpp)
LINK_DIRECTORIES(../third/lib/thrift)
LINK_DIRECTORIES(../third/lib/iniparser)
LINK_DIRECTORIES(./common)
LINK_DIRECTORIES(./bus)
LINK_DIRECTORIES(./intf)
LINK_DIRECTORIES(./service)
#debug mode
SET(CMAKE_BUILD_TYPE Debug)
#################################################################
AUX_SOURCE_DIRECTORY(${PROJECT_SOURCE_DIR} SOURCE_FILES)
ADD_EXECUTABLE(brks ${SOURCE_FILES})
#for backtrace_symbols
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -rdynamic -Wall -g3 -m64 -pipe -std=c++0x -lrt -Wno-reorder -Wdeprecated-declarations -fpermissive")
# 需要链接哪一些库(第三方库)
TARGET_LINK_LIBRARIES(brks log4cpp)
TARGET_LINK_LIBRARIES(brks thrift)
TARGET_LINK_LIBRARIES(brks thriftnb)
TARGET_LINK_LIBRARIES(brks event)
TARGET_LINK_LIBRARIES(brks libjsoncpp.a)
TARGET_LINK_LIBRARIES(brks libtinyxml.a)
TARGET_LINK_LIBRARIES(brks curl)
TARGET_LINK_LIBRARIES(brks iniparser)
TARGET_LINK_LIBRARIES(brks mysqlclient)
TARGET_LINK_LIBRARIES(brks pthread)
TARGET_LINK_LIBRARIES(brks dl)
# 链接子目录生成的库文件
TARGET_LINK_LIBRARIES(brks bus)
TARGET_LINK_LIBRARIES(brks intf)
TARGET_LINK_LIBRARIES(brks common)
TARGET_LINK_LIBRARIES(brks service)
#子目录
ADD_SUBDIRECTORY(bus)
ADD_SUBDIRECTORY(common)
ADD_SUBDIRECTORY(intf)
ADD_SUBDIRECTORY(service)
#install
#SET(CMAKE_INSTALL_PREFIX ${CMAKE_CURRENT_BINARY_DIR})
SET(CMAKE_INSTALL_PREFIX ${CMAKE_BINARY_DIR})
INSTALL(TARGETS brks DESTINATION bin)
已经编译的库文件
需要自己再编译的库
- thrift event curl jsoncpp 这部分我编译成静态库放到brks/third的include和lib目录
- mysqlclient lua 直接编译到linux系统
target_link_libraries
TARGET_LINK_LIBRARIES(myProject comm) # 连接libhello.so库,默认优先链接动态库,如果要静态链接请显式链接libcomm.a
TARGET_LINK_LIBRARIES(myProject libcomm.a) # 显式指定链接静态库
TARGET_LINK_LIBRARIES(myProject libcomm.so) # 显式指定链接动态库
再如:
TARGET_LINK_LIBRARIES(myProject libcomm.so) #这些库名写法都可以。
TARGET_LINK_LIBRARIES(myProject comm)
TARGET_LINK_LIBRARIES(myProject -lcomm)
1. 我编译完后third的include和lib
- third/include
lqf@ubuntu:~/dongnao/brks/third/include$ ls
boost curl evdns.h event.h event2 evhttp.h evrpc.h evutil.h include.am iniparser json log4cpp thrift tinyxml
- third/lib
lqf@ubuntu:~/dongnao/brks/third/lib$ ls
boost curl event iniparser jsoncpp log4cpp thrift tinyxml
2. 编译第三方库
2.1. 编译log4cpp
log4cpp 已经编译好,这里只是提供一个编译的方法
- 下载地址
地址:https://github.com/orocos-toolchain/log4cpp.git
lqf@ubuntu:~/dongnao/brks_third/log4cpp$ git clone https://github.com/orocos-toolchain/log4cpp.git
lqf@ubuntu:~/dongnao/brks_third/log4cpp$ git tag
...
v2.8.1
v2.8.3
v2.9.0-rc1
# 选择tag,不用master,master容易有bug
lqf@ubuntu:~/dongnao/brks_third/log4cpp$ git checkout v2.9.0-rc1
-
修改CMakeList.txt
将动态库改为静态库(由SHARED改为STATIC)
- 使用cmake的方式编译
cd log4cpp
mkdir build
cd build
cmake ..
make
- 编译后的build目录下
- 将include的log4cpp整个文件夹拷贝到brks/third/include
- 将 liborocos-log4cpp.a重命名 liblog4cpp.a并拷贝到brks/third/lib/log4cpp
lqf@ubuntu:~/dongnao/brks_third/log4cpp/build$ ls
CMakeCache.txt CMakeFiles Makefile cmake_install.cmake configure-tryouts include liborocos-log4cpp.a orocos-log4cpp.pc
2.2 编译tinyxml
- 下载地址:https://sourceforge.net/projects/tinyxml/
- 下载和解压
wget https://nchc.dl.sourceforge.net/project/tinyxml/tinyxml/2.6.2/tinyxml_2_6_2.zip
unzip tinyxml_2_6_2.zip
cd tinyxml
- 需要修改tinyxml的Makefile以编译静态库
(1)使用vim或者其他的编辑器打开Makefile文件
(2)将其中的注释为Targets of the build的下一行OUTPUT := xmltest
一行修改为:OUTPUT := libtinyxml.a
(3)将其中的注释为Source files 的下一行SRCS:=tinyxml.cpp tinyxml-parser.cpp xmltest.cpp tinyxmlerror.cpp tinystr.cpp中的xmltest.cpp删除,因为它只是一个测试源文件,不需要编译。修改后为:SRCS := tinyxml.cpp tinyxmlparser.cpp tinyxmlerror.cpp tinystr.cpp
(4)将其中的Output的下一行的${LD} -o $@ ${LDFLAGS} ${OBJS} ${LIBS} ${EXTRA_LIBS}修改为:${AR} $@ ${LDFLAGS} ${OBJS} ${LIBS} ${EXTRA_LIBS}。修改后为:
${OUTPUT}: ${OBJS}
${AR} $@ ${LDFLAGS} ${OBJS} ${LIBS} ${EXTRA_LIBS}
# ${LD} -o $@ ${LDFLAGS} ${OBJS} ${LIBS} ${EXTRA_LIBS}
(5)将Makefile的倒数第二行 xmltest.o:tinyxml.h tinystr.h,注释掉,因为不需要将演示程序添加到静态库中。然后保存退出。
#xmltest.o: tinyxml.h tinystr.h
(6)在终端下进入Makefile所在目录,执行make命令编译,即可在Makefile所在目录下生成libtinyxml.a文件。
- 编译完后的目录
lqf@ubuntu:~/dongnao/brks_third/tinyxml$ ls
Makefile readme.txt tinystr.h tinyxml.o tinyxmlerror.cpp utf8test.gif xmltest.cpp
changes.txt tinyXmlTest.vcxproj tinystr.o tinyxml.sln tinyxmlerror.o utf8test.xml xmltest.o
docs tinyXmlTestSTL.vcxproj tinyxml.cpp tinyxmlSTL.vcxproj tinyxmlparser.cpp utf8testverify.xml
libtinyxml.a tinystr.cpp tinyxml.h tinyxml_lib.vcxproj tinyxmlparser.o xmltest
(1)将tinyxml.h拷贝到brks/third/include/tinyxml
(2)将libtinyxml.a拷贝到brks/third/lib/tinyxml
2.3 安装jsoncpp
JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式。
- 先安装scons
sudo apt-get install scons
- 下载和解压
wget https://nchc.dl.sourceforge.net/project/jsoncpp/jsoncpp/0.5.0/jsoncpp-src-0.5.0.tar.gz
tar -zxvf jsoncpp-src-0.5.0.tar.gz
- 编译和安装
cd jsoncpp-src-0.5.0
scons platform=linux-gcc
- 拷贝头文件和库文件
生成的静态库和动态库在jsoncpp-src-0.5.0/libs/linux-gcc-5.4.0目录
lqf@ubuntu:~/dongnao/brks_third/jsoncpp-src-0.5.0/libs/linux-gcc-5.4.0$ ls
libjson_linux-gcc-5.4.0_libmt.a libjson_linux-gcc-5.4.0_libmt.so
(1)将libjson_linux-gcc-5.4.0_libmt.a改名为libjsoncpp.a并拷贝到brks/third/lib/jsoncp
(2)将jsoncpp-src-0.5.0/include目录的json整个目录拷贝到brks/third/include
2.4 安装libcurl
libcurl主要功能就是用不同的协议连接和沟通不同的服务器~也就是相当封装了的sockPHP 支持libcurl(允许你用不同的协议连接和沟通不同的服务器)。, libcurl当前支持http, https, ftp, gopher, telnet, dict, file, 和ldap 协议。libcurl同样支持HTTPS证书授权,HTTP POST, HTTP PUT, FTP 上传(当然你也可以使用PHP的ftp扩展), HTTP基本表单上传,代理,cookies,和用户认证。
libcurl官网:https://curl.haxx.se/libcurl/
- 下载和解压
wget https://curl.haxx.se/download/curl-7.61.1.tar.gz
tar -xzf curl-7.61.1.tar.gz
- 编译和安装
cd curl-7.61.1
./configure
make
make test (可选)
#sudo make install 不安装,自己直接拷贝
- 拷贝头文件和库文件
cd lib/.libs
(1)拷贝库文件到brks/third/lib/curl
cp -arf libcurl.* /home/lqf/dongnao/brks/third/lib/curl
(2)拷贝头文件curl整个目录到/brks/third/include
# 回到curl-7.61.1/include
cd ../../include/
cp -arf curl /home/lqf/dongnao/brks/third/include
2.5 安装libevent
- 下载libevent和解压
wget https://github.com/libevent/libevent/releases/download/release-2.1.8-stable/libevent-2.1.8-stable.tar.gz
tar zxf libevent-2.1.8-stable.tar.gz
- 编译
cd libevent-2.1.8-stable
make
- 拷贝头文件和库文件到brks/third
(1)到libevent-2.1.8-stable/include将目录下所有文件拷贝到brks/third/include
cd include
cp -arf * /home/lqf/dongnao/brks/third/include
(2)到libevent-2.1.8-stable/.libs拷贝库文件到brks/third/lib/event
cd .libs
cp -arf libevent* /home/lqf/dongnao/brks/third/lib/event
- 拷贝到/brks/third后
(1)头文件
lqf@ubuntu:~/dongnao/brks/third/include$ ls
boost curl evdns.h event.h event2 evhttp.h evrpc.h evutil.h include.am iniparser json log4cpp tinyxml
(2)库文件
lqf@ubuntu:~/dongnao/brks/third/lib/event$ ls
libevent-2.1.so.6 libevent_core-2.1.so.6.0.2 libevent_extra.a libevent_openssl.la libevent_pthreads.la
libevent-2.1.so.6.0.2 libevent_core.a libevent_extra.la libevent_openssl.lai libevent_pthreads.lai
libevent.a libevent_core.la libevent_extra.lai libevent_openssl.so libevent_pthreads.so
libevent.la libevent_core.lai libevent_extra.so libevent_openssl_la-bufferevent_openssl.o
libevent.lai libevent_core.so libevent_openssl-2.1.so.6 libevent_pthreads-2.1.so.6
libevent.so libevent_extra-2.1.so.6 libevent_openssl-2.1.so.6.0.2 libevent_pthreads-2.1.so.6.0.2
libevent_core-2.1.so.6 libevent_extra-2.1.so.6.0.2 libevent_openssl.a libevent_pthreads.a
2.6 安装lua
- 下载和解压
wget http://archive.ubuntu.com/ubuntu/pool/main/l/lua5.2/lua5.2_5.2.4.orig.tar.gz
tar zxf lua5.2_5.2.4.orig.tar.gz
- 安装支持库
sudo apt-get install libreadline-dev libncurses5-dev
- 修改makefile
cd lua5.2-5.2.4
vim src/Makefile
- 编译安装
make linux
sudo make install
被安装到
lqf@ubuntu:~/dn/download/lua5.2-5.2.4$ sudo make install
cd src && mkdir -p /usr/local/bin /usr/local/include /usr/local/lib /usr/local/man/man1 /usr/local/share/lua/5.2 /usr/local/lib/lua/5.2
cd src && install -p -m 0755 lua luac /usr/local/bin
cd src && install -p -m 0644 lua.h luaconf.h lualib.h lauxlib.h lua.hpp /usr/local/include
cd src && install -p -m 0644 liblua.a /usr/local/lib
cd doc && install -p -m 0644 lua.1 luac.1 /usr/local/man/man1
- lua不需要拷贝到brks目录,在终端输入 lua -v确认是否安装成功
lqf@ubuntu:~/dongnao/brks_third/curl-7.61.1/lib/.libs$ lua -v
Lua 5.2.4 Copyright (C) 1994-2015 Lua.org, PUC-Rio
2.7 thrift编译
- 下载thrift-0.11.0和解压
wget http://apache.fayea.com/thrift/0.11.0/thrift-0.11.0.tar.gz
tar -zxf thrift-0.11.0.tar.gz
- 安装依赖项,依赖项非常多
sudo apt-get install libboost-dev libboost-test-dev libboost-program-options-dev libevent-dev automake libtool flex bison pkg-config g++ libssl-dev
- 编译和安装
指定安装路径,否则不知道要拷贝哪一些文件,我先把它安装到~/dongnao/brks_third/thrift_lib
编译的时候要 -with-cpp -with-lua
./configure -with-cpp -with-lua -with-boost -without-python -without-csharp -without-java -without-erlang -without-perl -without-php -without-php_extension -without-ruby -without-haskell -without-go
make
# 安装到系统/usr/local,lua脚本也需要thrift支持
sudo make install
报错
libtool: warning: relinking 'libluabpack.la'
libtool: install: (cd /home/lqf/dongnao/brks_third/thrift-0.11.0/lib/lua; /bin/bash "/home/lqf/dongnao/brks_third/thrift-0.11.0/libtool" --tag CC --mode=relink gcc -g -O2 -o libluabpack.la -rpath /usr/local/lib src/libluabpack_la-luabpack.lo liblualongnumber.la -llua -ldl -lm -lrt -lpthread )
libtool: relink: gcc -shared -fPIC -DPIC src/.libs/libluabpack_la-luabpack.o -L/usr/local/lib -llualongnumber -llua -ldl -lm -lrt -lpthread -g -O2 -Wl,-soname -Wl,libluabpack.so.0 -o .libs/libluabpack.so.0.0.0
/usr/bin/ld: cannot find -llualongnumber
collect2: error: ld returned 1 exit status
libtool: error: error: relink 'libluabpack.la' with the above command before installing it
Makefile:659: recipe for target 'install-libLTLIBRARIES' failed
make[4]: *** [install-libLTLIBRARIES] Error 1
make[4]: Leaving directory '/home/lqf/dongnao/brks_third/thrift-0.11.0/lib/lua'
Makefile:1004: recipe for target 'install-am' failed
make[3]: *** [install-am] Error 2
make[3]: Leaving directory '/home/lqf/dongnao/brks_third/thrift-0.11.0/lib/lua'
Makefile:842: recipe for target 'install-recursive' failed
make[2]: *** [install-recursive] Error 1
make[2]: Leaving directory '/home/lqf/dongnao/brks_third/thrift-0.11.0/lib/lua'
Makefile:577: recipe for target 'install-recursive' failed
make[1]: *** [install-recursive] Error 1
make[1]: Leaving directory '/home/lqf/dongnao/brks_third/thrift-0.11.0/lib'
Makefile:660: recipe for target 'install-recursive' failed
需要修改lib/lua/Makefile,大概580行
lib_LTLIBRARIES = \
libluasocket.la \
libluabpack.la \
libluabitwise.la \
liblualongnumber.la
改为
lib_LTLIBRARIES = \
libluasocket.la \
liblualongnumber.la \
libluabitwise.la \
libluabpack.la
重新编译
make
# 安装到系统默认目录
sudo make install
安装到自己的目录方便拷贝头文件和库文件
# 安装到我们自己的另外目录,以方便拷贝到brks/third下
make DESTDIR=/home/lqf/dongnao/brks_third/thrift_lib install
make install后被拷贝到~/dongnao/brks_third/thrift_lib
lqf@ubuntu:~/dongnao/brks_third/thrift_lib$ ls
bin include lib
- 将头文件和库文件拷贝到brks/third
进到~/dongnao/brks_third/thrift_lib/usr/local
(1)将include下的 thrift整个目录拷贝到brks/third/include
(2)将lib下的所有文件拷贝到brks/third/lib/thrift
2.8 安装mysql数据库服务器
- 安装命令
sudo apt-get install mysql-server
sudo apt install mysql-client
sudo apt install libmysqlclient-dev
- 安装后可以通过下面的命令检查mysql是否安装成功
登录mysql,输入在安装时的密码
mysql -u root -p
#输入status查看版本
mysql Ver 14.14 Distrib 5.7.24, for Linux (x86_64) using EditLine wrapper
- mysql退出三种方法
mysql > exit;
mysql > quit;
mysql > \q;
3 编译共享单车
3.1 修改src/CMakeList.txt
在安装完以上第三方库后,并对src/CMakeList.txt进行修改,主要的改动如下所示
添加curl和event的包含路径
#library path
LINK_DIRECTORIES(../third/lib/curl)
LINK_DIRECTORIES(../third/lib/event)
3.2 编译共享单车
- 进入brks/src,并创建build目录
cd brks/src
cmake .
make
make install
lqf@ubuntu:~/dongnao/brks/src$ ./brks
./brks: error while loading shared libraries: libjsoncpp.so: cannot open shared object file: No such file or directory
将libjsoncpp改为静态链接,重新编译
cmake .
make
make install
3.3 执行共享单车
- 启动服务器
在make install后,执行文件brks 被安装到了brks/src目录
我们先将brks/conf下面的所有文件拷贝到brks/src
lqf@ubuntu:~/dongnao/brks/src$ cp -a ../../conf/* .
执行brks ,需要传入log的配置文件,以及brks的配置文件
sudo ./brks log.conf brks.ini
正常启动后打印
Thrift: Sat Dec 15 14:18:02 2018 TNonblockingServer: Serving with 1 io threads.
Thrift: Sat Dec 15 14:18:02 2018 TNonblockingServer: using libevent 2.1.8-stable method epoll
Thrift: Sat Dec 15 14:18:02 2018 TNonblocking: IO thread #0 registered for listen.
Thrift: Sat Dec 15 14:18:02 2018 TNonblocking: IO thread #0 registered for notify.
Thrift: Sat Dec 15 14:18:02 2018 TNonblockingServer: IO thread #0 entering loop...
- 测试用例
进入brks/test,先测试cases.lua,但要修改host地址以及手机号码
(1)20行,修改为:host = '127.0.0.1',
(2)116行,修改为:local r = client:brk_list_travel('18570368134')
然后执行lua cases.lua
lqf@ubuntu:~/dongnao/brks/test$ lua cases.lua
测试结果
1..1
# Started on Sat Dec 15 15:32:03 2018
# Starting class: TestProtocol
not ok 1 TestProtocol.test_brk_list_travel_success
# cases.lua:117: expected: 200, actual: 406
# Ran 1 tests in 0.016 seconds, 0 successes, 1 failure
测试失败,我们要检查下log,查看/var/log/brk.log,发现错误
12-15 15:19:13 689 [140436871661376][ERROR]connect mysql failed Unknown database 'dongnaobike'
12-15 15:19:13 689 [140436871661376][ERROR]create table userinfo table failed. error msg:MySQL server has gone away
12-15 15:19:13 689 [140436871661376][ERROR]create table bikeinfo table failed. error msg:MySQL server has gone away
12-15 15:19:13 689 [140436871661376][ERROR]create table account records table failed. error msg:MySQL server has gone away
12-15 15:19:13 689 [140436871661376][ERROR]create table arenainfo failed. error msg:MySQL server has gone away
12-15 15:19:13 689 [140436871661376][INFO]INTF : try to started thrift service on 9090 port.
12-15 15:19:13 692 [140436871661376][INFO]INTF : started thrift service on 9090 port.
12-15 15:19:13 692 [140436871661376][INFO]brks start successful!
12-15 15:19:26 155 [140436871653120][DEBUG]brk_list_travel.
12-15 15:19:26 155 [140436871653120][DEBUG]dispatch ev : 9
12-15 15:19:26 155 [140436871653120][ERROR]excute sql 'select * from travelinfo where mobile=18570368134' failed.
12-15 15:32:03 410 [140436800022272][DEBUG]brk_list_travel.
12-15 15:32:03 410 [140436800022272][DEBUG]dispatch ev : 9
12-15 15:32:03 410 [140436800022272][ERROR]excute sql 'select * from travelinfo where mobile=18570368134' failed.
从log上看是数据库没有创建,所以如果你还没有学习数据库,那先学习下mysql再继续搞这个项目。
参考
Linux共享对象之编译参数fPIC