msgpack linux下编译出C库和example的例程
一、源码和过程:
### Building and Installing
#### Install from git repository
##### Using the Terminal (CLI)
You will need:
- `gcc >= 4.1.0`
- `cmake >= 2.8.0`
How to build:
$ git clone https://github.com/msgpack/msgpack-c.git
$ cd msgpack-c
$ git checkout c_master
$ cmake .
$ make
$ sudo make install
核心压缩方式可参看官方说明messagepack specification
二、编译出现的错误
编译出现的问题解决:
问题出现主要是在cmake . 这个步骤
重新编译的cmake .的时候,要先把上次的缓存删除CMakeCache.txt,可以直接建一个build文件夹在这个文件夹里面编译
问题基本是缺少对应的库文件
1.-- Could NOT find GTest (missing: GTEST_LIBRARY GTEST_MAIN_LIBRARY)
方法:
sudo apt-get install libgtest-dev
sudo apt-get install cmake # install cmake
cd /usr/src/gtest
sudo cmake CMakeLists.txt
sudo make
#copy or symlink libgtest.a and libgtest_main.a to your /usr/lib folder
sudo cp *.a /usr/lib
2.-- Could NOT find ZLIB (missing: ZLIB_LIBRARY ZLIB_INCLUDE_DIR)
方法:
sudo apt-get install zlib1g zlib1g-dev
3.CMake Warning at example/CMakeLists.txt:1 (FIND_PACKAGE):
By not providing "FindcJSON.cmake" in CMAKE_MODULE_PATH this project has
asked CMake to find a package configuration file provided by "cJSON", but
CMake did not find one.
Could not find a package configuration file provided by "cJSON" with any of
the following names:
cJSONConfig.cmake
cjson-config.cmake
Add the installation prefix of "cJSON" to CMAKE_PREFIX_PATH or set
"cJSON_DIR" to a directory containing one of the above files. If "cJSON"
provides a separate development package or SDK, be sure it has been
installed.
方法:
1).下载cjson地址 https://sourceforge.net/projects/cjson/?source=directory
2).在msgpack-c 下建立cjson目录
3).直接拷贝cJSON.c cJSON.h到msgpack-c 下cjson目录
4).编译对应的libcJSON.so 动态库
gcc cJSON.c -fPIC -lm -shared -o libcJSON.so
注意,如果没有加上-lm会出现下面错误
/home/hhjian/Desktop/work/msgpack/msgpack-c/cjson/libcJSON.so: undefined reference to `pow'
/home/hhjian/Desktop/work/msgpack/msgpack-c/cjson/libcJSON.so: undefined reference to `floor'
collect2: error: ld returned 1 exit status
example/CMakeFiles/jsonconv.dir/build.make:96: recipe for target 'example/jsonconv' failed
make[2]: *** [example/jsonconv] Error 1
CMakeFiles/Makefile2:1310: recipe for target 'example/CMakeFiles/jsonconv.dir/all' failed
make[1]: *** [example/CMakeFiles/jsonconv.dir/all] Error 2
Makefile:140: recipe for target 'all' failed
make: *** [all] Error 2
原因:cjson 使用了#include <math.h>,编译的时候加上-lm
5).编译FindcJSON模块,在cjson 目录建立一个文件FindcJSON.cmake
内容为:
FIND_PATH(CJSON_INCLUDE_DIRS cJSON.h 对应的目录/msgpack-c/cjson)
FIND_LIBRARY(CJSON_LIBRARIES libcJSON.so 对应的目录/msgpack-c/cjson/)
IF(CJSON_INCLUDE_DIRS AND CJSON_LIBRARIES)
SET(cJSON_FOUND TRUE)
ENDIF(CJSON_INCLUDE_DIRS AND CJSON_LIBRARIES)
IF(cJSON_FOUND)
IF(NOT cJSON_FIND_QUIETLY)
MESSAGE(STATUS"Found cjson: ${CJSON_LIBRARIES}")
ENDIF(NOT cJSON_FIND_QUIETLY)
ELSE(cJSON_FOUND)
IF(cJSON_FIND_REQUIRED)
MESSAGE(FATAL_ERROR"Could not find hello library")
ENDIF(cJSON_FIND_REQUIRED)
ENDIF(cJSON_FOUND)
6).添加FindcJSON模块
在msgpack-c 下的CMakeList.txt添加
7).修改example/jsonconv.c
#include <cjson/cJSON.h> 修改为 #include <../cjson/cJSON.h>
注释掉
//case cJSON_Invalid:
// return -1;
8).重新编译
4.
-- Could NOT find Doxygen (missing: DOXYGEN_EXECUTABLE)
方法:
sudo apt-get install doxygen
三、编译成功
编译成功后,在example下对应例子都编译成功了
msgapck-c 对应的库
对应的magpck完整c源码在src和include文件夹,可以直接拿出来移植到不用的平台