Skip to content

Commit 8fc9779

Browse files
authored
Merge pull request #536 from xushiwei/x
doc How-to-migrate-a-C&C++-Library
2 parents 85b16b2 + df2ba37 commit 8fc9779

File tree

1 file changed

+20
-32
lines changed

1 file changed

+20
-32
lines changed

doc/llgo_migration_for_c_c++_third-party_libraries.md doc/How-to-migrate-a-C&C++-Library.md

+20-32
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,29 @@
1-
# LLGO Migration for C/C++ Third-Party Libraries
1+
# How to migrate a C/C++ Library
22

3-
# LLGO Migration for C Third-Party Libraries
3+
# Migrate a C Library
44

5-
## Installing Third-Party Libraries
5+
## Install a C Library
66

7-
### Using Package Manager to Download
7+
We recommend using a package manager (such as brew, apt-get, winget, etc.) to install a C library. For example:
88

99
```bash
1010
brew install inih
1111
```
1212

13-
### Compiling from Source
14-
15-
```bash
16-
# Compile dylib from source
17-
clang++ -dynamiclib x.cpp -o {users third-party libraries path}/lib/libbar.dylib -lfmt -std=c++11
18-
# Generate pc for the corresponding dylib
19-
# Install via `https://github.com/hackerchai/dylib-installer`
20-
21-
```
22-
2313
## Writing Go Files to Link Library Functions
2414

2515
1. On macOS, use `nm -gU libbar.dylib` to parse C-style symbols
2616

27-
```jsx
28-
0000000000003e55 T _ini_parse
17+
```jsx
18+
0000000000003e55 T _ini_parse
19+
```
2920

30-
```
3121
2. Find the function prototype you want to convert in the corresponding .h file
3222

33-
```c
34-
int ini_parse(const char* filename, ini_handler handler, void* user);
23+
```c
24+
int ini_parse(const char* filename, ini_handler handler, void* user);
25+
```
3526
36-
```
3727
3. Create the corresponding Go file
3828
3929
```c
@@ -46,32 +36,30 @@ clang++ -dynamiclib x.cpp -o {users third-party libraries path}/lib/libbar.dylib
4636
```
4737
4. In `inih.go`, use LLGoPackage to specify the location of the third-party library so that llgo can link to the third-party library. Both `pkg-config --libs inih` and `linih` are used to specify the location of the third-party library.
4838

49-
```go
50-
package inih
39+
```go
40+
package inih
5141

52-
import (
53-
// Using go:linkname unsafe is necessary for the next step
54-
_ "unsafe"
55-
)
42+
import (
43+
_ "unsafe" // unsafe is necessary when using go:linkname
44+
)
5645

57-
const (
58-
LLGoPackage = "link: $(pkg-config --libs inih); -linih"
59-
)
46+
const (
47+
LLGoPackage = "link: $(pkg-config --libs inih); -linih"
48+
)
49+
```
6050

61-
```
6251
5. Write the corresponding function in `inih.go`
6352

6453
Note that the basic C function type mapping to Go function type can be found at [https://github.com/goplus/llgo/blob/main/doc/Type-Mapping-between-C-and-Go.md](https://github.com/goplus/llgo/blob/main/doc/Type-Mapping-between-C-and-Go.md). Some types requiring special handling are listed at the end of this document for reference.
6554

6655
```go
6756
//go:linkname Parse C.ini_parse
6857
func Parse(filename *c.Char, handler func(user c.Pointer, section *c.Char, name *c.Char, value *c.Char) c.Int, user c.Pointer) c.Int
69-
7058
```
59+
7160
6. Write the function call in `inih_demo.go`
7261

7362
```go
74-
7563
package main
7664

7765
import (

0 commit comments

Comments
 (0)