-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from devlights/add-cflags-ldflags
- Loading branch information
Showing
11 changed files
with
186 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# これは何? | ||
|
||
CGOヘッダーには、C言語で利用する ```CFLAGS```, ```LDFLAGS``` が利用できます。 | ||
|
||
外部ライブラリを利用する際は、CGOヘッダーにて指定します。 | ||
|
||
```go | ||
/* | ||
#cgo CFLAGS: -I../lib | ||
#cgo LDFLAGS: -L../lib -lmylib | ||
#include <stdlib.h> | ||
#include "lib.c" | ||
*/ | ||
import "C" | ||
``` | ||
|
||
本サンプルを実行すると以下のようになります。 | ||
|
||
```sh | ||
$ task | ||
task: [build-lib] make build | ||
gcc -g -O0 -Wall -Wextra -std=c17 -c /workspace/try-golang-cgo/20.C_CFLAGS_LDFLAGS/lib/lib.c -o /workspace/try-golang-cgo/20.C_CFLAGS_LDFLAGS/lib/lib.o | ||
gcc -g -O0 -Wall -Wextra -std=c17 -fPIC -shared -o ./libmylib.so /workspace/try-golang-cgo/20.C_CFLAGS_LDFLAGS/lib/lib.o | ||
task: [build-capp] make build | ||
gcc -g -O0 -Wall -Wextra -std=c17 -c /workspace/try-golang-cgo/20.C_CFLAGS_LDFLAGS/capp/main.c -o /workspace/try-golang-cgo/20.C_CFLAGS_LDFLAGS/capp/main.o -I../lib | ||
gcc -g -O0 -Wall -Wextra -std=c17 -L../lib -o ./cApp /workspace/try-golang-cgo/20.C_CFLAGS_LDFLAGS/capp/main.o -lmylib | ||
task: [build-goapp] go build -o goApp | ||
task: [run-capp] LD_LIBRARY_PATH=../lib ./cApp | ||
[C ] hello world | ||
task: [run-goapp] ./goApp | ||
[C ] hello world | ||
task: [list-symbols] nm --extern-only capp/cApp | grep -E ' [T|U] (main|myPrint)' | ||
0000000000001149 T main | ||
U myPrint | ||
task: [list-symbols] nm --extern-only goapp/goApp | grep -E ' [T|U] (main|myPrint)' | ||
000000000045c600 T main | ||
0000000000461030 T myPrint | ||
task: [ldd] LD_LIBRARY_PATH=lib ldd capp/cApp | ||
linux-vdso.so.1 (0x00007ffe22799000) | ||
libmylib.so => lib/libmylib.so (0x00007f29c42ce000) | ||
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f29c409e000) | ||
/lib64/ld-linux-x86-64.so.2 (0x00007f29c42da000) | ||
task: [ldd] ldd goapp/goApp | ||
linux-vdso.so.1 (0x00007ffcd01a7000) | ||
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fcf8ac0d000) | ||
/lib64/ld-linux-x86-64.so.2 (0x00007fcf8ae3f000) | ||
``` | ||
|
||
よく見ると、C言語のアプリの方は nm, ldd の結果ともに外部ライブラリの参照となっているが | ||
|
||
cgoの方は、soファイルが取り込まれているのか nm コマンドにて myPrint 関数が ```T``` で表示される。(C言語側は ```U```) | ||
|
||
また、ldd で見た場合に、```libmylib.so``` が出てこない。 |
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 |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# https://taskfile.dev | ||
|
||
version: '3' | ||
|
||
tasks: | ||
default: | ||
cmds: | ||
- task: build-lib | ||
- task: build-capp | ||
- task: build-goapp | ||
- task: run-capp | ||
- task: run-goapp | ||
- task: list-symbols | ||
- task: ldd | ||
clean: | ||
cmds: | ||
- make -C lib clean | ||
- make -C capp clean | ||
- rm -f goapp/goApp | ||
build-lib: | ||
dir: lib | ||
cmds: | ||
- make build | ||
build-capp: | ||
dir: capp | ||
cmds: | ||
- make build | ||
build-goapp: | ||
dir: goapp | ||
cmds: | ||
- go build -o goApp | ||
run-capp: | ||
dir: capp | ||
cmds: | ||
- LD_LIBRARY_PATH=../lib ./cApp | ||
run-goapp: | ||
dir: goapp | ||
cmds: | ||
- ./goApp | ||
list-symbols: | ||
cmds: | ||
- nm --extern-only capp/cApp | grep -E ' [T|U] (main|myPrint)' | ||
- nm --extern-only goapp/goApp | grep -E ' [T|U] (main|myPrint)' | ||
ldd: | ||
cmds: | ||
- LD_LIBRARY_PATH=lib ldd capp/cApp | ||
- ldd goapp/goApp |
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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
cApp | ||
*.o |
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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
DESTDIR = . | ||
PROGRAM = cApp | ||
SRCS = $(shell find $(PWD) -name "*.c" -type f) | ||
OBJS = $(SRCS:%.c=%.o) | ||
CC = gcc | ||
INCDIRS = -I../lib | ||
CFLAGS = -g -O0 -Wall -Wextra -std=c17 | ||
LDFLAGS = -L../lib | ||
LDLIBS = -lmylib | ||
|
||
%.o: %.c | ||
$(CC) $(CFLAGS) -c $< -o $@ $(INCDIRS) | ||
|
||
$(PROGRAM): $(OBJS) | ||
$(CC) $(CFLAGS) $(LDFLAGS) -o $(DESTDIR)/$(PROGRAM) $(OBJS) $(LDLIBS) | ||
|
||
.PHONY: build | ||
build: $(PROGRAM) | ||
|
||
.PHONY: clean | ||
clean: | ||
$(RM) $(OBJS) $(DESTDIR)/$(PROGRAM) |
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#include "lib.h" | ||
|
||
int main() { | ||
myPrint("hello world"); | ||
} |
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
goApp |
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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package main | ||
|
||
/* | ||
#cgo CFLAGS: -I../lib | ||
#cgo LDFLAGS: -L../lib -lmylib | ||
#include <stdlib.h> | ||
#include "lib.c" | ||
*/ | ||
import "C" | ||
import "unsafe" | ||
|
||
func main() { | ||
var ( | ||
goStr = "hello world" | ||
cStr = C.CString(goStr) | ||
) | ||
defer C.free(unsafe.Pointer(cStr)) | ||
|
||
C.myPrint(cStr) | ||
} |
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
*.o |
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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
DESTDIR = . | ||
PROGRAM = libmylib.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) |
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#include <stdio.h> | ||
|
||
void myPrint(const char *msg) { | ||
printf("[C ] %s\n", msg); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#ifndef __LIB_H | ||
#define __LIB_H | ||
|
||
extern void myPrint(const char *msg); | ||
|
||
#endif /* __LIB_H */ |