-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Yi Wang
committed
Jul 12, 2014
1 parent
5c3f614
commit e511200
Showing
1 changed file
with
19 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,28 @@ | ||
# Go-Cpp # | ||
|
||
This project demonstrates how to make Go programs call C++ facilities. | ||
This project demonstrates how to call C++ code from Go program without | ||
the need of using SWIG. | ||
|
||
The basic idea is to write a | ||
[C wrapper](https://github.com/wangkuiyi/go-cpp/tree/master/c%2B%2B/c%2B%2B_interface_lib) | ||
of the | ||
[C++ facilities](https://github.com/wangkuiyi/go-cpp/tree/master/c%2B%2B/c%2B%2B_static_lib), | ||
of | ||
[C++ code](https://github.com/wangkuiyi/go-cpp/tree/master/c%2B%2B/c%2B%2B_static_lib), | ||
and use `cgo` to call this C wrapper. | ||
|
||
However, it is known that C programs cannot call C++ facilities. The | ||
trick is to write the wrapper in C++, so it can contain some C | ||
functions defined using `extern "C"`; and exports only these C | ||
functions to `cgo`. | ||
## Checkout and Build | ||
|
||
It is also notable that `cgo` allows Go programs to call C functions | ||
in shared libraries only. So we need to build the wrapper as a shared | ||
library. To do so, we build the C++ facilities into a static library, | ||
which is then linked into the wrapper (shared) library. | ||
git clone -b develop https://github.com/wangkuiyi/go-cpp | ||
cd go-cpp/c++ | ||
scons | ||
cd ../go | ||
export GOPATH=`pwd` | ||
cd src/go_prog | ||
go install | ||
DYLD_LIBRARY_PATH=$GOPATH/../c++/ $GOPATH/bin/go_prog | ||
|
||
In this example project, we use `SCons` to build C++ code. | ||
Note that you need to install SCons to build the C++ part. In order | ||
to install SCons on Mac OS X, you can use Homebrew. | ||
|
||
If you are using Linux, the last command to run the demo program is: | ||
|
||
LD_LIBRARY_PATH=$GOPATH/../c++/ $GOPATH/bin/go_prog |