co is being sponsored by the following tool; please help to support us by taking a look and signing up to a free trial.
Basic (中文)
CO
is an elegant and efficient C++ base library that supports Linux, Windows and Mac platforms.
CO
pursues minimalism and efficiency. It does not rely on third-party libraries such as boost, and uses only a few C++11 features.
- CO contains the following functional components:
- Basic definitions (def)
- Atomic operations (atomic)
- Fast random number generator (random)
- LruMap
- Fast string casting for basic types (fast)
- Efficient byte stream (fastream)
- Efficient strings (fastring)
- String operations (str)
- Command line arguments and configuration file parsing library (flag)
- Efficient streaming log library (log)
- Unit testing framework (unitest)
- Time library (time)
- Thread library (thread)
- Coroutine library (co)
- Efficient json library
- High-performance json rpc framework
- Hash library
- Path library
- File system operations (fs)
- System operations (os)
-
This is a command line arguments and configuration file parsing library that is really considered for programmers. It supports automatic generation of configuration files. It supports integer types with units
k, m, g, t, p
. -
This is a super fast local logging system, see how fast it is below:
log vs glog google glog co/log win2012 HHD 1.6MB/s 180MB/s win10 SSD 3.7MB/s 560MB/s mac SSD 17MB/s 450MB/s linux SSD 54MB/s 1023MB/s The above table is the test result of one million info log (about 50 bytes) continuously printed by a single thread. The co/log is almost two orders of magnitude faster than glog.
Why is it so fast? The first is that it is based on fastream that is 8-25 times faster than
sprintf
. The second is that it has almost no memory allocation operations. -
This is a json library that is comparable to rapidjson in performance. If you use jemalloc, the performance of
parse
andstringify
will be further improved.It does not support the json standard as comprehensively as rapidjson, but meets the basic needs of programmers and is easier to use.
-
This is a golang-style coroutine library with built-in multi-threaded scheduling, which is a great tool for network programming.
-
This is a high-performance rpc framework based on coroutines and json. It supports automatic code generation, easy to use, and single-threaded qps can reach 120k+.
-
co/include
Header files oflibco
. -
co/src
Source files oflibco
. -
co/test
Some test code, each.cc
file will be compiled into a separate test program. -
co/unitest
Some unit test code, each.cc
file corresponds to a different test unit, and all code is compiled into a single test program. -
co/gen
A code generation tool automatically generates rpc framework code according to theproto
file.
Xmake is recommended for compiling the CO
project.
-
Compiler
- Linux: gcc 4.8+
- Mac: clang 3.3+
- Windows: vs2015+
-
Install xmake
For windows, mac and debian/ubuntu, you can directly go to the xmake release page to download the installation package. For other systems, please refer to xmake's installation instructions.
Xmake disables compilation as root by default on linux. ruki says it is not safe. You can add the following line to
~/.bashrc
to enable root compilation:export XMAKE_ROOT=y
-
Quick start
# All commands are executed in the root directory of co (the same below) xmake # build libco and gen by default xmake -a # build all projects (libco, gen, co/test, co/unitest)
-
Build libco
xmake build libco # build libco only xmake -b libco # the same as above xmake b libco # the same as above, required newer version of xmake
-
Build and run unitest code
co/unitest is unit test code that verifies the correctness of the functionality of the base library.
xmake build unitest # build can be abbreviated as -b xmake run unitest -a # run all unit tests xmake r unitest -a # the same as above xmake r unitest -os # run unit test os xmake r unitest -json # run unit test json
-
Build and run test code
co/test contains some test code. You can easily add a
xxx_test.cc
source file in theco/test
directory, and then executexmake build xxx
in the co root directory to build it.xmake build flag # compile flag_test.cc xmake build log # compile log_test.cc xmake build json # compile json_test.cc xmake build rapidjson # compile rapidjson_test.cc xmake build rpc # compile rpc_test.cc xmake r flag -xz # test flag xmake r log # test log xmake r log -cout # also log to terminal xmake r log -perf # performance test xmake r json # test json xmake r rapidjson # test rapidjson xmake r rpc # start rpc server xmake r rpc -c # start rpc client
-
Build gen
xmake build gen # It is recommended to put gen in the system directory (e.g. /usr/local/bin/). gen hello_world.proto
Proto file format can refer to hello_world.proto.
-
Installation
# Install header files, libco, gen by default. xmake install -o pkg # package related files to the pkg directory xmake i -o pkg # the same as above xmake install -o /usr/local # install to the /usr/local directory
izhengfan has helped to provide cmake support:
- Build
libco
andgen
by default. - The library files are in the
build/lib
directory, and the executable files are in thebuild/bin
directory. - You can use
BUILD_ALL
to compile all projects. - You can use
CMAKE_INSTALL_PREFIX
to specify the installation directory.
mkdir build && cd build
cmake ..
cmake .. -DBUILD_ALL=ON -DCMAKE_INSTALL_PREFIX=pkg
make -j8
make install
CO
is licensed under the MIT
License. It includes code from some other projects, which have their own licenses, see details in LICENSE.md.
- The code of co/context is from tbox by ruki, special thanks!
- The English reference documents of CO are translated by Leedehai (1-10), daidai21 (11-15) and google, special thanks!
- ruki has helped to improve the xmake compilation scripts, thanks in particular!
- izhengfan provided cmake compilation scripts, thank you very much!