Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 22.C_SRCDIR #22

Merged
merged 1 commit into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions 22.C_SRCDIR/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
app
44 changes: 44 additions & 0 deletions 22.C_SRCDIR/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# これは何?

cgoでは、cgoヘッダーのcgoディレクティブにて ```${SRCDIR}``` という変数が利用できます。

```go
/*
#cgo LDFLAGS: -L${SRCDIR}/libs -llibs
*/
import "C"
```

```#include```の部分には利用出来ません。つまり以下は駄目です。

```go
/*
// これはオッケイ
#cgo LDFLAGS: -L${SRCDIR}/libs -llibs

// 以下は駄目
#include "${SRCDIR}/libs/libs.h"
*/
import "C"
```


この値は、実行時に現在のソースファイルのディレクトリに置換されます。ディレクトリパスは絶対パスで展開されます。

https://pkg.go.dev/cmd/cgo に以下の記載で記されています。

> When the cgo directives are parsed, any occurrence of the string ${SRCDIR} will be replaced by the absolute path to the directory containing the source file.

> (cgoディレクティブが解析されるとき、${SRCDIR}という文字列は、ソースファイルを含むディレクトリへの絶対パスに置き換えられます。)

本サンプルを実行すると、以下のようになります。

```sh
$ task
task: [build-lib] make build
gcc -g -O0 -Wall -Wextra -std=c17 -c /workspace/try-golang-cgo/22.C_SRCDIR/libs/libs.c -o /workspace/try-golang-cgo/22.C_SRCDIR/libs/libs.o
gcc -g -O0 -Wall -Wextra -std=c17 -fPIC -shared -o ./liblibs.so /workspace/try-golang-cgo/22.C_SRCDIR/libs/libs.o
task: [build-goapp] go build -o app
task: [run-goapp] ./app
30
```
26 changes: 26 additions & 0 deletions 22.C_SRCDIR/Taskfile.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# https://taskfile.dev

version: '3'

tasks:
default:
cmds:
- task: build-lib
- task: build-goapp
- task: run-goapp
clean:
cmds:
- make -C libs clean
- rm -f ./app
build-lib:
dir: libs
cmds:
- make build
build-goapp:
cmds:
- go build -o app
run-goapp:
cmds:
- ./app
env:
LD_LIBRARY_PATH: libs
2 changes: 2 additions & 0 deletions 22.C_SRCDIR/libs/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.o
*.so
22 changes: 22 additions & 0 deletions 22.C_SRCDIR/libs/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
DESTDIR = .
PROGRAM = liblibs.so
SRCS = $(shell find $(PWD) -name "*.c" -type f)
OBJS = $(SRCS:%.c=%.o)
CC = gcc
INCDIRS =
CFLAGS = -g -O0 -Wall -Wextra -std=c17
LDFLAGS =
LDLIBS =

%.o: %.c
$(CC) $(CFLAGS) -c $< -o $@ $(INCDIRS)

$(PROGRAM): $(OBJS)
$(CC) $(CFLAGS) -fPIC -shared $(LDFLAGS) -o $(DESTDIR)/$(PROGRAM) $(OBJS) $(LDLIBS)

.PHONY: build
build: $(PROGRAM)

.PHONY: clean
clean:
$(RM) $(OBJS) $(DESTDIR)/$(PROGRAM)
5 changes: 5 additions & 0 deletions 22.C_SRCDIR/libs/libs.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include "libs.h"

int add(int x, int y) {
return x+y;
}
6 changes: 6 additions & 0 deletions 22.C_SRCDIR/libs/libs.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef __LIB_H
#define __LIB_H

extern int add(int x, int y);

#endif /* __LIB_H */
22 changes: 22 additions & 0 deletions 22.C_SRCDIR/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package main

/*
#cgo LDFLAGS: -L${SRCDIR}/libs -llibs

#include "libs/libs.h"
*/
import "C"
import "log"

func init() {
log.SetFlags(0)
}

func main() {
var (
x = C.int(10)
y = C.int(20)
)

log.Println(int(C.add(x, y)))
}